-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
153 lines (125 loc) · 3.9 KB
/
server.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package core
// MARKED TO IGNORE COVERAGE
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
type ServerParams struct {
Ctx context.Context
Name string
logger *logrus.Logger
restManager Manager
grpcManager GrpcManager
prometheusRegistry *prometheus.Registry
}
//nolint:gocognit // Long method names are okay because only generated code will call this, not humans.
func NewServerParams(ctx context.Context, name string, opts ...ServerOption) *ServerParams {
params := &ServerParams{Ctx: ctx, Name: name}
for _, o := range opts {
o.apply(params)
}
return params
}
//nolint:gocognit // Long method are okay because only generated code will call this, not humans.
func (params *ServerParams) Start() error {
mWare := prepareMiddleware(params.Name, params.logger, params.prometheusRegistry)
var restIsRunning, grpcIsRunning bool
// Run the REST server
var listenAdmin func() error
if params.restManager != nil && params.restManager.AdminServerConfig() != nil {
var err error
listenAdmin, err = configureAdminServerListener(params.restManager, params.logger, params.prometheusRegistry, mWare.admin)
if err != nil {
return err
}
} else {
// set up a dummy listener which will never exit if admin disabled
listenAdmin = func() error { select {} }
}
var listenPublic func() error
if params.restManager != nil && params.restManager.PublicServerConfig() != nil {
var err error
listenPublic, err = configurePublicServerListener(params.Ctx, params.restManager, params.logger, mWare.public)
if err != nil {
return err
}
restIsRunning = true
} else {
listenPublic = func() error { select {} }
}
// Run the gRPC server
var listenPublicGrpc func() error
if params.grpcManager != nil && params.grpcManager.GrpcPublicServerConfig() != nil {
var err error
listenPublicGrpc, err = configurePublicGrpcServerListener(params.Ctx, params.grpcManager, params.logger)
if err != nil {
return err
}
grpcIsRunning = true
} else {
listenPublicGrpc = func() error { select {} }
}
// Panic if REST&gRPC are not running
if !restIsRunning && !grpcIsRunning {
panic("Both servers are set to nil")
}
errChan := make(chan error, 1)
go func() {
errChan <- listenPublic()
}()
go func() {
errChan <- listenAdmin()
}()
go func() {
errChan <- listenPublicGrpc()
}()
return <-errChan
}
type ServerOption interface {
apply(params *ServerParams)
}
type restManagerOption struct {
restManager Manager
}
func (o *restManagerOption) apply(params *ServerParams) {
params.restManager = o.restManager
}
func WithRestManager(manager Manager) ServerOption {
return &restManagerOption{manager}
}
type loggerOption struct {
logger *logrus.Logger
}
func (o *loggerOption) apply(params *ServerParams) {
params.logger = o.logger
}
func WithLogrusLogger(logger *logrus.Logger) ServerOption {
return &loggerOption{logger}
}
func WithPrometheusRegistry(prometheusRegistry *prometheus.Registry) ServerOption {
return &prometheusRegistryOption{prometheusRegistry}
}
type prometheusRegistryOption struct {
prometheusRegistry *prometheus.Registry
}
func (o *prometheusRegistryOption) apply(params *ServerParams) {
params.prometheusRegistry = o.prometheusRegistry
}
type grpcManagerOption struct {
grpcManager GrpcManager
}
func (o *grpcManagerOption) apply(params *ServerParams) {
params.grpcManager = o.grpcManager
}
func WithGrpcManager(manager GrpcManager) ServerOption {
return &grpcManagerOption{manager}
}
//nolint:gocognit // Long method names are okay because only generated code will call this, not humans.
func Server(ctx context.Context, name string, hl Manager, grpcHl GrpcManager, logger *logrus.Logger, promRegistry *prometheus.Registry) error {
return NewServerParams(ctx, name,
WithLogrusLogger(logger),
WithRestManager(hl),
WithGrpcManager(grpcHl),
WithPrometheusRegistry(promRegistry)).Start()
}