/
router.go
180 lines (149 loc) · 4.65 KB
/
router.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
package bgo
import (
"context"
"fmt"
"net"
"net/http"
httprouter "github.com/julienschmidt/httprouter"
bc "github.com/pickjunk/bgo/config"
cors "github.com/rs/cors"
)
// Router thin wrapper for httprouter.Router
type Router struct {
prefix string
middlewares []Middleware
cors *cors.Cors
*httprouter.Router
}
// New create a bgo Router
func New() *Router {
return &Router{
Router: httprouter.New(),
middlewares: []Middleware{
logMiddleware,
recoverMiddleware,
},
}
}
// ListenAndServe is a shortcut for http.ListenAndServe
func (r *Router) ListenAndServe() {
port := 8080
if bc.Get("port").Exists() {
port = int(bc.Get("port").Int())
}
log.Info().Int("port", port).Msg("http.ListenAndServe")
err := http.ListenAndServe(":"+fmt.Sprintf("%d", port), r)
if err != nil {
log.Panic().Err(err).Send()
}
}
// Serve is a shortcut for http.Serve
func (r *Router) Serve(l net.Listener) {
log.Panic().Err(http.Serve(l, r)).Send()
}
// Prefix append prefix
func (r *Router) Prefix(p string) *Router {
new := *r
new.prefix = new.prefix + p
return &new
}
// Middlewares register middlewares
func (r *Router) Middlewares(layers ...Middleware) *Router {
new := *r
new.middlewares = append(r.middlewares, layers...)
return &new
}
// CORS register middlewares
func (r *Router) CORS(c *cors.Cors) *Router {
new := *r
new.cors = c
return &new
}
// Handle func
type Handle = func(context.Context)
// Middleware func
type Middleware = func(context.Context, Handle)
// Handle define a route
func (r *Router) Handle(method, path string, middlewaresAndHandle ...interface{}) *Router {
l := len(middlewaresAndHandle)
if l == 0 {
log.Panic().Msg("expect bgo.Handle")
}
var handle Handle
switch h := middlewaresAndHandle[l-1].(type) {
case Handle:
handle = h
case http.Handler:
handle = func(ctx context.Context) {
h.ServeHTTP(Response(ctx), Request(ctx))
}
default:
log.Panic().Msgf("expect bgo.Handle or http.Handler, but get %T", middlewaresAndHandle[l-1])
}
middlewares := r.middlewares
for i := 0; i < l-1; i++ {
middleware, ok := middlewaresAndHandle[i].(Middleware)
if !ok {
log.Panic().Msgf("expect bgo.Middleware, but get %T", middlewaresAndHandle[i])
}
middlewares = append(middlewares, middleware)
}
// handle wrapped with middlewares
for i := len(middlewares) - 1; i >= 0; i-- {
middleware := middlewares[i]
next := handle
handle = func(ctx context.Context) {
middleware(ctx, next)
}
}
// wrap it as httprouter.Handle
// attach response, request, params to context
hrHandle := func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := withValue(r.Context(), "http", &HTTP{w, r, ps})
handle(ctx)
}
hr := r.Router
// compatible with rs/cors
if r.cors != nil {
if method != "OPTIONS" {
hr.Handle("OPTIONS", r.prefix+path, func(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
r.cors.HandlerFunc(w, req)
})
}
innerHandle := hrHandle
hrHandle = func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
r.cors.HandlerFunc(w, req)
innerHandle(w, req, ps)
}
}
hr.Handle(method, r.prefix+path, hrHandle)
return r
}
// GET is a shortcut for router.Handle("GET", path, middlewaresAndHandle...)
func (r *Router) GET(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("GET", path, middlewaresAndHandle...)
}
// HEAD is a shortcut for router.Handle("HEAD", path, middlewaresAndHandle...)
func (r *Router) HEAD(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("HEAD", path, middlewaresAndHandle...)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, middlewaresAndHandle...)
func (r *Router) OPTIONS(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("OPTIONS", path, middlewaresAndHandle...)
}
// POST is a shortcut for router.Handle("POST", path, middlewaresAndHandle...)
func (r *Router) POST(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("POST", path, middlewaresAndHandle...)
}
// PUT is a shortcut for router.Handle("PUT", path, middlewaresAndHandle...)
func (r *Router) PUT(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("PUT", path, middlewaresAndHandle...)
}
// PATCH is a shortcut for router.Handle("PATCH", path, middlewaresAndHandle...)
func (r *Router) PATCH(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("PATCH", path, middlewaresAndHandle...)
}
// DELETE is a shortcut for router.Handle("DELETE", path, middlewaresAndHandle...)
func (r *Router) DELETE(path string, middlewaresAndHandle ...interface{}) *Router {
return r.Handle("DELETE", path, middlewaresAndHandle...)
}