/
route.go
71 lines (62 loc) · 1.43 KB
/
route.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
package web
import (
"fmt"
"net/http"
"regexp"
"strings"
)
// Entry route entry point
type Entry struct {
regex *regexp.Regexp
MyInterface Handler
}
// Multiplexer mux
type Multiplexer []*Entry
// NewMultiplexer new mux
func NewMultiplexer() Multiplexer {
var mux []*Entry
return mux
}
// Handle std http handle
func (mux *Multiplexer) Handle(path string, handler http.Handler) {
warp := warpHandlerFunc(handler.ServeHTTP)
mux.Route(path, warp)
}
// HandleFunc handlefunc
func (mux *Multiplexer) HandleFunc(path string, f http.HandlerFunc) {
warp := warpHandlerFunc(f)
mux.Route(path, warp)
}
// Route handle
func (mux *Multiplexer) Route(path string, handler Handler) {
entry := Entry{
regex: regexp.MustCompile(path),
MyInterface: handler,
}
*mux = append(*mux, &entry)
}
// RouteFunc route handlerFunc
func (mux *Multiplexer) RouteFunc(path string, f HandlerFunc) {
mux.Route(path, f)
}
// FindRoute find router
func (mux Multiplexer) FindRoute(path string) *Entry {
for _, m := range mux {
if matchs := m.regex.FindStringSubmatch(path); matchs != nil {
match := make(map[string]string, len(matchs))
for idx, value := range m.regex.SubexpNames() {
match[value] = matchs[idx]
}
delete(match, "")
return m
}
}
return nil
}
func (mux Multiplexer) String() string {
var res []string
for _, m := range mux {
res = append(res, fmt.Sprintf("%s", m.regex))
}
return strings.Join(res, "\n")
}