package router import ( "github.com/containous/mux" "github.com/containous/traefik/old/configuration" "github.com/containous/traefik/old/log" "github.com/containous/traefik/old/middlewares" mauth "github.com/containous/traefik/old/middlewares/auth" "github.com/containous/traefik/old/types" "github.com/urfave/negroni" ) // NewInternalRouterAggregator Create a new internalRouterAggregator func NewInternalRouterAggregator(globalConfiguration configuration.GlobalConfiguration, entryPointName string) *InternalRouterAggregator { var serverMiddlewares []negroni.Handler if globalConfiguration.EntryPoints[entryPointName].WhiteList != nil { ipStrategy := globalConfiguration.EntryPoints[entryPointName].ClientIPStrategy if globalConfiguration.EntryPoints[entryPointName].WhiteList.IPStrategy != nil { ipStrategy = globalConfiguration.EntryPoints[entryPointName].WhiteList.IPStrategy } strategy, err := ipStrategy.Get() if err != nil { log.Fatalf("Error creating whitelist middleware: %s", err) } ipWhitelistMiddleware, err := middlewares.NewIPWhiteLister(globalConfiguration.EntryPoints[entryPointName].WhiteList.SourceRange, strategy) if err != nil { log.Fatalf("Error creating whitelist middleware: %s", err) } if ipWhitelistMiddleware != nil { serverMiddlewares = append(serverMiddlewares, ipWhitelistMiddleware) } } if globalConfiguration.EntryPoints[entryPointName].Auth != nil { authMiddleware, err := mauth.NewAuthenticator(globalConfiguration.EntryPoints[entryPointName].Auth, nil) if err != nil { log.Fatalf("Error creating authenticator middleware: %s", err) } serverMiddlewares = append(serverMiddlewares, authMiddleware) } router := InternalRouterAggregator{} routerWithMiddleware := InternalRouterAggregator{} if globalConfiguration.Metrics != nil && globalConfiguration.Metrics.Prometheus != nil && globalConfiguration.Metrics.Prometheus.EntryPoint == entryPointName { // routerWithMiddleware.AddRouter(metrics.PrometheusHandler{}) } if globalConfiguration.Rest != nil && globalConfiguration.Rest.EntryPoint == entryPointName { routerWithMiddleware.AddRouter(globalConfiguration.Rest) } if globalConfiguration.API != nil && globalConfiguration.API.EntryPoint == entryPointName { routerWithMiddleware.AddRouter(globalConfiguration.API) } if globalConfiguration.Ping != nil && globalConfiguration.Ping.EntryPoint == entryPointName { router.AddRouter(globalConfiguration.Ping) } if globalConfiguration.ACME != nil && globalConfiguration.ACME.HTTPChallenge != nil && globalConfiguration.ACME.HTTPChallenge.EntryPoint == entryPointName { router.AddRouter(globalConfiguration.ACME) } router.AddRouter(&WithMiddleware{router: &routerWithMiddleware, routerMiddlewares: serverMiddlewares}) return &router } // WithMiddleware router with internal middleware type WithMiddleware struct { router types.InternalRouter routerMiddlewares []negroni.Handler } // AddRoutes Add routes to the router func (wm *WithMiddleware) AddRoutes(systemRouter *mux.Router) { realRouter := systemRouter.PathPrefix("/").Subrouter() wm.router.AddRoutes(realRouter) if len(wm.routerMiddlewares) > 0 { if err := realRouter.Walk(wrapRoute(wm.routerMiddlewares)); err != nil { log.Error(err) } } } // InternalRouterAggregator InternalRouter that aggregate other internalRouter type InternalRouterAggregator struct { internalRouters []types.InternalRouter } // AddRouter add a router in the aggregator func (r *InternalRouterAggregator) AddRouter(router types.InternalRouter) { r.internalRouters = append(r.internalRouters, router) } // AddRoutes Add routes to the router func (r *InternalRouterAggregator) AddRoutes(systemRouter *mux.Router) { for _, router := range r.internalRouters { router.AddRoutes(systemRouter) } } // wrapRoute with middlewares func wrapRoute(middlewares []negroni.Handler) func(*mux.Route, *mux.Router, []*mux.Route) error { return func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error { middles := append(middlewares, negroni.Wrap(route.GetHandler())) route.Handler(negroni.New(middles...)) return nil } }