2018-04-23 13:30:03 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/containous/mux"
|
2018-11-14 09:18:03 +00:00
|
|
|
"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"
|
2018-04-23 13:30:03 +00:00
|
|
|
"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 {
|
2018-08-24 14:20:03 +00:00
|
|
|
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)
|
2018-04-23 13:30:03 +00:00
|
|
|
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{}
|
2018-11-14 09:18:03 +00:00
|
|
|
routerWithMiddleware := InternalRouterAggregator{}
|
2018-04-23 13:30:03 +00:00
|
|
|
|
|
|
|
if globalConfiguration.Metrics != nil && globalConfiguration.Metrics.Prometheus != nil && globalConfiguration.Metrics.Prometheus.EntryPoint == entryPointName {
|
2018-11-14 09:18:03 +00:00
|
|
|
// routerWithMiddleware.AddRouter(metrics.PrometheusHandler{})
|
2018-04-23 13:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if globalConfiguration.Rest != nil && globalConfiguration.Rest.EntryPoint == entryPointName {
|
2018-11-14 09:18:03 +00:00
|
|
|
routerWithMiddleware.AddRouter(globalConfiguration.Rest)
|
2018-04-23 13:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if globalConfiguration.API != nil && globalConfiguration.API.EntryPoint == entryPointName {
|
2018-11-14 09:18:03 +00:00
|
|
|
routerWithMiddleware.AddRouter(globalConfiguration.API)
|
2018-04-23 13:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if globalConfiguration.Ping != nil && globalConfiguration.Ping.EntryPoint == entryPointName {
|
2018-11-14 09:18:03 +00:00
|
|
|
router.AddRouter(globalConfiguration.Ping)
|
2018-04-23 13:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if globalConfiguration.ACME != nil && globalConfiguration.ACME.HTTPChallenge != nil && globalConfiguration.ACME.HTTPChallenge.EntryPoint == entryPointName {
|
|
|
|
router.AddRouter(globalConfiguration.ACME)
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
router.AddRouter(&WithMiddleware{router: &routerWithMiddleware, routerMiddlewares: serverMiddlewares})
|
2018-04-23 13:30:03 +00:00
|
|
|
|
|
|
|
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 {
|
2018-08-06 18:00:03 +00:00
|
|
|
if err := realRouter.Walk(wrapRoute(wm.routerMiddlewares)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-04-23 13:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|