/
go-restful-helper.go
368 lines (329 loc) · 9.02 KB
/
go-restful-helper.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
package biu
import (
"context"
"net"
"net/http"
"net/http/httptest"
"os"
"os/signal"
"reflect"
"regexp"
"strings"
"syscall"
"testing"
"time"
_ "unsafe"
restful "github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
"github.com/gavv/httpexpect"
"github.com/go-openapi/spec"
"github.com/tuotoo/biu/box"
"github.com/tuotoo/biu/log"
"github.com/tuotoo/biu/opt"
)
const (
// MIME_HTML_FORM is application/x-www-form-urlencoded header
MIME_HTML_FORM = "application/x-www-form-urlencoded"
// MIME_FILE_FORM is multipart/form-data
MIME_FILE_FORM = "multipart/form-data"
)
var AutoGenPathDoc = false
type pathExpression struct {
LiteralCount int // the number of literal characters (means those not resulting from template variable substitution)
VarNames []string // the names of parameters (enclosed by {}) in the path
VarCount int // the number of named parameters (enclosed by {}) in the path
Matcher *regexp.Regexp
Source string // Path as defined by the RouteBuilder
tokens []string
}
//go:linkname newPathExpression github.com/emicklei/go-restful.newPathExpression
func newPathExpression(path string) (*pathExpression, error)
//go:linkname nameOfFunction github.com/emicklei/go-restful.nameOfFunction
func nameOfFunction(f interface{}) string
var swaggerTags = make(map[*http.ServeMux][]spec.Tag)
// Container of restful
type Container struct{ *restful.Container }
// New creates a new restful container.
func New() Container {
return Container{Container: restful.NewContainer()}
}
// AddServices adds services with namespace for container.
func (c *Container) AddServices(prefix string, opts opt.ServicesFuncArr, wss ...NS) {
addService(prefix, opts, c.Container, wss...)
}
// AddServices adds services with namespace.
func AddServices(prefix string, opts opt.ServicesFuncArr, wss ...NS) {
addService(prefix, opts, restful.DefaultContainer, wss...)
}
// Run starts up a web server for container.
func (c *Container) Run(addr string, opts ...opt.RunFunc) {
run(addr, c.Container, opts...)
}
// Run starts up a web server with default container.
func Run(addr string, opts ...opt.RunFunc) {
run(addr, nil, opts...)
}
// Route creates a new Route using the RouteBuilder
// and add to the ordered list of Routes.
func (ws WS) Route(builder *restful.RouteBuilder, opts ...opt.RouteFunc) {
cfg := &opt.Route{
EnableAutoPathDoc: true,
To: func(ctx box.Ctx) {},
}
for _, f := range opts {
if f != nil {
f(cfg)
}
}
builder = builder.To(Handle(cfg.To))
if cfg.ID != "" {
builder = builder.Operation(cfg.ID)
} else {
builder = builder.Operation(nameOfFunction(cfg.To))
}
elm := reflect.ValueOf(builder).Elem()
p1 := elm.FieldByName("rootPath").String()
p2 := elm.FieldByName("currentPath").String()
path := strings.TrimRight(p1, "/") + "/"
if ws.namespace != "" {
path += ws.namespace + "/"
builder.Path(ws.namespace + p2)
}
path += strings.TrimLeft(p2, "/")
method := elm.FieldByName("httpMethod").String()
mapKey := path + " " + method
if AutoGenPathDoc && cfg.EnableAutoPathDoc {
exp, err := newPathExpression(p2)
if err != nil {
log.Fatal().Err(err).Str("path", p2).Msg("invalid path")
}
for i, v := range exp.VarNames {
desc := v
if len(cfg.ExtraPathDocs) > i {
desc = cfg.ExtraPathDocs[i]
}
builder = builder.Param(ws.PathParameter(v, desc))
}
}
if cfg.ID != "" {
box.RouteIDMap[mapKey] = cfg.ID
}
if _, ok := box.RouteErrMap[mapKey]; !ok {
box.RouteErrMap[mapKey] = make(map[int]string)
}
for k, v := range cfg.Errors {
box.RouteErrMap[mapKey][k] = v
builder = builder.Returns(k, v, nil)
}
if cfg.Auth {
builder = builder.Metadata("jwt", true)
}
ws.WebService.Route(builder)
}
func addService(
prefix string,
opts opt.ServicesFuncArr,
container *restful.Container,
wss ...NS,
) {
expr, err := newPathExpression(prefix)
if err != nil {
panic(err)
}
var inCommonNS bool
if expr.VarCount > 0 {
inCommonNS = true
}
cfg := &opt.Services{}
for _, f := range opts {
f(cfg)
}
for k, v := range cfg.Errors {
box.GlobalErrMap[k] = v
}
commonWS := WS{WebService: new(restful.WebService)}
commonWS.Path(prefix).Produces(restful.MIME_JSON)
var filterAdded bool
for _, v := range wss {
// build web service
ws := WS{WebService: new(restful.WebService)}
path := prefix + "/" + v.NameSpace
ws.Path(path).Produces(restful.MIME_JSON)
if inCommonNS {
ws = commonWS
ws.namespace = v.NameSpace
}
if (inCommonNS && !filterAdded) || !inCommonNS {
for _, f := range cfg.Filters {
ws.Filter(f)
}
}
v.Controller.WebService(ws)
if !inCommonNS {
container.Add(ws.WebService)
}
// add swagger tags to routes of webservice
tagProps := spec.TagProps{
Name: v.NameSpace,
Description: v.Desc,
}
if v.ExternalDesc != "" {
tagProps.ExternalDocs = &spec.ExternalDocumentation{
Description: v.ExternalDesc,
URL: v.ExternalURL,
}
}
swaggerTags[container.ServeMux] = append(swaggerTags[container.ServeMux], spec.Tag{
TagProps: tagProps,
})
routes := ws.Routes()
for ri, r := range routes {
if routes[ri].Metadata == nil {
routes[ri].Metadata = make(map[string]interface{})
}
if len(routes[ri].Consumes) == 0 {
if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
r.Consumes = []string{MIME_HTML_FORM}
} else {
r.Consumes = []string{restful.MIME_JSON}
}
}
if strings.HasPrefix(r.Path, path) {
log.Info().Str("path", r.Path).
Str("method", r.Method).
Msg("routers")
routes[ri].Metadata[restfulspec.KeyOpenAPITags] = []string{v.NameSpace}
}
}
}
if inCommonNS {
container.Add(commonWS.WebService)
}
}
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
// ListenAndServe listens on the TCP network address srv.Addr and then
// calls Serve to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
// If srv.Addr is blank, ":http" is used.
// ListenAndServe always returns a non-nil error.
func ListenAndServe(srv *http.Server, addrChan chan<- string) error {
addr := srv.Addr
if addr == "" {
addr = ":0"
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
tcpListener := ln.(*net.TCPListener)
{
addr := tcpListener.Addr()
addrChan <- addr.String()
}
return srv.Serve(tcpKeepAliveListener{TCPListener: tcpListener})
}
func run(addr string, handler http.Handler, opts ...opt.RunFunc) {
nCtx, nCancel := context.WithCancel(context.Background())
cfg := &opt.Run{
BeforeShutDown: func() {},
AfterShutDown: func() {},
Ctx: nCtx,
Cancel: nCancel,
}
for _, f := range opts {
if f != nil {
f(cfg)
}
}
server := &http.Server{
Addr: addr,
Handler: handler,
}
addrChan := make(chan string)
go func() {
log.Error().Err(ListenAndServe(server, addrChan)).Msg("listening")
if cfg.Cancel != nil {
cfg.Cancel()
}
}()
select {
case addr := <-addrChan:
log.Info().Str("addr", addr).Msg("listening")
case <-time.After(time.Second):
log.Fatal().Msg("something went wrong when starting the server")
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Info().Interface("ch", <-ch).Msg("signal receive")
cfg.BeforeShutDown()
log.Info().Err(server.Shutdown(cfg.Ctx)).Msg("shutting down")
select {
case <-cfg.Ctx.Done():
log.Info().Msg("biu~biu~biu~")
}
cfg.AfterShutDown()
log.Info().Msg("server shuts down gracefully")
}
// TestServer wraps a httptest.Server
type TestServer struct {
*httptest.Server
}
// NewTestServer returns a Test Server.
func (c *Container) NewTestServer() *TestServer {
return &TestServer{
Server: httptest.NewServer(c),
}
}
// NewTestServer returns a Test Server.
func NewTestServer() *TestServer {
return &TestServer{
Server: httptest.NewServer(restful.DefaultContainer),
}
}
// WithT accept testing.T and returns httpexpect.Expect
func (s *TestServer) WithT(t *testing.T) *httpexpect.Expect {
return httpexpect.New(t, s.URL)
}
// LogFilter logs
// {
// remote_addr,
// method,
// uri,
// proto,
// status_code,
// content_length,
// }
// for each request
func LogFilter() restful.FilterFunction {
return func(
req *restful.Request,
resp *restful.Response,
chain *restful.FilterChain,
) {
start := time.Now()
chain.ProcessFilter(req, resp)
log.Info().
Str("remote_addr", strings.Split(req.Request.RemoteAddr, ":")[0]).
Str("method", req.Request.Method).
Str("uri", req.Request.URL.RequestURI()).
Str("proto", req.Request.Proto).
Int("status_code", resp.StatusCode()).
Dur("dur", time.Since(start)).
Int("content_length", resp.ContentLength()).Msg("req")
}
}