-
Notifications
You must be signed in to change notification settings - Fork 16
/
rux.go
186 lines (157 loc) · 4.22 KB
/
rux.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
// Package rux is a simple and fast request router for golang HTTP applications.
//
// Source code and other details for the project are available at GitHub:
// https://github.com/gookit/rux
//
// Usage please ref examples and README
package rux
import (
"strings"
"github.com/gookit/color"
)
// All supported HTTP verb methods name
const (
GET = "GET"
PUT = "PUT"
HEAD = "HEAD"
POST = "POST"
PATCH = "PATCH"
TRACE = "TRACE"
DELETE = "DELETE"
CONNECT = "CONNECT"
OPTIONS = "OPTIONS"
)
const (
anyMatch = `[^/]+`
)
const (
// ContentType header key
ContentType = "Content-Type"
// ContentBinary represents content type application/octet-stream
ContentBinary = "application/octet-stream"
// ContentDisposition describes contentDisposition
ContentDisposition = "Content-Disposition"
// describes content disposition type
dispositionInline = "inline"
// describes content disposition type
dispositionAttachment = "attachment"
)
const (
// CTXMatchResult key name in the context
// CTXMatchResult = "_matchResult"
// CTXRecoverResult key name in the context
CTXRecoverResult = "_recoverResult"
// CTXAllowedMethods key name in the context
CTXAllowedMethods = "_allowedMethods"
// CTXCurrentRouteName key name in the context
CTXCurrentRouteName = "_currentRouteName"
// CTXCurrentRoutePath key name in the context
CTXCurrentRoutePath = "_currentRoutePath"
)
type routes []*Route
// like "GET": [ Route, ...]
type methodRoutes map[string]routes
// ControllerFace a simple controller interface
type ControllerFace interface {
// AddRoutes for support register routes in the controller.
AddRoutes(g *Router)
}
var (
debug bool
// current supported HTTP method
// all supported methods string, use for method check
// more: ,COPY,PURGE,LINK,UNLINK,LOCK,UNLOCK,VIEW,SEARCH
anyMethods = []string{GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD, CONNECT, TRACE}
)
// RESTFul method names definition
var (
IndexAction = "Index"
CreateAction = "Create"
StoreAction = "Store"
ShowAction = "Show"
EditAction = "Edit"
UpdateAction = "Update"
DeleteAction = "Delete"
// RESTFulActions action methods definition
RESTFulActions = map[string][]string{
IndexAction: {GET},
CreateAction: {GET},
StoreAction: {POST},
ShowAction: {GET},
EditAction: {GET},
UpdateAction: {PUT, PATCH},
DeleteAction: {DELETE},
}
)
// Debug switch debug mode
func Debug(val bool) {
debug = val
if debug {
color.Info.Println(" NOTICE, rux DEBUG mode is opened by rux.Debug(true)")
color.Info.Println("===========================================================")
}
}
// IsDebug return rux is debug mode.
func IsDebug() bool {
return debug
}
// AnyMethods get all methods
func AnyMethods() []string {
return anyMethods
}
// AllMethods get all methods
func AllMethods() []string {
return anyMethods
}
// MethodsString of all supported methods
func MethodsString() string {
return strings.Join(anyMethods, ",")
}
/*************************************************************
* Router options
*************************************************************/
// InterceptAll setting for the router
func InterceptAll(path string) func(*Router) {
return func(r *Router) {
r.interceptAll = strings.TrimSpace(path)
}
}
// MaxNumCaches setting for the router
func MaxNumCaches(num uint16) func(*Router) {
return func(r *Router) {
r.maxNumCaches = num
}
}
// CachingWithNum for the router
func CachingWithNum(num uint16) func(*Router) {
return func(r *Router) {
r.maxNumCaches = num
r.enableCaching = true
}
}
// UseEncodedPath enable for the router
func UseEncodedPath(r *Router) {
r.useEncodedPath = true
}
// EnableCaching for the router
func EnableCaching(r *Router) {
r.enableCaching = true
}
// StrictLastSlash enable for the router
func StrictLastSlash(r *Router) {
r.strictLastSlash = true
}
// MaxMultipartMemory set max memory limit for post forms
// func MaxMultipartMemory(max int64) func(*Router) {
// return func(r *Router) {
// r.maxMultipartMemory = max
// }
// }
// HandleFallbackRoute enable for the router
func HandleFallbackRoute(r *Router) {
r.handleFallbackRoute = true
}
// HandleMethodNotAllowed enable for the router
func HandleMethodNotAllowed(r *Router) {
r.handleMethodNotAllowed = true
}