-
Notifications
You must be signed in to change notification settings - Fork 63
/
helpers.go
149 lines (132 loc) · 3.06 KB
/
helpers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/martini-contrib/render"
"html/template"
"net/url"
"sync"
"time"
)
var Pipelines = template.FuncMap{
"strftime": Strftime,
"json": Json,
}
func Strftime(ts int64) string {
if ts == 0 {
return ""
}
t := time.Unix(ts, 0)
return t.Format("2006-01-02 15:04:05")
}
func Json(value interface{}) string {
if value == nil {
return "{}"
}
result, _ := json.Marshal(value)
return string(result)
}
func BodyBytes(data map[string]interface{}) []byte {
var buffer bytes.Buffer
i := 0
for k, v := range data {
var item = fmt.Sprintf("%s=%v", k, v)
buffer.WriteString(item)
if i < len(data)-1 {
buffer.WriteString("&")
}
i++
}
return buffer.Bytes()
}
func Urlcat(host string, urls string, params map[string]interface{}) string {
var protocol = "http"
var u, _ = url.Parse(fmt.Sprintf("%s://%s%s", protocol, host, urls))
var values, _ = url.ParseQuery(u.RawQuery)
for k, v := range params {
values.Add(k, fmt.Sprintf("%v", v))
}
u.RawQuery = values.Encode()
return u.String()
}
func GenMethodSelectors(method string) []MethodSelector {
methods := make([]MethodSelector, 5)
methods[0] = MethodSelector{"GET", false}
methods[1] = MethodSelector{"POST", false}
methods[2] = MethodSelector{"PUT", false}
methods[3] = MethodSelector{"DELETE", false}
methods[4] = MethodSelector{"HEAD", false}
if method == "GET" {
methods[0].Selected = true
} else if method == "POST" {
methods[1].Selected = true
} else if method == "PUT" {
methods[2].Selected = true
} else if method == "DELETE" {
methods[3].Selected = true
} else if method == "HEADER" {
methods[4].Selected = true
} else {
methods[0].Selected = true
}
return methods
}
func GenTeamSelectors(team string) []TeamSelector {
var teams = make([]TeamSelector, len(G_AlexTeams)+1)
teams[0] = TeamSelector{"", false}
for i := 1; i <= len(G_AlexTeams); i++ {
teams[i] = TeamSelector{G_AlexTeams[i-1], false}
}
for i := 0; i <= len(G_AlexTeams); i++ {
if teams[i].Team == team {
teams[i].Selected = true
}
}
return teams
}
func MaxInt(nums ...int) int {
max := nums[0]
for _, num := range nums {
if num > max {
max = num
}
}
return max
}
func RenderTemplate(r render.Render, tmpl string, context map[string]interface{}) {
context["ShowLayout"] = G_ShowLayout
r.HTML(200, tmpl, context)
}
type ConcurrentSet struct {
// thread safe string set
d map[string]bool
mutex sync.Mutex
}
func NewConcurrentSet() *ConcurrentSet {
return &ConcurrentSet{map[string]bool{}, sync.Mutex{}}
}
func (this *ConcurrentSet) Empty() bool {
return len(this.d) == 0
}
func (this *ConcurrentSet) Size() int {
this.mutex.Lock()
defer this.mutex.Unlock()
return len(this.d)
}
func (this *ConcurrentSet) Put(key string) {
this.mutex.Lock()
defer this.mutex.Unlock()
this.d[key] = true
}
func (this *ConcurrentSet) Exists(key string) bool {
this.mutex.Lock()
defer this.mutex.Unlock()
_, ok := this.d[key]
return ok
}
func (this *ConcurrentSet) Delete(key string) {
this.mutex.Lock()
defer this.mutex.Unlock()
delete(this.d, key)
}