-
Notifications
You must be signed in to change notification settings - Fork 2
/
ink.go
147 lines (127 loc) · 3.59 KB
/
ink.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package ink
import (
"net/http"
"strings"
"regexp"
"fmt"
)
/* data structure */
type MatchMap map[string]string
type Handle func(ctx *Context)
type Context struct {
http.ResponseWriter
Res http.ResponseWriter
Req *http.Request
Param MatchMap
Ware map[string]interface{}
Stop func()
}
type Web struct {
route map[string][]Handle
patternAry [][]string
patternRegx regexp.Regexp
}
/* private method */
func (web *Web) ServeHTTP(w http.ResponseWriter, r *http.Request) {
matchMap, pattern := web.match(r.URL.Path)
ctx := &Context{w, w, r, matchMap, make(map[string]interface{}), nil}
allHandle := make([]Handle, 0)
handleAry1, ok1 := web.route[r.Method + ":" + pattern]
if ok1 {
allHandle = append(allHandle, handleAry1...)
}
handleAry2, ok2 := web.route[r.Method + ":*"]
if ok2 {
allHandle = append(allHandle, handleAry2...)
}
if len(allHandle) != 0 {
for _, handle := range allHandle {
keep := true
ctx.Stop = func() {
keep = false
}
handle(ctx)
if !keep {
return
}
}
} else {
http.NotFound(w, r)
}
}
func (web *Web) match(path string) (matchMap MatchMap, pattern string) {
pathAry := web.patternRegx.FindAllString(path, -1)
matchMap = make(map[string]string)
for _, patternItem := range web.patternAry {
if len(pathAry) != len(patternItem) {
continue
}
for j, patternKey := range patternItem {
if j > len(pathAry) - 1 {
break
}
pathKey := pathAry[j]
if strings.HasPrefix(patternKey, ":") {
name := strings.TrimPrefix(patternKey, ":")
matchMap[name] = pathKey
} else {
if pathKey != patternKey {
break
}
}
// match success
if j == len(patternItem) - 1 {
pattern = strings.Join(patternItem, "/")
return
}
}
}
return
}
func (web *Web) addHandle(method string, pattern string, handle Handle) {
path := method + ":" + pattern
if _, ok := web.route[path]; !ok {
web.route[path] = make([]Handle, 0)
}
web.route[path] = append(web.route[path], handle)
web.patternAry = append(web.patternAry, strings.Split(pattern, "/"))
}
/* public api */
func (web *Web) Use(handle Handle) {
methods := []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"}
for _, method := range methods {
web.addHandle(method, "*", handle)
}
}
func (web *Web) Get(pattern string, handle Handle) {
web.addHandle("GET", pattern, handle)
}
func (web *Web) Post(pattern string, handle Handle) {
web.addHandle("POST", pattern, handle)
}
func (web *Web) Put(pattern string, handle Handle) {
web.addHandle("PUT", pattern, handle)
}
func (web *Web) Delete(pattern string, handle Handle) {
web.addHandle("DELETE", pattern, handle)
}
func (web *Web) Options(pattern string, handle Handle) {
web.addHandle("OPTIONS", pattern, handle)
}
func (web *Web) Head(pattern string, handle Handle) {
web.addHandle("HEAD", pattern, handle)
}
func (web *Web) Listen(addr string) {
err := http.ListenAndServe(addr, web)
if err != nil {
fmt.Println(err)
}
}
func New() (web Web) {
web = Web{}
web.route = make(map[string][]Handle)
web.patternAry = make([][]string, 0)
patternRegx, _ := regexp.Compile("([^/])*")
web.patternRegx = *patternRegx
return
}