-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
141 lines (120 loc) · 2.73 KB
/
server.go
File metadata and controls
141 lines (120 loc) · 2.73 KB
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
package httpx
import (
"context"
"crypto/tls"
"net"
"net/http"
"time"
)
// Server for HTTP protocol.
type Server struct {
srv *http.Server
cfg *ServerConfig
}
// ServerConfig configures Server.
type ServerConfig struct {
Addr string
Handler http.Handler
NoHTTP2 bool
TLSConfig *tls.Config
CertFile string
KeyFile string
// TODO: enable option
HeartbeatPath string
HeartbeatHandler http.HandlerFunc
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
}
// Validate the config.
func (c *ServerConfig) Validate() error {
if c.ReadTimeout == 0 {
c.ReadTimeout = 30 * time.Second
}
if c.ReadHeaderTimeout == 0 {
c.ReadHeaderTimeout = 5 * time.Second
}
if c.WriteTimeout == 0 {
c.WriteTimeout = 15 * time.Second
}
if c.IdleTimeout == 0 {
c.IdleTimeout = 30 * time.Second
}
if c.MaxHeaderBytes == 0 {
c.MaxHeaderBytes = 8 * 1024
}
return nil
}
// NewServer returns a new Server.
func NewServer(config *ServerConfig) (*Server, error) {
if err := config.Validate(); err != nil {
return nil, err
}
s := &Server{
srv: &http.Server{
Addr: config.Addr,
Handler: config.Handler,
ReadTimeout: config.ReadTimeout,
ReadHeaderTimeout: config.ReadHeaderTimeout,
WriteTimeout: config.WriteTimeout,
IdleTimeout: config.IdleTimeout,
MaxHeaderBytes: config.MaxHeaderBytes,
TLSConfig: config.TLSConfig,
},
cfg: config,
}
if config.NoHTTP2 {
s.srv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
}
return s, nil
}
// Start the server with a given handler.
// Same as Run but allows to set handler later.
func (s *Server) Start(ctx context.Context, h http.Handler) error {
s.srv.Handler = h
return s.Run(ctx)
}
// Run starts the server.
func (s *Server) Run(ctx context.Context) error {
if s.srv.Handler == nil {
panic("handler is nil")
}
s.srv.BaseContext = func(net.Listener) context.Context {
return ctx
}
if s.cfg.TLSConfig == nil {
return s.run(ctx)
}
return s.runTLS(ctx)
}
func (s *Server) run(ctx context.Context) error {
errCh := make(chan error)
go func() {
errCh <- s.srv.ListenAndServe()
}()
select {
case <-ctx.Done():
return s.shutdown()
case err := <-errCh:
return err
}
}
func (s *Server) runTLS(ctx context.Context) error {
errCh := make(chan error)
go func() {
errCh <- s.srv.ListenAndServeTLS(s.cfg.CertFile, s.cfg.KeyFile)
}()
select {
case <-ctx.Done():
return s.shutdown()
case err := <-errCh:
return err
}
}
func (s *Server) shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
return s.srv.Shutdown(ctx)
}