fix(SimpleRouter): global middlewares

This commit is contained in:
2025-01-22 01:03:31 +01:00
parent 46a47e7cd5
commit 3d413a8605
3 changed files with 27 additions and 13 deletions

View File

@@ -66,9 +66,9 @@ func (rg *RouteGroup) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rg.mux.ServeHTTP(w, r)
})
mwlen := len(rg.global_middlewares) - 1
for i := range rg.global_middlewares {
handler = rg.global_middlewares[mwlen-i](handler)
mwlen := len(*rg.global_middlewares) - 1
for i := range *rg.global_middlewares {
handler = (*rg.global_middlewares)[mwlen-i](handler)
}
handler.ServeHTTP(w, r)
@@ -81,7 +81,7 @@ func (rg *RouteGroup) Handler(r *http.Request) (h http.Handler, pattern string)
// Creates a new SimpleRouter with the basePath of "/"
func SimpleRouter() *RouteGroup {
return &RouteGroup{mux: http.NewServeMux(), basePath: "/"}
return &RouteGroup{mux: http.NewServeMux(), basePath: "/", global_middlewares: &[]Middleware{}}
}
// Puts middlewares on top of existing ones in order
@@ -105,20 +105,21 @@ func (rg *RouteGroup) PopMiddleware() Middleware {
return lastMiddleware
}
// this vs PushMiddleware ? what's the diff?
func (rg *RouteGroup) PushGlobalMiddleware(first Middleware, others ...Middleware) *RouteGroup {
rg.global_middlewares = append(rg.global_middlewares, first)
rg.global_middlewares = append(rg.global_middlewares, others...)
*rg.global_middlewares = append(*rg.global_middlewares, first)
*rg.global_middlewares = append(*rg.global_middlewares, others...)
return rg
}
func (rg *RouteGroup) PopGlobalMiddleware() Middleware {
if len(rg.global_middlewares) == 0 {
if len(*rg.global_middlewares) == 0 {
return nil
}
lastIndex := len(rg.global_middlewares) - 1
lastMiddleware := rg.global_middlewares[lastIndex]
rg.global_middlewares = rg.global_middlewares[:lastIndex]
lastIndex := len(*rg.global_middlewares) - 1
lastMiddleware := (*rg.global_middlewares)[lastIndex]
*rg.global_middlewares = (*rg.global_middlewares)[:lastIndex]
return lastMiddleware
}
@@ -148,6 +149,7 @@ func (rg *RouteGroup) SubPath(path string) *RouteGroup {
rgc := &RouteGroup{
mux: rg.mux,
basePath: rg.basePath + path + "/",
global_middlewares: rg.global_middlewares,
}
middlewares := make([]Middleware, len(rg.middlewares))
copy(middlewares, rg.middlewares)

View File

@@ -192,12 +192,24 @@ func Test_globalMiddlewares(t *testing.T) {
})
}
simpleRouter := SimpleRouter()
simpleRouter2 := simpleRouter.SubPath("2")
simpleRouter.PushGlobalMiddleware(gmw1)
simpleRouter.PushGlobalMiddleware(gmw2)
simpleRouter.PushMiddleware(mw1, mw2)
simpleRouter.GET("test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
simpleRouter.GET("test2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
// Subpath
simpleRouter2.GET("test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
_ts := httptest.NewServer(simpleRouter2)
_req, _ := http.NewRequest("GET", _ts.URL+"/2/test", nil)
_, _err := http.DefaultClient.Do(_req)
Testing.AssertNotError(t, _err)
Testing.AssertEqual(t, 12, counter)
counter = 0
// Subpath
ts := httptest.NewServer(simpleRouter)
req, _ := http.NewRequest("GET", ts.URL+"/test", nil)
_, err := http.DefaultClient.Do(req)

View File

@@ -8,5 +8,5 @@ type RouteGroup struct {
mux *http.ServeMux // A ponter to the underlying ServeMux, this allows us to call ListenAndServe on any instance
basePath string // The current path we are defining handlers on / appending to
middlewares []Middleware // Stack of middlewares that will be applied on a handler in order
global_middlewares []Middleware
global_middlewares *[]Middleware
}