-
Notifications
You must be signed in to change notification settings - Fork 8
/
urls.go
68 lines (55 loc) · 1.18 KB
/
urls.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
package gottp
import (
"log"
"net/http"
"regexp"
)
type Url struct {
name string
url string
handler Handler
pattern *regexp.Regexp
}
var boundUrls = []*Url{}
func NewUrl(name string, pattern string, handler Handler) {
compiled_pattern, err := regexp.Compile(pattern)
if err != nil {
panic(err)
}
url := Url{
name: name,
handler: handler,
pattern: compiled_pattern,
url: pattern,
}
boundUrls = append(boundUrls, &url)
}
func (u Url) MakeUrlArgs(url *string) (*map[string]string, bool) {
matches := u.pattern.FindStringSubmatch(*url)
named_groups := u.pattern.SubexpNames()
data := map[string]string{}
var err bool
if len(matches) > 0 {
for ix, key := range named_groups {
if len(key) > 0 {
data[key] = matches[ix]
}
}
} else if len(named_groups) > 0 {
err = true
}
return &data, err
}
func bindGlobalHandler() {
http.HandleFunc("/", GlobalHandler)
}
func bindHandlers() {
NewUrl("async_pipe", "^/async_pipe/?$", new(AsyncPipeHandler))
NewUrl("pipe", "^/pipe/?$", new(PipeHandler))
NewUrl("urls", "^/urls/?$", new(UrlHandler))
}
func init() {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
bindGlobalHandler()
bindHandlers()
}