2018-11-14 09:18:03 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/containous/alice"
|
2019-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/pkg/config"
|
|
|
|
"github.com/containous/traefik/pkg/log"
|
|
|
|
"github.com/containous/traefik/pkg/middlewares/accesslog"
|
|
|
|
"github.com/containous/traefik/pkg/middlewares/recovery"
|
|
|
|
"github.com/containous/traefik/pkg/middlewares/tracing"
|
|
|
|
"github.com/containous/traefik/pkg/responsemodifiers"
|
|
|
|
"github.com/containous/traefik/pkg/rules"
|
|
|
|
"github.com/containous/traefik/pkg/server/internal"
|
|
|
|
"github.com/containous/traefik/pkg/server/middleware"
|
|
|
|
"github.com/containous/traefik/pkg/server/service"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
recoveryMiddlewareName = "traefik-internal-recovery"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewManager Creates a new Manager
|
|
|
|
func NewManager(routers map[string]*config.Router,
|
|
|
|
serviceManager *service.Manager, middlewaresBuilder *middleware.Builder, modifierBuilder *responsemodifiers.Builder,
|
|
|
|
) *Manager {
|
|
|
|
return &Manager{
|
|
|
|
routerHandlers: make(map[string]http.Handler),
|
|
|
|
configs: routers,
|
|
|
|
serviceManager: serviceManager,
|
|
|
|
middlewaresBuilder: middlewaresBuilder,
|
|
|
|
modifierBuilder: modifierBuilder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manager A route/router manager
|
|
|
|
type Manager struct {
|
|
|
|
routerHandlers map[string]http.Handler
|
|
|
|
configs map[string]*config.Router
|
|
|
|
serviceManager *service.Manager
|
|
|
|
middlewaresBuilder *middleware.Builder
|
|
|
|
modifierBuilder *responsemodifiers.Builder
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildHandlers Builds handler for all entry points
|
2019-03-14 08:30:04 +00:00
|
|
|
func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string, tls bool) map[string]http.Handler {
|
|
|
|
entryPointsRouters := m.filteredRouters(rootCtx, entryPoints, tls)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
entryPointHandlers := make(map[string]http.Handler)
|
|
|
|
for entryPointName, routers := range entryPointsRouters {
|
2019-01-30 15:24:07 +00:00
|
|
|
entryPointName := entryPointName
|
2018-11-14 09:18:03 +00:00
|
|
|
ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName))
|
|
|
|
|
|
|
|
handler, err := m.buildEntryPointHandler(ctx, routers)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-01-18 14:18:04 +00:00
|
|
|
|
|
|
|
handlerWithAccessLog, err := alice.New(func(next http.Handler) (http.Handler, error) {
|
|
|
|
return accesslog.NewFieldHandler(next, log.EntryPointName, entryPointName, accesslog.AddOriginFields), nil
|
|
|
|
}).Then(handler)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
entryPointHandlers[entryPointName] = handler
|
|
|
|
} else {
|
|
|
|
entryPointHandlers[entryPointName] = handlerWithAccessLog
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.serviceManager.LaunchHealthCheck()
|
|
|
|
|
|
|
|
return entryPointHandlers
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
func (m *Manager) filteredRouters(ctx context.Context, entryPoints []string, tls bool) map[string]map[string]*config.Router {
|
2018-11-14 09:18:03 +00:00
|
|
|
entryPointsRouters := make(map[string]map[string]*config.Router)
|
|
|
|
|
|
|
|
for rtName, rt := range m.configs {
|
2019-03-14 08:30:04 +00:00
|
|
|
if (tls && rt.TLS == nil) || (!tls && rt.TLS != nil) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
eps := rt.EntryPoints
|
|
|
|
if len(eps) == 0 {
|
2018-11-27 16:42:04 +00:00
|
|
|
eps = entryPoints
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
for _, entryPointName := range eps {
|
|
|
|
if !contains(entryPoints, entryPointName) {
|
|
|
|
log.FromContext(log.With(ctx, log.Str(log.EntryPointName, entryPointName))).
|
|
|
|
Errorf("entryPoint %q doesn't exist", entryPointName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := entryPointsRouters[entryPointName]; !ok {
|
|
|
|
entryPointsRouters[entryPointName] = make(map[string]*config.Router)
|
|
|
|
}
|
|
|
|
|
|
|
|
entryPointsRouters[entryPointName][rtName] = rt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entryPointsRouters
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string]*config.Router) (http.Handler, error) {
|
2019-01-30 15:24:07 +00:00
|
|
|
router, err := rules.NewRouter()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
for routerName, routerConfig := range configs {
|
2019-05-09 12:30:06 +00:00
|
|
|
ctxRouter := log.With(internal.AddProviderInContext(ctx, routerName), log.Str(log.RouterName, routerName))
|
2019-01-30 15:24:07 +00:00
|
|
|
logger := log.FromContext(ctxRouter)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
handler, err := m.buildRouterHandler(ctxRouter, routerName)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
err = router.AddRoute(routerConfig.Rule, routerConfig.Priority, handler)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
router.SortRoutes()
|
|
|
|
|
|
|
|
chain := alice.New()
|
|
|
|
chain = chain.Append(func(next http.Handler) (http.Handler, error) {
|
|
|
|
return recovery.New(ctx, next, recoveryMiddlewareName)
|
|
|
|
})
|
|
|
|
|
|
|
|
return chain.Then(router)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) buildRouterHandler(ctx context.Context, routerName string) (http.Handler, error) {
|
|
|
|
if handler, ok := m.routerHandlers[routerName]; ok {
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
configRouter, ok := m.configs[routerName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no configuration for %s", routerName)
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
handler, err := m.buildHTTPHandler(ctx, configRouter, routerName)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
handlerWithAccessLog, err := alice.New(func(next http.Handler) (http.Handler, error) {
|
|
|
|
return accesslog.NewFieldHandler(next, accesslog.RouterName, routerName, nil), nil
|
|
|
|
}).Then(handler)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
m.routerHandlers[routerName] = handler
|
|
|
|
} else {
|
|
|
|
m.routerHandlers[routerName] = handlerWithAccessLog
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.routerHandlers[routerName], nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
func (m *Manager) buildHTTPHandler(ctx context.Context, router *config.Router, routerName string) (http.Handler, error) {
|
2019-04-04 09:50:04 +00:00
|
|
|
qualifiedNames := make([]string, len(router.Middlewares))
|
|
|
|
for i, name := range router.Middlewares {
|
|
|
|
qualifiedNames[i] = internal.GetQualifiedName(ctx, name)
|
|
|
|
}
|
|
|
|
rm := m.modifierBuilder.Build(ctx, qualifiedNames)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
sHandler, err := m.serviceManager.BuildHTTP(ctx, router.Service, rm)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:28:04 +00:00
|
|
|
mHandler := m.middlewaresBuilder.BuildChain(ctx, router.Middlewares)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
tHandler := func(next http.Handler) (http.Handler, error) {
|
|
|
|
return tracing.NewForwarder(ctx, routerName, router.Service, next), nil
|
|
|
|
}
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
return alice.New().Extend(*mHandler).Append(tHandler).Then(sHandler)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-05-09 12:30:06 +00:00
|
|
|
|
|
|
|
func contains(entryPoints []string, entryPointName string) bool {
|
|
|
|
for _, name := range entryPoints {
|
|
|
|
if name == entryPointName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|