-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
managerfactory.go
74 lines (58 loc) · 2.44 KB
/
managerfactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package service
import (
"net/http"
"github.com/traefik/traefik/v2/pkg/api"
"github.com/traefik/traefik/v2/pkg/config/runtime"
"github.com/traefik/traefik/v2/pkg/config/static"
"github.com/traefik/traefik/v2/pkg/metrics"
"github.com/traefik/traefik/v2/pkg/safe"
)
// ManagerFactory a factory of service manager.
type ManagerFactory struct {
metricsRegistry metrics.Registry
roundTripperManager *RoundTripperManager
api func(configuration *runtime.Configuration) http.Handler
restHandler http.Handler
dashboardHandler http.Handler
metricsHandler http.Handler
pingHandler http.Handler
acmeHTTPHandler http.Handler
routinesPool *safe.Pool
}
// NewManagerFactory creates a new ManagerFactory.
func NewManagerFactory(staticConfiguration static.Configuration, routinesPool *safe.Pool, metricsRegistry metrics.Registry, roundTripperManager *RoundTripperManager, acmeHTTPHandler http.Handler) *ManagerFactory {
factory := &ManagerFactory{
metricsRegistry: metricsRegistry,
routinesPool: routinesPool,
roundTripperManager: roundTripperManager,
acmeHTTPHandler: acmeHTTPHandler,
}
if staticConfiguration.API != nil {
factory.api = api.NewBuilder(staticConfiguration)
if staticConfiguration.API.Dashboard {
factory.dashboardHandler = api.DashboardHandler{Assets: staticConfiguration.API.DashboardAssets}
}
}
if staticConfiguration.Providers != nil && staticConfiguration.Providers.Rest != nil {
factory.restHandler = staticConfiguration.Providers.Rest.CreateRouter()
}
if staticConfiguration.Metrics != nil && staticConfiguration.Metrics.Prometheus != nil {
factory.metricsHandler = metrics.PrometheusHandler()
}
// This check is necessary because even when staticConfiguration.Ping == nil ,
// the affectation would make factory.pingHandle become a typed nil, which does not pass the nil test,
// and would break things elsewhere.
if staticConfiguration.Ping != nil {
factory.pingHandler = staticConfiguration.Ping
}
return factory
}
// Build creates a service manager.
func (f *ManagerFactory) Build(configuration *runtime.Configuration) *InternalHandlers {
svcManager := NewManager(configuration.Services, f.metricsRegistry, f.routinesPool, f.roundTripperManager)
var apiHandler http.Handler
if f.api != nil {
apiHandler = f.api(configuration)
}
return NewInternalHandlers(svcManager, apiHandler, f.restHandler, f.metricsHandler, f.pingHandler, f.dashboardHandler, f.acmeHTTPHandler)
}