-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnano.go
170 lines (142 loc) · 5.02 KB
/
nano.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
// Copyright 2020 Vicky Hariadi Pratama. All rights reserved.
// license that can be found in the LICENSE file.
// this package is http route multiplexing.
package nano
import (
"errors"
"net/http"
"strings"
)
const (
// HeaderAcceptEncoding is accept encoding.
HeaderAcceptEncoding = "Accept-Encoding"
// HeaderContentEncoding is content encoding.
HeaderContentEncoding = "Content-Encoding"
// HeaderContentLength is content length.
HeaderContentLength = "Content-Length"
// HeaderContentType is content type.
HeaderContentType = "Content-Type"
// HeaderAccept is accept content type.
HeaderAccept = "Accept"
// HeaderOrigin is request origin.
HeaderOrigin = "Origin"
// HeaderVary is request vary.
HeaderVary = "Vary"
// HeaderAccessControlRequestMethod is cors request method.
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
// HeaderAccessControlRequestHeader is cors request header.
HeaderAccessControlRequestHeader = "Access-Control-Request-Header"
// HeaderAccessControlAllowOrigin is cors allowed origins.
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
// HeaderAccessControlAllowMethods is cors allowed origins.
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
// HeaderAccessControlAllowHeader is cors allowed headers.
HeaderAccessControlAllowHeader = "Access-Control-Allow-Header"
// MimeJSON is standard json mime.
MimeJSON = "application/json"
// MimeXML is standard json mime.
MimeXML = "application/xml"
// MimeHTML is standard html mime.
MimeHTML = "text/html"
// MimePlainText is standard plain text mime.
MimePlainText = "text/plain"
// MimeMultipartForm is standard multipart form mime.
MimeMultipartForm = "multipart/form-data"
// MimeFormURLEncoded is standard urlencoded form mime.
MimeFormURLEncoded = "application/x-www-form-urlencoded"
)
var (
// ErrDefaultHandler should be returned when user try to set default handler for seconds time.
ErrDefaultHandler = errors.New("default handler already registered")
)
// Engine defines nano web engine.
type Engine struct {
*RouterGroup
router *router
debug bool
groups []*RouterGroup
}
// RouterGroup defines collection of route that has same prefix
type RouterGroup struct {
prefix string
engine *Engine
middlewares []HandlerFunc
parent *RouterGroup
}
// H defines json wrapper.
type H map[string]interface{}
// HandlerFunc defines nano request handler function signature.
type HandlerFunc func(c *Context)
// New is nano constructor
func New() *Engine {
engine := &Engine{
router: newRouter(),
debug: false,
}
engine.RouterGroup = &RouterGroup{engine: engine}
engine.groups = []*RouterGroup{engine.RouterGroup}
return engine
}
// Use is functions to apply middleware function(s).
func (rg *RouterGroup) Use(middlewares ...HandlerFunc) {
rg.middlewares = append(rg.middlewares, middlewares...)
}
// Group is functions to create new router group.
func (rg *RouterGroup) Group(prefix string) *RouterGroup {
group := &RouterGroup{
prefix: rg.prefix + prefix,
parent: rg,
engine: rg.engine,
}
rg.engine.groups = append(rg.engine.groups, group)
return group
}
// GET is functions to register route with GET request method.
func (rg *RouterGroup) GET(urlPattern string, handler ...HandlerFunc) {
rg.addRoute(http.MethodGet, urlPattern, handler...)
}
// POST is functions to register route with POST request method.
func (rg *RouterGroup) POST(urlPattern string, handler HandlerFunc) {
rg.addRoute(http.MethodPost, urlPattern, handler)
}
// PUT is functions to register route with PUT request method.
func (rg *RouterGroup) PUT(urlPattern string, handler HandlerFunc) {
rg.addRoute(http.MethodPut, urlPattern, handler)
}
// DELETE is functions to register route with DELETE request method.
func (rg *RouterGroup) DELETE(urlPattern string, handler HandlerFunc) {
rg.addRoute(http.MethodDelete, urlPattern, handler)
}
// Default is functions to register default handler when no matching routes.
// Only one Default handler allowed to register.
func (rg *RouterGroup) Default(handler HandlerFunc) error {
// reject overriding.
if rg.engine.router.defaultHandler != nil {
return ErrDefaultHandler
}
rg.engine.router.defaultHandler = handler
return nil
}
// addRoute is functions to register new route with current group prefix.
func (rg *RouterGroup) addRoute(requestMethod, urlPattern string, handler ...HandlerFunc) {
// append router group prefix.
prefixedURLPattern := rg.prefix + urlPattern
rg.engine.router.addRoute(requestMethod, prefixedURLPattern, handler...)
}
// ServeHTTP implements multiplexer.
func (ng *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
middlewares := make([]HandlerFunc, 0)
// scanning for router group middleware.
for _, group := range ng.groups {
if strings.HasPrefix(r.URL.Path, group.prefix) {
middlewares = append(middlewares, group.middlewares...)
}
}
ctx := newContext(w, r)
ctx.handlers = middlewares
ng.router.handle(ctx)
}
// Run applications.
func (ng *Engine) Run(address string) error {
return http.ListenAndServe(address, ng)
}