-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhelper.go
215 lines (184 loc) · 4.71 KB
/
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
// 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 (
"encoding/json"
"fmt"
"os"
"reflect"
"runtime"
"strconv"
"strings"
)
/*************************************************************
* global path params
*************************************************************/
var globalVars = map[string]string{
"all": `.*`,
"any": `[^/]+`,
"num": `[1-9][0-9]*`,
}
// SetGlobalVar set an global path var
func SetGlobalVar(name, regex string) {
globalVars[name] = regex
}
// GetGlobalVars get all global path vars
func GetGlobalVars() map[string]string {
return globalVars
}
func getGlobalVar(name, def string) string {
if val, ok := globalVars[name]; ok {
return val
}
return def
}
/*************************************************************
* help functions
*************************************************************/
// no route params
func isFixedPath(s string) bool {
return strings.IndexByte(s, '{') < 0 && strings.IndexByte(s, '[') < 0
}
func resolveAddress(addr []string) (fullAddr string) {
ip := "0.0.0.0"
switch len(addr) {
case 0:
if port := os.Getenv("PORT"); len(port) > 0 {
debugPrint("Environment variable PORT=\"%s\"", port)
return ip + ":" + port
}
debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
return ip + ":8080"
case 1:
var port string
// "IP:PORT" OR ":PORT"
if strings.IndexByte(addr[0], ':') != -1 {
ss := strings.SplitN(addr[0], ":", 2)
if ss[0] != "" {
return addr[0]
}
port = ss[1]
} else { // Only port
port = addr[0]
}
return ip + ":" + port
default:
panic("too much parameters")
}
}
func checkAndParseOptional(path string) string {
noClosedOptional := strings.TrimRight(path, "]")
optionalNum := len(path) - len(noClosedOptional)
if optionalNum != strings.Count(noClosedOptional, "[") {
panic("Optional segments can only occur at the end of a route")
}
// '/hello[/{name}]' -> '/hello(?:/{name})?'
return strings.NewReplacer("[", "(?:", "]", ")?").Replace(path)
}
func quotePointChar(path string) string {
if strings.IndexByte(path, '.') > 0 {
// "about.html" -> "about\.html"
return strings.Replace(path, ".", `\.`, -1)
}
return path
}
func nameOfFunction(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func debugPrintRoute(route *Route) {
// if debug {
// fmt.Println("[SUX-DEBUG]", route.String())
// }
debugPrint(route.String())
}
func panicf(f string, v ...interface{}) {
panic(fmt.Sprintf(f, v...))
}
func debugPrintError(err error) {
if err != nil {
debugPrint("[ERROR] %v\n", err)
}
}
func debugPrint(f string, v ...interface{}) {
if debug {
msg := fmt.Sprintf(f, v...)
// fmt.Printf("[RUX-DEBUG] %s %s\n", time.Now().Format("2006-01-02 15:04:05"), msg)
fmt.Printf("[RUX-DEBUG] %s\n", msg)
}
}
// from gin framework
func parseAccept(acceptHeader string) []string {
if acceptHeader == "" {
return []string{}
}
parts := strings.Split(acceptHeader, ",")
outs := make([]string, 0, len(parts))
for _, part := range parts {
if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
outs = append(outs, part)
}
}
return outs
}
func formatMethods(methods []string) (formatted []string) {
for _, method := range methods {
method = strings.TrimSpace(method)
if method != "" {
formatted = append(formatted, strings.ToUpper(method))
}
}
return
}
func formatMethodsWithDefault(methods []string, defMethod string) []string {
if len(methods) == 0 {
methods = []string{defMethod}
} else {
methods = formatMethods(methods)
}
return methods
}
func toString(i interface{}) string {
if i == nil {
return ""
}
switch value := i.(type) {
case int:
return strconv.Itoa(value)
case int8:
return strconv.Itoa(int(value))
case int16:
return strconv.Itoa(int(value))
case int32:
return strconv.Itoa(int(value))
case int64:
return strconv.Itoa(int(value))
case uint:
return strconv.FormatUint(uint64(value), 10)
case uint8:
return strconv.FormatUint(uint64(value), 10)
case uint16:
return strconv.FormatUint(uint64(value), 10)
case uint32:
return strconv.FormatUint(uint64(value), 10)
case uint64:
return strconv.FormatUint(value, 10)
case float32:
return strconv.FormatFloat(float64(value), 'f', -1, 32)
case float64:
return strconv.FormatFloat(value, 'f', -1, 64)
case bool:
return strconv.FormatBool(value)
case string:
return value
case []byte:
return string(value)
default:
// String conversion using JSON by default
jsonContent, _ := json.Marshal(value)
return string(jsonContent)
}
}