forked from caicloud/nirvana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nirvana.go
388 lines (346 loc) · 10.5 KB
/
nirvana.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright 2017 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nirvana
import (
"context"
"fmt"
"net/http"
"sync"
"sync/atomic"
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/errors"
"github.com/caicloud/nirvana/log"
"github.com/caicloud/nirvana/service"
)
// Server is a complete API server.
// The server contains a router to handle all requests form clients.
type Server interface {
// Serve starts to listen and serve requests.
// The method won't return except an error occurs.
Serve() error
// Shutdown gracefully shuts down the server without interrupting any
// active connections.
Shutdown(ctx context.Context) error
// Builder create a service builder for current server. Don't use this method directly except
// there is a special server to hold http services. After server shutdown, clean resources via
// returned cleaner.
// This method always returns same builder until cleaner is called. Then it will
// returns new one.
Builder() (builder service.Builder, cleaner func() error, err error)
}
// Config describes configuration of server.
type Config struct {
// tls cert file
certFile string
// tls ket file
keyFile string
// ip is the ip to listen. Empty means `0.0.0.0`.
ip string
// port is the port to listen.
port uint16
// logger is used to output info inside framework.
logger log.Logger
// descriptors contains all APIs.
descriptors []definition.Descriptor
// filters is http filters.
filters []service.Filter
// modifiers is definition modifiers
modifiers service.DefinitionModifiers
// configSet contains all configurations of plugins.
configSet map[string]interface{}
// locked is for locking current config. If the field
// is not 0, any modification causes panic.
locked int32
}
// lock locks config. If succeed, it will return ture.
func (c *Config) lock() bool {
return atomic.CompareAndSwapInt32(&c.locked, 0, 1)
}
// Locked checks if the config is locked.
func (c *Config) Locked() bool {
return atomic.LoadInt32(&c.locked) != 0
}
// IP returns listenning ip.
func (c *Config) IP() string {
return c.ip
}
// Port returns listenning port.
func (c *Config) Port() uint16 {
return c.port
}
// Logger returns logger.
func (c *Config) Logger() log.Logger {
return c.logger
}
// Configurer is used to configure server config.
type Configurer func(c *Config) error
var immutableConfig = errors.InternalServerError.Build("Nirvana:ImmutableConfig", "config has been locked and must not be modified")
// Configure configs by configurers. It panics if an error occurs or config is locked.
func (c *Config) Configure(configurers ...Configurer) *Config {
if c.Locked() {
panic(immutableConfig.Error())
}
for _, configurer := range configurers {
if err := configurer(c); err != nil {
panic(err)
}
}
return c
}
// Config gets external config by name. This method is for plugins.
func (c *Config) Config(name string) interface{} {
return c.configSet[name]
}
// Set sets external config by name. This method is for plugins.
// Set a nil config will delete it.
func (c *Config) Set(name string, config interface{}) {
if config == nil {
delete(c.configSet, name)
} else {
c.configSet[name] = config
}
}
// forEach traverse all plugin configs.
func (c *Config) forEach(f func(name string, config interface{}) error) error {
for name, cfg := range c.configSet {
if err := f(name, cfg); err != nil {
return err
}
}
return nil
}
// NewDefaultConfig creates default config.
// Default config contains:
// Filters: RedirectTrailingSlash, FillLeadingSlash, ParseRequestForm.
// Modifiers: FirstContextParameter,
// ConsumeAllIfConsumesIsEmpty, ProduceAllIfProducesIsEmpty,
// ConsumeNoneForHTTPGet, ConsumeNoneForHTTPDelete,
// ProduceNoneForHTTPDelete.
func NewDefaultConfig() *Config {
return NewConfig().Configure(
Logger(log.DefaultLogger()),
Filter(
service.RedirectTrailingSlash(),
service.FillLeadingSlash(),
service.ParseRequestForm(),
),
Modifier(
service.FirstContextParameter(),
service.ConsumeAllIfConsumesIsEmpty(),
service.ProduceAllIfProducesIsEmpty(),
service.ConsumeNoneForHTTPGet(),
service.ConsumeNoneForHTTPDelete(),
service.ProduceNoneForHTTPDelete(),
),
)
}
// NewConfig creates a pure config. If you don't know how to set filters and
// modifiers for specific scenario, please use NewDefaultConfig().
func NewConfig() *Config {
return &Config{
ip: "",
port: 8080,
logger: &log.SilentLogger{},
filters: []service.Filter{},
descriptors: []definition.Descriptor{},
modifiers: []service.DefinitionModifier{},
configSet: make(map[string]interface{}),
}
}
// server is nirvana server.
type server struct {
lock sync.Mutex
config *Config
server *http.Server
builder service.Builder
cleaner func() error
}
// NewServer creates a nirvana server. After creation, don't modify
// config. Also don't create another server with current config.
func NewServer(c *Config) Server {
if c.Locked() || !c.lock() {
panic(immutableConfig.Error())
}
return &server{
config: c,
}
}
var noConfigInstaller = errors.InternalServerError.Build("Nirvana:NoConfigInstaller", "no config installer for external config name ${name}")
// Builder create a service builder for current server. Don't use this method directly except
// there is a special server to hold http services. After server shutdown, clean resources via
// returned cleaner.
// This method always returns same builder until cleaner is called. Then it will
// returns new one.
func (s *server) Builder() (builder service.Builder, cleaner func() error, err error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.builder != nil {
return s.builder, s.cleaner, nil
}
builder = service.NewBuilder()
builder.SetLogger(s.config.logger)
builder.AddFilter(s.config.filters...)
builder.SetModifier(s.config.modifiers.Combine())
if err := builder.AddDescriptor(s.config.descriptors...); err != nil {
return nil, nil, err
}
if err := s.config.forEach(func(name string, config interface{}) error {
installer := ConfigInstallerFor(name)
if installer == nil {
return noConfigInstaller.Error(name)
}
return installer.Install(builder, s.config)
}); err != nil {
return nil, nil, err
}
s.builder = builder
s.cleaner = func() (err error) {
// Clean builder and plugins.
s.lock.Lock()
defer func() {
if err == nil {
s.builder = nil
s.cleaner = nil
}
s.lock.Unlock()
}()
if s.builder == nil {
return nil
}
return s.config.forEach(func(name string, config interface{}) error {
installer := ConfigInstallerFor(name)
if installer == nil {
return noConfigInstaller.Error(name)
}
return installer.Uninstall(builder, s.config)
})
}
return s.builder, s.cleaner, nil
}
var builderInUse = errors.InternalServerError.Build("Nirvana:BuilderInUse", "service builder is in use, clean it before serving")
// Serve starts to listen and serve requests.
// The method won't return except an error occurs.
func (s *server) Serve() (e error) {
s.lock.Lock()
if s.builder != nil || s.cleaner != nil {
return builderInUse.Error()
}
s.lock.Unlock()
builder, cleaner, err := s.Builder()
if err != nil {
return err
}
defer func() {
if err := cleaner(); err != nil {
s.config.logger.Error(err)
if e == nil {
e = err
}
}
}()
service, err := builder.Build()
if err != nil {
return err
}
s.server = &http.Server{
Addr: fmt.Sprintf("%s:%d", s.config.ip, s.config.port),
Handler: service,
}
if len(s.config.certFile) != 0 && len(s.config.keyFile) != 0 {
return s.server.ListenAndServeTLS(s.config.certFile, s.config.keyFile)
}
return s.server.ListenAndServe()
}
// Shutdown gracefully shuts down the server without interrupting any
// active connections.
func (s *server) Shutdown(ctx context.Context) error {
if s.server != nil {
return s.server.Shutdown(ctx)
}
return nil
}
// ConfigInstaller is used to install config to service builder.
type ConfigInstaller interface {
// Name is the external config name.
Name() string
// Install installs stuffs before server starting.
Install(builder service.Builder, config *Config) error
// Uninstall uninstalls stuffs after server terminating.
Uninstall(builder service.Builder, config *Config) error
}
var installers = map[string]ConfigInstaller{}
// ConfigInstallerFor gets installer by name.
func ConfigInstallerFor(name string) ConfigInstaller {
return installers[name]
}
// RegisterConfigInstaller registers a config installer.
func RegisterConfigInstaller(ci ConfigInstaller) {
if ConfigInstallerFor(ci.Name()) != nil {
panic(fmt.Sprintf("Config installer %s has been installed.", ci.Name()))
}
installers[ci.Name()] = ci
}
// IP returns a configurer to set ip into config.
func IP(ip string) Configurer {
return func(c *Config) error {
c.ip = ip
return nil
}
}
// TLS returns a configurer to set certFile and keyFile in config.
func TLS(certFile, keyFile string) Configurer {
return func(c *Config) error {
c.certFile = certFile
c.keyFile = keyFile
return nil
}
}
// Port returns a configurer to set port into config.
func Port(port uint16) Configurer {
return func(c *Config) error {
c.port = port
return nil
}
}
// Logger returns a configurer to set logger into config.
func Logger(logger log.Logger) Configurer {
return func(c *Config) error {
if logger == nil {
c.logger = &log.SilentLogger{}
} else {
c.logger = logger
}
return nil
}
}
// Descriptor returns a configurer to add descriptors into config.
func Descriptor(descriptors ...definition.Descriptor) Configurer {
return func(c *Config) error {
c.descriptors = append(c.descriptors, descriptors...)
return nil
}
}
// Filter returns a configurer to add filters into config.
func Filter(filters ...service.Filter) Configurer {
return func(c *Config) error {
c.filters = append(c.filters, filters...)
return nil
}
}
// Modifier returns a configurer to add definition modifiers into config.
func Modifier(modifiers ...service.DefinitionModifier) Configurer {
return func(c *Config) error {
c.modifiers = append(c.modifiers, modifiers...)
return nil
}
}