forked from dolab/gogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
198 lines (161 loc) · 4.81 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
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
package gogo
import (
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/golib/httprouter"
)
type AppServer struct {
*AppRoute
mode RunMode
config *AppConfig
router *httprouter.Router
pool sync.Pool
throttle *time.Ticker // time.Ticker for rate limit
slowdown time.Duration // cache for performance
logger Logger
requestId string // request id header name
filterParams []string // filter out params when logging
}
func NewAppServer(mode RunMode, config *AppConfig, logger Logger) *AppServer {
server := &AppServer{
mode: mode,
config: config,
logger: logger,
requestId: DefaultHttpRequestId,
}
// init Route
server.AppRoute = NewAppRoute("/", server)
// init router
server.router = httprouter.New()
server.router.RedirectTrailingSlash = false
server.router.HandleMethodNotAllowed = false // strict for RESTful
// overwrite
server.pool.New = func() interface{} {
return NewContext(server)
}
return server
}
// Mode returns run mode of the app server
func (s *AppServer) Mode() string {
return string(s.mode)
}
// Config returns app config of the app server
func (s *AppServer) Config() *AppConfig {
return s.config
}
// Run runs the http server
func (s *AppServer) Run() {
var (
config = s.config.Section()
network = "tcp"
addr = config.Server.Addr
port = config.Server.Port
rtimeout = DefaultHttpRequestTimeout
wtimeout = DefaultHttpResponseTimeout
maxheaderbytes = 0
localAddr string
)
// throttle of rate limit
if config.Server.Throttle > 0 {
s.throttle = time.NewTicker(time.Second / time.Duration(config.Server.Throttle))
}
// adjust app server slowdown ms
s.slowdown = time.Duration(config.Server.SlowdownMs) * time.Millisecond
// adjust app server request id
if config.Server.RequestId != "" {
s.requestId = config.Server.RequestId
}
// adjust app logger filter parameters
s.filterParams = config.Logger.FilterParams
// If the port is zero, treat the address as a fully qualified local address.
// This address must be prefixed with the network type followed by a colon,
// e.g. unix:/tmp/app.socket or tcp6:::1 (equivalent to tcp6:0:0:0:0:0:0:0:1)
if port == 0 {
pieces := strings.SplitN(addr, ":", 2)
network = pieces[0]
localAddr = pieces[1]
} else {
localAddr = addr + ":" + strconv.Itoa(port)
}
if config.Server.RTimeout > 0 {
rtimeout = config.Server.RTimeout
}
if config.Server.WTimeout > 0 {
wtimeout = config.Server.WTimeout
}
if config.Server.MaxHeaderBytes > 0 {
maxheaderbytes = config.Server.MaxHeaderBytes
}
server := &http.Server{
Addr: localAddr,
Handler: s,
ReadTimeout: time.Duration(rtimeout) * time.Second,
WriteTimeout: time.Duration(wtimeout) * time.Second,
MaxHeaderBytes: maxheaderbytes,
}
s.logger.Infof("Listening on %s", localAddr)
if config.Server.Ssl {
if network != "tcp" {
// This limitation is just to reduce complexity, since it is standard
// to terminate SSL upstream when using unix domain sockets.
s.logger.Fatal("=> SSL is only supported for TCP sockets.")
}
s.logger.Fatal("=> Failed to listen:", server.ListenAndServeTLS(config.Server.SslCert, config.Server.SslKey))
} else {
listener, err := net.Listen(network, localAddr)
if err != nil {
s.logger.Fatal("=> Failed to listen:", err)
}
s.logger.Fatal("=> Failed to serve:", server.Serve(listener))
}
}
// Use applies middlewares to app route
// NOTE: It dispatch to Route.Use by overwrite
func (s *AppServer) Use(middlewares ...Middleware) {
s.AppRoute.Use(middlewares...)
}
// Clean removes all registered middlewares, it useful in testing cases.
func (s *AppServer) Clean() {
s.Handlers = []Middleware{}
}
// ServeHTTP implements the http.Handler interface
func (s *AppServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logger.Debugf(`processing %s "%s"`, r.Method, s.filterParameters(r.URL))
// rate limit
if s.throttle != nil {
<-s.throttle.C
}
s.router.ServeHTTP(w, r)
}
// new returns a new context for the server
func (s *AppServer) new(w http.ResponseWriter, r *http.Request, params *AppParams, handlers []Middleware) *Context {
// adjust request id
requestId := r.Header.Get(s.requestId)
if requestId == "" {
requestId = NewObjectId().Hex()
// inject request header with new request id
r.Header.Set(s.requestId, requestId)
}
w.Header().Set(s.requestId, requestId)
ctx := s.pool.Get().(*Context)
ctx.Request = r
ctx.Response = &ctx.writer
ctx.Params = params
ctx.Logger = s.logger.New(requestId)
ctx.settings = nil
ctx.frozenSettings = nil
ctx.writer.reset(w)
ctx.handlers = handlers
ctx.index = -1
ctx.startedAt = time.Now()
ctx.downAfter = ctx.startedAt.Add(s.slowdown)
return ctx
}
// reuse puts the context back to pool for later usage
func (s *AppServer) reuse(ctx *Context) {
s.pool.Put(ctx)
}