Skip to content

Commit 0f5bf2e

Browse files
committed
Expose http middlewares
1 parent 2c03d12 commit 0f5bf2e

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

faucet.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ type Faucet struct {
3030

3131
mux *chi.Mux // HTTP routing
3232

33-
config *config.Config // faucet configuration
34-
middlewares []Middleware // request middlewares
35-
handlers []Handler // request handlers
33+
config *config.Config // faucet configuration
34+
35+
httpMiddlewares []func(http.Handler) http.Handler // http middlewares (http.Handler -> http.Handler)
36+
rpcMiddlewares []Middleware // JSON-RPC request middlewares (Handler -> Handler)
37+
rpcHandlers []Handler // JSON-RPC request handlers
38+
3639
prepareTxMsgFn PrepareTxMessageFn // transaction message creator
3740

3841
maxSendAmount std.Coins // the max send amount per drip
@@ -52,13 +55,13 @@ func NewFaucet(
5255
logger: noopLogger,
5356
config: config.DefaultConfig(),
5457
prepareTxMsgFn: defaultPrepareTxMessage,
55-
middlewares: nil, // no middlewares by default
58+
rpcMiddlewares: nil, // no rpcMiddlewares by default
5659

5760
mux: chi.NewMux(),
5861
}
5962

6063
// Set the single default HTTP handler
61-
f.handlers = []Handler{
64+
f.rpcHandlers = []Handler{
6265
{
6366
f.defaultHTTPHandler,
6467
"/",
@@ -99,11 +102,17 @@ func NewFaucet(
99102
// Branch off another route group, so they don't influence
100103
// "standard" routes like health
101104
f.mux.Group(func(r chi.Router) {
102-
for _, h := range f.handlers {
105+
// Apply HTTP transport middlewares
106+
for _, mw := range f.httpMiddlewares {
107+
r.Use(mw)
108+
}
109+
110+
// Apply JSON-RPC request middlewares
111+
for _, h := range f.rpcHandlers {
103112
r.Post(h.Pattern,
104113
wrapJSONRPC(
105114
h.HandlerFunc,
106-
f.middlewares...,
115+
f.rpcMiddlewares...,
107116
),
108117
)
109118
}

faucet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestFaucet_NewFaucet(t *testing.T) {
8686
&mockEstimator{},
8787
&mockClient{},
8888
WithConfig(config.DefaultConfig()),
89-
WithHandlers(handlers),
89+
WithRPCHandlers(handlers),
9090
)
9191

9292
require.NotNil(t, f)

handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
errInvalidMethod = errors.New("unknown RPC method call")
2525
)
2626

27-
// wrapJSONRPC wraps the given handler and middlewares into a JSON-RPC 2.0 pipeline
27+
// wrapJSONRPC wraps the given handler and rpcMiddlewares into a JSON-RPC 2.0 pipeline
2828
func wrapJSONRPC(handlerFn HandlerFunc, mws ...Middleware) http.HandlerFunc {
2929
callChain := chainMiddlewares(mws...)(handlerFn)
3030

@@ -60,7 +60,7 @@ func wrapJSONRPC(handlerFn HandlerFunc, mws ...Middleware) http.HandlerFunc {
6060
}
6161

6262
// Parse the request.
63-
// This executes all the middlewares, and
63+
// This executes all the rpcMiddlewares, and
6464
// finally the base handler for the endpoint
6565
resp := callChain(ctx, req)
6666

@@ -84,7 +84,7 @@ func wrapJSONRPC(handlerFn HandlerFunc, mws ...Middleware) http.HandlerFunc {
8484
}
8585
}
8686

87-
// chainMiddlewares combines the given middlewares
87+
// chainMiddlewares combines the given rpcMiddlewares
8888
func chainMiddlewares(mw ...Middleware) Middleware {
8989
return func(final HandlerFunc) HandlerFunc {
9090
h := final

options.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package faucet
22

33
import (
44
"log/slog"
5+
"net/http"
56

67
"github.com/gnolang/faucet/config"
78
)
@@ -22,18 +23,26 @@ func WithConfig(c *config.Config) Option {
2223
}
2324
}
2425

25-
// WithMiddlewares specifies the request middlewares for the faucet.
26-
// Middlewares are applied for each handler
26+
// WithMiddlewares specifies the rpc request middlewares for the faucet.
27+
// Middlewares are applied for each endpoint
2728
func WithMiddlewares(middlewares []Middleware) Option {
2829
return func(f *Faucet) {
29-
f.middlewares = middlewares
30+
f.rpcMiddlewares = middlewares
3031
}
3132
}
3233

33-
// WithHandlers specifies the HTTP handlers for the faucet
34-
func WithHandlers(handlers []Handler) Option {
34+
// WithHTTPMiddlewares specifies the http request middlewares for the faucet.
35+
// Middlewares are applied for each endpoint
36+
func WithHTTPMiddlewares(middlewares []func(http.Handler) http.Handler) Option {
3537
return func(f *Faucet) {
36-
f.handlers = append(f.handlers, handlers...)
38+
f.httpMiddlewares = middlewares
39+
}
40+
}
41+
42+
// WithRPCHandlers specifies the HTTP handlers for the faucet
43+
func WithRPCHandlers(handlers []Handler) Option {
44+
return func(f *Faucet) {
45+
f.rpcHandlers = append(f.rpcHandlers, handlers...)
3746
}
3847
}
3948

0 commit comments

Comments
 (0)