/
service.go
472 lines (363 loc) · 10 KB
/
service.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package dcc
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
"gopkg.in/tylerb/graceful.v1"
"golang.org/x/net/context"
"golang.org/x/net/http2"
"github.com/Sirupsen/logrus"
"github.com/blang/semver"
"github.com/satori/go.uuid"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/labstack/echo.v1"
mw "gopkg.in/labstack/echo.v1/middleware"
)
// 422 Unprocessable Entity HTTP status code.
const StatusUnprocessableEntity = 422
var (
// Timeout when attempting to gracefully shut down a service.
ShutdownTimeout = 10 * time.Second
ErrNameRequired = errors.New("service: name required")
ErrVersionRequired = errors.New("service: version required")
ErrHostRequired = errors.New("service: host required")
ErrPortRequired = errors.New("service: port required")
ErrHandlerRequired = errors.New("service: handler required")
)
// Add prefix with service name
func newLogger(out io.Writer) *logrus.Logger {
return &logrus.Logger{
Out: out,
Level: logrus.InfoLevel,
Formatter: new(logrus.TextFormatter),
}
}
// optionsHandler returns an OPTIONS handler for a specific route.
func optionsHandler(methods ...string) echo.HandlerFunc {
return func(c *echo.Context) error {
resp := c.Response()
resp.Header().Set("Allow", strings.Join(methods, ","))
return c.NoContent(http.StatusOK)
}
}
// hasOptions returns true if the slice of methods contains the OPTIONS string.
func hasOptions(a []string) bool {
for _, m := range a {
if m == "OPTIONS" {
return true
}
}
return false
}
// bindOptions adds an OPTIONS handler for each route that does not already define one.
func bindOptions(mux *echo.Echo) {
routes := make(map[string][]string)
for _, r := range mux.Routes() {
routes[r.Path] = append(routes[r.Path], strings.ToUpper(r.Method))
}
for p, m := range routes {
if hasOptions(m) {
continue
}
mux.Options(p, optionsHandler(m...))
}
}
func newServeMux() *echo.Echo {
mux := echo.New()
// Apply some useful middleware.
mux.Use(mw.Logger())
mux.Use(mw.Recover())
mux.Use(mw.Gzip())
return mux
}
// Info defines the base service information that is exposed by
// the root endpoint.
type Info struct {
// Human-readonable name of the service.
Name string
// Version of the service.
Version string
// UUID of the service for tracing purposes.
UUID uuid.UUID
// Denotes whether the service is currently avaialble. When possible, this
// will be set in favor of a 503 Service Unavailable response, however it
// is semantically equivalent.
Available bool
// Time the service started.
Time time.Time
// Meta can store additional metadata baout the service.
Meta map[string]interface{}
}
// Uptime returns the uptime of the service as a duration.
func (i *Info) Uptime() time.Duration {
return time.Now().Sub(i.Time)
}
func (i *Info) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"name": i.Name,
"version": i.Version,
"available": i.Available,
"time": i.Time,
"uuid": i.UUID,
"uptime": i.Uptime(),
"meta": i.Meta,
})
}
// Handler is an interface that enables defining various kinds of
// service types.
type Handler interface {
// Init initializes the service handler.
Init(*Service) error
// Start starts the service handler. This method cannot block. Any blocking
// should be performed in a goroutine and managed by the handler.
Start(*Service) error
// Stop stops the service handler and performs internal cleanup.
Stop() error
}
// HandlerFunc wraps a single function for long-running service. The handler
// will be launched in a goroutine and therefore expected to block.
type HandlerFunc struct {
handle func(*Service) error
done chan error
}
func (h *HandlerFunc) Init(*Service) error {
h.done = make(chan error)
return nil
}
func (h *HandlerFunc) Start(s *Service) error {
go func() {
h.done <- h.handle(s)
}()
return nil
}
func (h *HandlerFunc) Stop() error {
return <-h.done
}
// Service defines the boilerplate components of a service.
type Service struct {
// Name of the service.
Name string
// Labels are used to denote the environment, type, role, etc. of
// the service.
Labels []string
// Version is the semantic version.
Version string
// Unique identifier of the service.
UUID uuid.UUID
// Host of the service. Defaults to 127.0.0.1.
Host string
// Port of the service. Defaults to 5000.
Port int
// If true, the service is considered in debug mode.
Debug bool
// Flags is a flagset for the command line arguments.
Flags *pflag.FlagSet
// Config is the service configuration loaded from a local or remote source.
Config *Config
// Handle is a bare handler function.
Handle func(*Service) error
// Handler is the service handler.
Handler Handler
// HTTP multiplexer.
Mux *echo.Echo
// Server is a custom HTTP server value.
Server *http.Server
// TLS certificate and key files. Alternatively, a TLSConfig
// can be defined on the Server value above.
TLSCert string
TLSKey string
HTTP2 bool
// Logger for the service.
Logger *logrus.Logger
initTime time.Time
startTime time.Time
stopTime time.Time
// CLI config
cli *viper.Viper
// Top-level context with cancel function.
cxt context.Context
cancel context.CancelFunc
// Denotes whether the service is considered available.
available bool
// Graceful server that wraps the primary one.
server *graceful.Server
}
// Validate validates the minimum requirements of the service.
func (s *Service) validate() error {
// Check required components.
if s.Name == "" {
return ErrNameRequired
}
if _, err := semver.Parse(s.Version); err != nil {
return err
}
if s.Host == "" {
return ErrHostRequired
}
if s.Port <= 0 {
return ErrPortRequired
}
if s.Handle == nil && s.Handler == nil {
return ErrHandlerRequired
}
return nil
}
// Initializes the internal state for a new service.
func (s *Service) init() {
if s.Logger == nil {
s.Logger = newLogger(os.Stderr)
}
if s.Handler == nil {
s.Handler = &HandlerFunc{handle: s.Handle}
}
// Initialize a UUID if not set.
if s.UUID == uuid.Nil {
s.UUID = uuid.NewV4()
}
// A pre-defined flagset may be supplied.
if s.Flags == nil {
flags := pflag.NewFlagSet(s.Name, pflag.ExitOnError)
// Initialize the base set of options. All other configuration should
// be specified and driven by the contents of the `config`.
flags.String("host", "127.0.0.1", "Host of the service.")
flags.Int("port", 5000, "Port of the service.")
flags.Bool("debug", false, "Toggle debug mode.")
flags.String("consul", "", "Host of Consul agent.")
flags.String("config", "", "Path to the service config.")
s.Flags = flags
}
// Bind the CLI arguments for the service.
cli := viper.New()
cli.SetDefault("host", "127.0.0.1")
cli.SetDefault("port", 5000)
s.cli = cli
cli.BindPFlag("host", s.Flags.Lookup("host"))
cli.BindPFlag("port", s.Flags.Lookup("port"))
cli.BindPFlag("debug", s.Flags.Lookup("debug"))
cli.BindPFlag("consul", s.Flags.Lookup("consul"))
cli.BindPFlag("config", s.Flags.Lookup("config"))
mux := newServeMux()
s.Mux = mux
// Bind the root endpoint with info about the service.
mux.Get("/", func(cxt *echo.Context) error {
return cxt.JSON(http.StatusOK, s.Info())
})
// Top-level context.
cxt, cancel := context.WithCancel(context.Background())
s.cxt = cxt
s.cancel = cancel
// Configuration.
s.Config = newConfig(cxt, s.Name)
}
// Info returns the info of the service.
func (s *Service) Info() *Info {
return &Info{
Name: s.Name,
Version: s.Version,
Available: s.available,
UUID: s.UUID,
Time: s.startTime,
}
}
// Context returns the context of the service.
func (s *Service) Context() context.Context {
return s.cxt
}
// Start initializes and starts the service in the background.
func (s *Service) Start(args []string) error {
s.init()
s.Logger.Debug("service: parsing command-line arguments")
if args == nil {
args = os.Args[1:]
}
s.Flags.Parse(args)
// Set debug.
if s.cli.IsSet("debug") {
s.Debug = true
}
// Setup various debugging hooks.
if s.Debug {
s.Logger.Info("service: debug mode enabled")
s.Mux.SetDebug(true)
s.Logger.Level = logrus.DebugLevel
}
s.Host = s.cli.GetString("host")
s.Port = s.cli.GetInt("port")
// Validate the service.
s.Logger.Debug("service: validating")
if err := s.validate(); err != nil {
return err
}
// Initialize the service handler.
s.Logger.Debug("service: initializing handler")
if err := s.Handler.Init(s); err != nil {
return err
}
// Bind all the options methods for the endpoints.
bindOptions(s.Mux)
// Start the service handler.
s.Logger.Debug("service: starting handler")
if err := s.Handler.Start(s); err != nil {
return err
}
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
if s.Server == nil {
s.Server = &http.Server{}
}
s.Server.Addr = addr
s.Server.Handler = s.Mux
// Enable HTTP 2 support.
if s.HTTP2 {
http2.ConfigureServer(s.Server, nil)
}
// Wrap in a graceful server.
s.server = &graceful.Server{
Server: s.Server,
Timeout: ShutdownTimeout,
}
// Start the HTTP server in the background.
s.Logger.Debug("service: starting HTTP server")
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
s.Logger.Infof("service: listening on %s...", addr)
wg.Done()
if err := s.server.ListenAndServe(); err != nil {
s.Logger.WithError(err).Error("service: error from listen and serve")
}
}()
wg.Wait()
return nil
}
// Wait will block until the service stops.
func (s *Service) Wait() {
<-s.server.StopChan()
}
// Stop stops the service and handler.
func (s *Service) Stop() error {
s.Logger.Info("service: stop requested")
s.Logger.Debug("service: canceling context")
s.cancel()
s.Logger.Debug("service: stopping handler")
err := s.Handler.Stop()
s.Logger.Debug("service: stopping server")
s.server.Stop(s.server.Timeout)
s.Wait()
s.Logger.Info("service: stopped")
return err
}
// Serve starts and waits for the server to stop.
func (s *Service) Serve() error {
if err := s.Start(nil); err != nil {
return err
}
s.Wait()
return nil
}