-
Notifications
You must be signed in to change notification settings - Fork 0
/
pistol.go
64 lines (52 loc) · 1.34 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
package Sex
import (
"net/http"
"strings"
"log"
"os"
)
type Pistol struct {
RootPath string
RouteConfs Dict
Routes Dict
Auth bool
Mux *http.ServeMux
}
func NewPistol() *Pistol {
router := new(Pistol)
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
}