-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.go
executable file
·127 lines (109 loc) · 2.85 KB
/
router.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
127
// Copyright 2016 José Santos <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package router
import (
"fmt"
"net/http"
)
type Handler func(http.ResponseWriter, *http.Request, Parameter)
type Router struct {
trees map[string]*routeNode
}
func New() *Router {
return &Router{trees: make(map[string]*routeNode)}
}
func splitURLpath(path string) (parts []string, names map[string]int) {
var (
nameidx int = -1
partidx int
paramCounter int
)
for i := 0; i < len(path); i++ {
// recording name
if nameidx != -1 {
//found /
if path[i] == '/' {
if names == nil {
names = make(map[string]int)
}
names[path[nameidx:i]] = paramCounter
paramCounter++
nameidx = -1 // switch to normal recording
partidx = i
}
} else {
if path[i] == ':' || path[i] == '*' {
if path[i-1] != '/' {
panic(fmt.Errorf("Inválid parameter : or * comes anwais after / - %q", path))
}
nameidx = i + 1
if partidx != i {
parts = append(parts, path[partidx:i])
}
parts = append(parts, path[i:nameidx])
}
}
}
if nameidx != -1 {
if names == nil {
names = make(map[string]int)
}
names[path[nameidx:]] = paramCounter
paramCounter++
} else if partidx < len(path) {
parts = append(parts, path[partidx:])
}
return
}
func (router *Router) Finalize() {
for _, _node := range router.trees {
_node.finalize()
}
}
func (router *Router) FindRoute(method string, path string) (Handler, Parameter) {
_node := router.trees[method]
if _node == nil {
return nil, Parameter{}
}
fn, wildcard := _node.findRoute(path)
if fn != nil {
return fn.handler, Parameter{routeNode: fn, path: path, wildcard: wildcard}
}
return nil, Parameter{}
}
func (router *Router) AddRoute(method string, path string, fn Handler) {
parts, names := splitURLpath(path)
_node := router.trees[method]
if _node == nil {
_node = &routeNode{}
router.trees[method] = _node
}
_node.addRoute(parts, names, fn)
_node.optimizeRoutes()
}
func (router *Router) String() string {
var lines string
for method, _node := range router.trees {
lines += method + " " + _node.String()
}
return lines
}
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler, variables := router.FindRoute(r.Method, r.URL.Path)
if handler != nil {
handler(w, r, variables)
} else {
http.NotFound(w, r)
}
}