示例#1
0
func (s *server) handleWithMiddleware(
	ctx types.Context,
	route types.Route) types.APIFunc {

	/*if route.GetMethod() == "HEAD" {
		return route.GetHandler()
	}*/

	handler := route.GetHandler()

	middlewaresForRouteName, ok := s.routeHandlers[route.GetName()]
	if !ok {
		ctx.Warn("no middlewares for route")
	} else {
		for h := range reverse(middlewaresForRouteName) {
			handler = h.Handler(handler)
			ctx.WithField(
				"middleware", h.Name()).Debug("added route middleware")
		}
	}

	// add the global handlers
	for h := range reverse(s.globalHandlers) {
		handler = h.Handler(handler)
		ctx.WithField(
			"middleware", h.Name()).Debug("added global middleware")
	}

	return handler
}
示例#2
0
func (s *server) addRouterMiddleware(
	r types.Route, middlewares ...types.Middleware) {

	middlewaresForRouteName, ok := s.routeHandlers[r.GetName()]
	if !ok {
		middlewaresForRouteName = []types.Middleware{}
	}
	middlewaresForRouteName = append(middlewaresForRouteName, middlewares...)
	s.routeHandlers[r.GetName()] = middlewaresForRouteName
}