-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
59 lines (51 loc) · 1.33 KB
/
util.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
package lightning
import (
"net/http"
"os"
"strings"
)
// parsePattern splits a route pattern string into its individual parts.
func parsePattern(pattern string) []string {
parts := strings.Split(pattern, "/")
result := make([]string, 0)
for _, part := range parts {
if part != "" {
result = append(result, part)
if part[0] == '*' {
break
}
}
}
return result
}
func resolveAddress(addr []string) string {
if port := os.Getenv("PORT"); port != "" {
logger.Debug("Environment variable PORT=\"%s\"", port)
return ":" + port
}
switch len(addr) {
case 0:
logger.Debug("Using port :6789 by default")
return ":6789"
case 1:
return addr[0]
default:
panic("too many parameters")
}
}
// defaultNotFound is the default handler function for 404 Not Found error
func defaultNotFound(ctx *Context) {
ctx.Text(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
// defaultInternalServerError is the default handler function for 500 Internal Server Error
func defaultInternalServerError(ctx *Context) {
ctx.Text(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
// isValidHTTPMethod checks if a given HTTP method is valid.
func isValidHTTPMethod(method string) bool {
switch method {
case "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS":
return true
}
return false
}