-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.go
156 lines (130 loc) · 3.99 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
154
155
156
package gimlet
import (
"context"
"crypto/tls"
"net"
"net/http"
"time"
"github.com/mongodb/grip"
"github.com/mongodb/grip/recovery"
"github.com/pkg/errors"
)
// Server provides a wrapper around *http.Server methods, with an
// Run method which provides a clean-shutdown compatible listen operation.
type Server interface {
Close() error
ListenAndServe() error
ListenAndServeTLS(string, string) error
Serve(net.Listener) error
Shutdown(context.Context) error
// Run provides a simple wrapper around default http.Server
// functionality to allow clean shutdown that uses a context
// with a wait function
Run(context.Context) (WaitFunc, error)
// GetServer allows you to access the underlying http server.
GetServer() *http.Server
}
// TODO add ServeTLS when we can move up to 1.9+:
//
// ServeTLS(net.Listener, string, string) error
type ServerConfig struct {
Timeout time.Duration
Handler http.Handler
App *APIApp
TLS *tls.Config
Address string
Info string
handlerGenerated bool
}
// Validate returns an error if there are any problems with a
// ServerConfig that would make it impossible to render a server from
// the configuration.
func (c *ServerConfig) Validate() error {
catcher := grip.NewBasicCatcher()
catcher.NewWhen(c.TLS != nil && c.TLS.Certificates == nil, "TLS config specified without certificates")
catcher.NewWhen(c.Handler == nil && c.App == nil, "must specify a handler or an app")
catcher.NewWhen(c.Handler != nil && c.App != nil && !c.handlerGenerated, "can only specify a handler or an app")
catcher.NewWhen(c.Address == "", "must specify an address")
catcher.NewWhen(c.Timeout < time.Second, "must specify timeout greater than or equal to a second")
catcher.NewWhen(c.Timeout > 10*time.Minute, "must specify timeout less than 10 minutes")
_, _, err := net.SplitHostPort(c.Address)
catcher.Wrap(err, "splitting host and port in address")
if c.App != nil {
c.Handler, err = c.App.Handler()
catcher.Add(err)
c.handlerGenerated = true
}
return catcher.Resolve()
}
func (c *ServerConfig) build() Server {
return server{&http.Server{
Addr: c.Address,
Handler: c.Handler,
ReadTimeout: c.Timeout,
ReadHeaderTimeout: c.Timeout / 2,
WriteTimeout: c.Timeout,
TLSConfig: c.TLS,
}}
}
// Resolve validates a config and constructs a server from the
// configuration if possible.
func (c *ServerConfig) Resolve() (Server, error) {
if err := c.Validate(); err != nil {
return nil, errors.WithStack(err)
}
return c.build(), nil
}
// NewServer constructs a server based on a handler and an
// address. Uses a default 1 minute timeout.
func NewServer(addr string, n http.Handler) (Server, error) {
conf := ServerConfig{
Timeout: time.Minute,
Handler: n,
Address: addr,
}
return conf.Resolve()
}
// BuildNewServer constructs a new server that uses TLS, returning an
// error if you pass a nil TLS configuration.
func BuildNewServer(addr string, n http.Handler, tlsConf *tls.Config) (Server, error) {
if tlsConf == nil {
return nil, errors.New("must specify a non-nil TLS config")
}
conf := ServerConfig{
Address: addr,
Handler: n,
TLS: tlsConf,
Timeout: time.Minute,
}
return conf.Resolve()
}
type server struct {
*http.Server
}
func (s server) GetServer() *http.Server { return s.Server }
func (s server) Run(ctx context.Context) (WaitFunc, error) {
serviceWait := make(chan struct{})
go func() {
defer recovery.LogStackTraceAndContinue("app service")
if s.Server.TLSConfig != nil {
err := s.ListenAndServeTLS("", "")
grip.ErrorWhen(err != http.ErrServerClosed, errors.Wrap(err, "starting TLS service"))
} else {
err := s.ListenAndServe()
grip.ErrorWhen(err != http.ErrServerClosed, errors.Wrap(err, "starting service"))
}
close(serviceWait)
}()
go func() {
defer recovery.LogStackTraceAndContinue("server shutdown")
<-ctx.Done()
grip.Debug(s.Shutdown(ctx))
}()
wait := func(wctx context.Context) {
select {
case <-wctx.Done():
case <-serviceWait:
}
}
return wait, nil
}