-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
94 lines (87 loc) · 1.89 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
package fresh
import (
"strconv"
"strings"
"time"
"github.com/fatih/color"
"math/rand"
"net"
"regexp"
)
const (
B = 1 << (10 * iota)
K
M
G
T
)
// RandPort return an available server port number
func randPort(address string, port int) int {
ln, err := net.Listen("tcp", address+":"+strconv.Itoa(port))
defer ln.Close()
if err != nil {
rand.Seed(time.Now().Unix())
return randPort(address, rand.Intn(9999-1111)+1111)
}
return port
}
// Size convert a string like 10K or 5MB in relative int64 number size
func size(s string) (r int64) {
format := regexp.MustCompile("[A-Z]+")
num, err := strconv.ParseInt(regexp.MustCompile("[0-9]+").String(), 10, 64)
if err != nil {
return
}
switch format.String() {
case "B", "b":
return num * B
case "KB", "K", "kb", "k":
return num * K
case "MB", "M", "mb", "m":
return num * M
case "GB", "G", "gb", "g":
return num * G
case "TB", "T", "tb", "t":
return num * G
}
return
}
// Contain check if a string is inserted into a strings array
func contain(s string, arr []string) bool {
s = strings.ToLower(s)
for _, val := range arr {
if val == s {
return true
}
}
return false
}
// Print the list of routes
func PrintRouter(r *router) {
var tree func(routes []*route, parentPath string) error
tree = func(routes []*route, parentPath string) error {
for _, route := range routes {
separator := ""
if strings.HasSuffix(parentPath, "/") == false {
separator = "/"
}
currentPath := parentPath + separator + route.path
for _, handler := range route.handlers {
print(time.Now().Format("(2006-01-02 03:04:05)---"))
print("[")
color.Set(color.FgHiGreen)
print(handler.method)
color.Unset()
print("]")
for i := len(handler.method); i < 8; i++ {
print("-")
}
print(">")
println(currentPath)
}
tree(route.children, currentPath)
}
return nil
}
tree([]*route{r.route}, "")
}