Skip to content

Commit 617cac3

Browse files
committed
Add middleware options
1 parent a48fc73 commit 617cac3

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

middleware.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,56 @@ package castle
22

33
import "net/http"
44

5+
type middlewareOpts struct {
6+
methodFilter string
7+
pathFilter string
8+
ignoreEmpty bool
9+
}
10+
11+
type MiddlewareOpt func(*middlewareOpts)
12+
13+
func WithMethodFilter(method string) MiddlewareOpt {
14+
return func(o *middlewareOpts) {
15+
o.methodFilter = method
16+
}
17+
}
18+
19+
func WithPathFilter(path string) MiddlewareOpt {
20+
return func(o *middlewareOpts) {
21+
o.pathFilter = path
22+
}
23+
}
24+
25+
func WithIgnoreEmpty(ignore bool) MiddlewareOpt {
26+
return func(o *middlewareOpts) {
27+
o.ignoreEmpty = ignore
28+
}
29+
}
30+
531
// Middleware is a function that wraps an http.Handler to inject the Castle context into the request.
632
// If ignoreEmpty is true, it will skip the middleware if the request token is empty.
7-
func Middleware(ignoreEmpty bool) func(next http.Handler) http.Handler {
33+
func Middleware(opts ...MiddlewareOpt) func(next http.Handler) http.Handler {
34+
options := &middlewareOpts{
35+
methodFilter: "POST",
36+
ignoreEmpty: true,
37+
}
38+
39+
for _, opt := range opts {
40+
opt(options)
41+
}
42+
843
return func(next http.Handler) http.Handler {
944
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45+
if options.methodFilter != "" && r.Method != options.methodFilter {
46+
next.ServeHTTP(w, r)
47+
return
48+
}
49+
if options.pathFilter != "" && r.URL.Path != options.pathFilter {
50+
next.ServeHTTP(w, r)
51+
return
52+
}
1053
castleCtx := FromHTTPRequest(r)
11-
if ignoreEmpty && castleCtx.RequestToken == "" {
54+
if options.ignoreEmpty && castleCtx.RequestToken == "" {
1255
next.ServeHTTP(w, r)
1356
return
1457
}

middleware_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func Test_Middleware(t *testing.T) {
1010
t.Run("ctx is set", func(t *testing.T) {
11-
middleware := Middleware(false)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11+
middleware := Middleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1212
castleCtx := FromCtx(r.Context())
1313
if castleCtx == nil {
1414
t.Error("Expected castle context to be present in request context")
@@ -31,7 +31,7 @@ func Test_Middleware(t *testing.T) {
3131
}
3232
})
3333
t.Run("ctx is not set", func(t *testing.T) {
34-
middleware := Middleware(true)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34+
middleware := Middleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3535
castleCtx := FromCtx(r.Context())
3636
if castleCtx != nil {
3737
t.Error("Expected castle context to not be present in request context")

0 commit comments

Comments
 (0)