-
Notifications
You must be signed in to change notification settings - Fork 34
/
utils.go
179 lines (139 loc) · 3.12 KB
/
utils.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
package traffic
import (
"fmt"
"os"
"path"
"regexp"
"strconv"
"strings"
)
type ILogger interface {
Print(...interface{})
Printf(string, ...interface{})
}
const (
EnvDevelopment = "development"
DefaultViewsPath = "views"
DefaultPublicPath = "public"
DefaultConfigFile = "traffic.conf"
DefaultPort = 3000
DefaultHost = "127.0.0.1"
)
var (
env map[string]interface{}
logger ILogger
)
func Logger() ILogger {
return logger
}
func SetLogger(customLogger ILogger) {
logger = customLogger
}
func SetVar(key string, value interface{}) {
env[key] = value
}
func GetVar(key string) interface{} {
if value := env[key]; value != nil {
return value
}
envKey := fmt.Sprintf("TRAFFIC_%s", strings.ToUpper(key))
if value := os.Getenv(envKey); value != "" {
return value
}
return nil
}
func getStringVar(key string) string {
value := GetVar(key)
if s, ok := value.(string); ok {
return s
}
return ""
}
func pathToRegexp(routePath string) (*regexp.Regexp, bool) {
var (
re *regexp.Regexp
isStatic bool
)
regexpString := routePath
isStaticRegexp := regexp.MustCompile(`[\(\)\?\<\>:]`)
if !isStaticRegexp.MatchString(routePath) {
isStatic = true
}
// Dots
re = regexp.MustCompile(`([^\\])\.`)
regexpString = re.ReplaceAllStringFunc(regexpString, func(m string) string {
return fmt.Sprintf(`%s\.`, string(m[0]))
})
// Wildcard names
re = regexp.MustCompile(`:[^/#?()\.\\]+\*`)
regexpString = re.ReplaceAllStringFunc(regexpString, func(m string) string {
return fmt.Sprintf("(?P<%s>.+)", m[1:len(m)-1])
})
re = regexp.MustCompile(`:[^/#?()\.\\]+`)
regexpString = re.ReplaceAllStringFunc(regexpString, func(m string) string {
return fmt.Sprintf(`(?P<%s>[^/#?]+)`, m[1:len(m)])
})
s := fmt.Sprintf(`\A%s\z`, regexpString)
return regexp.MustCompile(s), isStatic
}
func getStringVarWithDefault(key, defaultValue string) string {
value := getStringVar(key)
if value == "" {
return defaultValue
}
return value
}
func Env() string {
return getStringVarWithDefault("env", EnvDevelopment)
}
func RootPath() string {
return getStringVarWithDefault("root", ".")
}
func ViewsPath() string {
viewsPath := getStringVarWithDefault("views", DefaultViewsPath)
if path.IsAbs(viewsPath) {
return viewsPath
}
return path.Join(RootPath(), viewsPath)
}
func ConfigFilePath() string {
filePath := getStringVarWithDefault("config_file", DefaultConfigFile)
if path.IsAbs(filePath) {
return filePath
}
return path.Join(RootPath(), filePath)
}
func PublicPath() string {
publicPath := getStringVarWithDefault("public", DefaultPublicPath)
if path.IsAbs(publicPath) {
return publicPath
}
return path.Join(RootPath(), publicPath)
}
func SetPort(port int) {
SetVar("port", port)
}
func Port() int {
port := GetVar("port")
if port == nil {
return DefaultPort
}
if i, ok := port.(int); ok {
return i
}
if s, ok := port.(string); ok {
i, err := strconv.Atoi(s)
if err != nil {
return DefaultPort
}
return i
}
return DefaultPort
}
func Host() string {
host := getStringVarWithDefault("host", DefaultHost)
return host
}
func SetHost(host string) {
SetVar("host", host)
}