-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorr.go
64 lines (59 loc) · 1.25 KB
/
gorr.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 gorr
import (
"fmt"
"net/http"
"strings"
"github.com/Contra-Culture/report"
"github.com/google/uuid"
)
type (
Dispatcher struct {
root *Node
}
DispatcherCfgr struct {
dispatcher *Dispatcher
report report.Node
}
PathHelper func(map[string]string) string
PathHelpers map[string]PathHelper
)
func (c *DispatcherCfgr) Root(d string, cfg func(*StaticNodeCfgr)) {
if c.dispatcher.root != nil {
c.report.Error("root node already specified")
return
}
r := report.New("root")
root := new(nil, STATIC)
nc := &StaticNodeCfgr{
NodeCfgr{
node: root,
report: r,
},
}
cfg(nc)
c.dispatcher.root = root
c.report = r
}
func New(cfg func(*DispatcherCfgr)) (d *Dispatcher, r report.Node) {
d = &Dispatcher{}
r = report.New("dispatcher")
cfg(
&DispatcherCfgr{
dispatcher: d,
report: r,
})
r.Finalize()
return
}
func (d *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rep := report.New("Request ID: %s", requestUUID())
rep.Info("URL: %s, Method: %s", r.URL.String(), r.Method)
child := rep.Structure("root route")
d.root.Handle(child, w, r)
child.Finalize()
rep.Finalize()
fmt.Println(report.ToString(rep))
}
func requestUUID() string {
return strings.Replace(uuid.New().String(), "-", "", -1)
}