You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
740 B
Go
31 lines
740 B
Go
1 year ago
|
package router
|
||
|
|
||
|
// ToMiddleware converts the given handler to a Middleware.
|
||
|
func ToMiddleware(handler func(next Handler) Handler) Middleware {
|
||
|
return func(next HandlerFunc) HandlerFunc {
|
||
|
return handler(next).Handle
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// WithMiddlewares adds given middlewares to given routes.
|
||
|
func WithMiddlewares(ms []Middleware, rs ...Route) []Route {
|
||
|
for i := len(ms) - 1; i >= 0; i-- {
|
||
|
rs = WithMiddleware(ms[i], rs...)
|
||
|
}
|
||
|
return rs
|
||
|
}
|
||
|
|
||
|
// WithMiddleware adds given middleware to given route.
|
||
|
func WithMiddleware(middleware Middleware, rs ...Route) []Route {
|
||
|
routes := make([]Route, len(rs))
|
||
|
|
||
|
for i := range rs {
|
||
|
route := rs[i]
|
||
|
routes[i] = Route{
|
||
|
Matches: route.Matches,
|
||
|
Handler: middleware(route.Handler),
|
||
|
}
|
||
|
}
|
||
|
return routes
|
||
|
}
|