-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
184 lines (163 loc) · 4.55 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package goblet
import (
"fmt"
"net/http"
"strings"
"github.com/extrame/goblet/config"
ge "github.com/extrame/goblet/error"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type router struct {
anchor *anchor
}
func (r *router) init() {
r.anchor = &anchor{0, "/", "", []*anchor{}, &_staticBlockOption{}}
}
func (rou *router) route(s *Server, w http.ResponseWriter, r *http.Request) (err error) {
defer func() {
errorWrap(w)
}()
var anch *anchor
var suffix_url string
var main, format string
if r.URL.Path == "/" {
anch, suffix_url = rou.anchor.match("/index", 6)
if !s.isSilence(r.URL.Path) {
logrus.Debugln("routing /index", r.URL.Path)
}
}
context := &Context{
s, r, w,
nil, suffix_url, format,
"", nil, "default", nil, nil, nil, "", 200, false, nil, nil, nil,
nil, false,
}
if s.nrPlugin != nil {
s.nrPlugin.OnNewRequest(context)
}
if anch == nil {
suff := strings.LastIndex(r.URL.Path, ".")
if suff > 0 && suff < len(r.URL.Path) {
format = r.URL.Path[suff+1:]
main = r.URL.Path[:suff]
} else {
main = r.URL.Path
}
anch, suffix_url = rou.anchor.match(main, len(main))
if !s.isSilence(r.URL.Path) {
logrus.Infof("routing %s", r.URL.Path)
}
if anch != nil {
logrus.Infof("(dynamic) %v", anch.opt)
}
} else {
format = "html"
}
if anch != nil {
w.Header().Add("Cache-Control", "no-store,no-cache,must-revalidate,post-check=0,pre-check=0")
w.Header().Add("Pragma", "no-cache")
context.option = anch.opt
context.suffix = suffix_url
context.format = format
if err = anch.opt.Parse(context); err == nil {
context.checkResponse()
if err = context.prepareRender(); err == nil {
err = context.render()
} else {
err = errors.Wrap(err, fmt.Sprintf("[Original Response]%v", context.response))
}
}
if s.Basic.Env == config.DevelopEnv && err != nil && !s.isSilence(r.URL.Path) {
logrus.Infoln("Err in Dynamic :", err)
}
return
}
return ge.NOSUCHROUTER("")
}
func (r *router) add(opt BlockOption) {
for _, v := range opt.GetRouting() {
r.addRoute(v, opt)
}
}
func (r *router) addRoute(path string, opt BlockOption) {
r.anchor.add(path, opt)
}
//---------------------anchors---------------
type anchor struct {
loc int
char string
prefix string
branches []*anchor
opt BlockOption
}
func (a *anchor) add(path string, opt BlockOption) bool {
if len(path) > a.loc {
var full_stored_path = a.prefix + a.char
if path[a.loc-len(a.prefix):a.loc+1] == full_stored_path {
for _, v := range a.branches {
if v.add(path, opt) {
return true
}
}
}
i := 0
// for i := 0; i < len(full_stored_path); i++ {
if path[a.loc+1-len(full_stored_path):a.loc+1-i] == full_stored_path[:len(full_stored_path)-i] {
var branch *anchor
if i != 0 {
branch = &anchor{a.loc, a.char, strings.TrimPrefix(a.prefix, full_stored_path[:len(full_stored_path)-i]), a.branches, a.opt}
a.branches = []*anchor{branch}
} else {
if path[a.loc-len(a.prefix):] == full_stored_path {
a.opt = opt
return true
}
}
//add new b
a.loc = a.loc - i
branch = &anchor{len(path) - 1, path[len(path)-1:], path[a.loc+1 : len(path)-1], []*anchor{}, opt}
a.branches = append(a.branches, branch)
//change a
a.char = full_stored_path[len(full_stored_path)-1-i : len(full_stored_path)-i]
a.prefix = full_stored_path[:len(full_stored_path)-1-i]
return true
}
// }
}
// else {
// loc_begin_prefix := a.loc - len(a.prefix)
// len_part_path := len(path) - loc_begin_prefix
// for i := loc_begin_prefix + len_part_path - 1; i > loc_begin_prefix; i-- {
// if path[loc_begin_prefix:i] == a.prefix[:i-loc_begin_prefix] {
// //new branch for old
// branch := &anchor{a.loc, a.char, a.prefix[i-loc_begin_prefix+1:], a.branches, a.opt}
// a.branches = []*anchor{branch}
// //change old
// a.char = a.prefix[i-loc_begin_prefix-1 : i-loc_begin_prefix]
// a.prefix = a.prefix[:i-loc_begin_prefix-1]
// a.loc = i - 1
// //new branch for new
// branch = &anchor{len(path) - 1, path[len(path)-1:], path[a.loc : len(path)-1], []*anchor{}, opt}
// a.branches = append(a.branches, branch)
// return true
// }
// }
// }
return false
}
func (a *anchor) match(path string, leng int) (*anchor, string) {
if leng > a.loc && path[a.loc:a.loc+1] == a.char {
if path[a.loc-len(a.prefix):a.loc] == a.prefix {
for _, v := range a.branches {
if res, suffix := v.match(path, leng); res != nil {
return res, suffix
}
}
if a.opt.MatchSuffix(path[a.loc+1:]) {
return a, path[a.loc+1:]
}
}
}
return nil, ""
}