fix(SimpleRouter): global middlewares
This commit is contained in:
@@ -66,9 +66,9 @@ func (rg *RouteGroup) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
rg.mux.ServeHTTP(w, r)
|
rg.mux.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
mwlen := len(rg.global_middlewares) - 1
|
mwlen := len(*rg.global_middlewares) - 1
|
||||||
for i := range rg.global_middlewares {
|
for i := range *rg.global_middlewares {
|
||||||
handler = rg.global_middlewares[mwlen-i](handler)
|
handler = (*rg.global_middlewares)[mwlen-i](handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.ServeHTTP(w, r)
|
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 "/"
|
// Creates a new SimpleRouter with the basePath of "/"
|
||||||
func SimpleRouter() *RouteGroup {
|
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
|
// Puts middlewares on top of existing ones in order
|
||||||
@@ -105,20 +105,21 @@ func (rg *RouteGroup) PopMiddleware() Middleware {
|
|||||||
return lastMiddleware
|
return lastMiddleware
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this vs PushMiddleware ? what's the diff?
|
||||||
func (rg *RouteGroup) PushGlobalMiddleware(first Middleware, others ...Middleware) *RouteGroup {
|
func (rg *RouteGroup) PushGlobalMiddleware(first Middleware, others ...Middleware) *RouteGroup {
|
||||||
rg.global_middlewares = append(rg.global_middlewares, first)
|
*rg.global_middlewares = append(*rg.global_middlewares, first)
|
||||||
rg.global_middlewares = append(rg.global_middlewares, others...)
|
*rg.global_middlewares = append(*rg.global_middlewares, others...)
|
||||||
return rg
|
return rg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rg *RouteGroup) PopGlobalMiddleware() Middleware {
|
func (rg *RouteGroup) PopGlobalMiddleware() Middleware {
|
||||||
if len(rg.global_middlewares) == 0 {
|
if len(*rg.global_middlewares) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
lastIndex := len(rg.global_middlewares) - 1
|
lastIndex := len(*rg.global_middlewares) - 1
|
||||||
lastMiddleware := rg.global_middlewares[lastIndex]
|
lastMiddleware := (*rg.global_middlewares)[lastIndex]
|
||||||
rg.global_middlewares = rg.global_middlewares[:lastIndex]
|
*rg.global_middlewares = (*rg.global_middlewares)[:lastIndex]
|
||||||
|
|
||||||
return lastMiddleware
|
return lastMiddleware
|
||||||
}
|
}
|
||||||
@@ -148,6 +149,7 @@ func (rg *RouteGroup) SubPath(path string) *RouteGroup {
|
|||||||
rgc := &RouteGroup{
|
rgc := &RouteGroup{
|
||||||
mux: rg.mux,
|
mux: rg.mux,
|
||||||
basePath: rg.basePath + path + "/",
|
basePath: rg.basePath + path + "/",
|
||||||
|
global_middlewares: rg.global_middlewares,
|
||||||
}
|
}
|
||||||
middlewares := make([]Middleware, len(rg.middlewares))
|
middlewares := make([]Middleware, len(rg.middlewares))
|
||||||
copy(middlewares, rg.middlewares)
|
copy(middlewares, rg.middlewares)
|
||||||
|
|||||||
@@ -192,12 +192,24 @@ func Test_globalMiddlewares(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
simpleRouter := SimpleRouter()
|
simpleRouter := SimpleRouter()
|
||||||
|
simpleRouter2 := simpleRouter.SubPath("2")
|
||||||
|
|
||||||
simpleRouter.PushGlobalMiddleware(gmw1)
|
simpleRouter.PushGlobalMiddleware(gmw1)
|
||||||
simpleRouter.PushGlobalMiddleware(gmw2)
|
simpleRouter.PushGlobalMiddleware(gmw2)
|
||||||
simpleRouter.PushMiddleware(mw1, mw2)
|
simpleRouter.PushMiddleware(mw1, mw2)
|
||||||
simpleRouter.GET("test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
simpleRouter.GET("test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||||
simpleRouter.GET("test2", 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)
|
ts := httptest.NewServer(simpleRouter)
|
||||||
req, _ := http.NewRequest("GET", ts.URL+"/test", nil)
|
req, _ := http.NewRequest("GET", ts.URL+"/test", nil)
|
||||||
_, err := http.DefaultClient.Do(req)
|
_, err := http.DefaultClient.Do(req)
|
||||||
|
|||||||
@@ -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
|
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
|
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
|
middlewares []Middleware // Stack of middlewares that will be applied on a handler in order
|
||||||
global_middlewares []Middleware
|
global_middlewares *[]Middleware
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user