Skip to content

Commit

Permalink
Merge pull request #24 from tatang26/feat-clear-middleware
Browse files Browse the repository at this point in the history
feat(server): allowing to clear middleware
  • Loading branch information
paganotoni authored Jun 21, 2024
2 parents 1959fab + 3df970c commit a67d6fc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
9 changes: 9 additions & 0 deletions server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
"time"
)

// baseMiddleware is a list that holds the middleware list that will be executed
// at the beginning of a client request.
var baseMiddleware = []Middleware{
setValuer,
requestID,
recoverer,
logger,
}

// Middleware is a function that receives a http.Handler and returns a http.Handler
// that can be used to wrap the original handler with some functionality.
type Middleware func(http.Handler) http.Handler
Expand Down
7 changes: 1 addition & 6 deletions server/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@ func New(options ...Option) *mux {
router: &router{
prefix: "",
mux: http.NewServeMux(),
middleware: []Middleware{},
middleware: baseMiddleware,
},

host: "0.0.0.0",
port: "3000",
}

ss.Use(logger)
ss.Use(recoverer)
ss.Use(requestID)
ss.Use(setValuer)

for _, option := range options {
option(ss)
}
Expand Down
8 changes: 8 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Router interface {
// Use allows to specify a middleware that should be executed for all the handlers
Use(middleware ...Middleware)

// ResetMiddleware clears the list of middleware on the router by setting the baseMiddleware.
ResetMiddleware()

// Handle allows to register a new handler for a specific pattern
Handle(pattern string, handler http.Handler)

Expand Down Expand Up @@ -43,6 +46,11 @@ func (rg *router) Use(middleware ...Middleware) {
rg.middleware = append(middleware, rg.middleware...)
}

// ResetMiddleware clears the list of middleware on the router by setting the baseMiddleware.
func (rg *router) ResetMiddleware() {
rg.middleware = baseMiddleware
}

// Handle allows to register a new handler for a specific pattern
// in the group with the middleware that should be executed for the handler
// specified in the group.
Expand Down
54 changes: 54 additions & 0 deletions server/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,57 @@ func TestRouter(t *testing.T) {
}

}

func TestMiddleware(t *testing.T) {
s := server.New()
s.Use(server.InCtxMiddleware("customValue", "Hello, World!"))

s.Group("/", func(r server.Router) {
r.HandleFunc("GET /mw/{$}", func(w http.ResponseWriter, r *http.Request) {
v := r.Context().Value("customValue").(string)
w.Write([]byte(v))
})

r.Group("/without", func(r server.Router) {
r.ResetMiddleware()

r.HandleFunc("GET /mw/{$}", func(w http.ResponseWriter, r *http.Request) {
v, ok := r.Context().Value("customValue").(string)
if !ok {
w.Write([]byte("customValue not found"))
return
}
w.Write([]byte(v))
})
})

r.Group("/other-with", func(r server.Router) {
r.HandleFunc("GET /mw/{$}", func(w http.ResponseWriter, r *http.Request) {
v := r.Context().Value("customValue").(string)
w.Write([]byte(v + " (again)"))
})
})
})

testCases := []struct {
description string
pattern string
expected string
}{
{"request to handler with middleware", "/mw/", "Hello, World!"},
{"request to handler without middleware", "/without/mw/", "customValue not found"},
{"request to other handler with middleware", "/other-with/mw/", "Hello, World! (again)"},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, tc.pattern, nil)
res := httptest.NewRecorder()
s.Handler().ServeHTTP(res, req)

if res.Body.String() != tc.expected {
t.Errorf("Expected body %s, got %s", tc.expected, res.Body.String())
}
})
}
}

0 comments on commit a67d6fc

Please sign in to comment.