-
Notifications
You must be signed in to change notification settings - Fork 0
/
pistol.go
126 lines (103 loc) · 2.96 KB
/
pistol.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
package Sex
import (
"github.com/Showmax/go-fqdn"
"github.com/rs/cors"
"net/http"
"strings"
"log"
"os"
)
type Pistol struct {
*http.ServeMux
RootPath string
RouteConfs Dict
Routes Dict
RawRoutes []string
Auth bool
}
func NewPistol() *Pistol {
router := new(Pistol)
router.ServeMux = http.NewServeMux()
if logger == nil {
logger = log.New(os.Stderr, "\r\n", log.LstdFlags)
}
return router
}
func (router *Pistol) Add(path string, route interface {}, methods ...string) *Pistol {
if methods == nil {
methods = []string{"GET"}
}
path = fixPath(path)
root_path := fixPath(router.RootPath)
if (path != root_path) {
path = root_path + path
}
path_pattern := GetPathPattern(path)
if len(router.RouteConfs) == 0 {
router.RouteConfs = make(Dict)
}
if len(router.Routes) == 0 {
router.Routes = make(Dict)
}
if _, exist := router.Routes[path_pattern]; !exist {
router.Routes[path_pattern] = make(Prop)
}
if _, exist := router.RouteConfs[path_pattern]; !exist {
router.RouteConfs[path_pattern] = make(Prop)
}
conf := Prop{}
conf["path-template"] = path
router.RouteConfs[path_pattern] = conf
for _, method := range methods {
method = strings.ToUpper(method)
router.Routes[path_pattern][method] = route
}
return router
}
func (router *Pistol) AddRaw(path string, f func(http.ResponseWriter, *http.Request)) (*Pistol) {
if router.ServeMux == nil {
router.ServeMux = http.NewServeMux()
}
path = fixPath(path)
router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request){
Log(r.Method, path, r.URL.RawQuery)
f(w, r)
})
router.RawRoutes = append(router.RawRoutes, path)
return router
}
func (router *Pistol) Run(a ...interface{}) error {
port := 8000
path := "/"
if a != nil {
for _, v := range a {
if ValidateData(v, GenericString) {
path = v.(string)
}
if ValidateData(v, GenericInt) {
port = v.(int)
}
}
}
router.RootPath = path
host, err := fqdn.FqdnHostname()
if err != nil {
host = "localhost"
}
Log(Fmt("Running Sex Pistol server at %s:%d%s", host, port, path))
if GetEnv("SEX_DEBUG", "false") != "false" {
for path, methods := range router.Routes {
methods_str := ""
for method := range methods {
methods_str += Fmt("%s ", method)
}
Log(Fmt("%s <- %s", router.RouteConfs[path]["path-template"], methods_str))
}
for _, path := range router.RawRoutes {
Log(Fmt("%s <- %s", path, "ALL METHODS"))
}
}
router.AddRaw("/", router.ROOT)
handler := cors.Default().Handler(router)
return http.ListenAndServe(Fmt(":%d", port), handler)
}