-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
73 lines (64 loc) · 1.46 KB
/
utils.go
File metadata and controls
73 lines (64 loc) · 1.46 KB
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
package main
import (
"fmt"
"html/template"
"log"
"net"
"net/http"
"net/url"
"path"
"regexp"
"strconv"
"strings"
)
func IsParamSet(r *http.Request, param string) bool {
return len(r.URL.Query().Get(param)) > 0
}
func Lang(r *http.Request) string {
lang := r.URL.Query().Get("lang")
if len(lang) == 0 {
lang = "en_US"
}
return lang
}
func GetQS(q url.Values, param string, deflt int) (num int, str string) {
str = q.Get(param)
num, err := strconv.Atoi(str)
if err != nil {
num = deflt
str = ""
} else {
str = fmt.Sprintf("&%s=%s", param, str)
}
return
}
func GetHost(r *http.Request) (host string, err error) {
// get remote ip
host = r.Header.Get("X-Forwarded-For")
if len(host) > 0 {
parts := strings.Split(host, ",")
// apache will append the remote address
host = strings.TrimSpace(parts[len(parts)-1])
} else {
host, _, err = net.SplitHostPort(r.RemoteAddr)
}
return
}
var TBBUserAgents = regexp.MustCompile(`^Mozilla/5\.0 \([^)]*\) Gecko/([\d]+\.0|20100101) Firefox/[\d]+\.0$`)
func LikelyTBB(ua string) bool {
return TBBUserAgents.MatchString(ua)
}
var Layout *template.Template
func CompileTemplate(base string, templateName string) *template.Template {
if Layout == nil {
Layout = template.New("")
Layout = template.Must(Layout.ParseFiles(
path.Join(base, "public/base.html"),
))
}
l, err := Layout.Clone()
if err != nil {
log.Fatal(err)
}
return template.Must(l.ParseFiles(path.Join(base, "public/", templateName)))
}