Skip to content

Commit ecf5094

Browse files
committed
updates
1 parent ee04b3f commit ecf5094

File tree

3 files changed

+499
-152
lines changed

3 files changed

+499
-152
lines changed

coverage.out

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -90,73 +90,79 @@ github.com/GolangToolKits/grrt/reqRoute.go:280.2,280.19 1 1
9090
github.com/GolangToolKits/grrt/reqRoute.go:283.55,287.2 3 1
9191
github.com/GolangToolKits/grrt/reqRouter.go:36.71,40.53 1 1
9292
github.com/GolangToolKits/grrt/reqRouter.go:40.53,42.3 1 1
93-
github.com/GolangToolKits/grrt/reqRouter.go:42.8,49.22 5 1
94-
github.com/GolangToolKits/grrt/reqRouter.go:49.22,52.4 1 1
95-
github.com/GolangToolKits/grrt/reqRouter.go:54.3,54.16 1 1
96-
github.com/GolangToolKits/grrt/reqRouter.go:54.16,56.4 1 1
97-
github.com/GolangToolKits/grrt/reqRouter.go:58.3,58.34 1 1
98-
github.com/GolangToolKits/grrt/reqRouter.go:58.34,60.4 1 1
99-
github.com/GolangToolKits/grrt/reqRouter.go:60.9,60.43 1 1
100-
github.com/GolangToolKits/grrt/reqRouter.go:60.43,62.4 1 1
101-
github.com/GolangToolKits/grrt/reqRouter.go:62.9,65.4 2 1
102-
github.com/GolangToolKits/grrt/reqRouter.go:70.38,74.2 3 1
103-
github.com/GolangToolKits/grrt/reqRouter.go:77.69,80.16 3 1
104-
github.com/GolangToolKits/grrt/reqRouter.go:80.16,82.3 1 1
105-
github.com/GolangToolKits/grrt/reqRouter.go:82.8,84.28 2 1
106-
github.com/GolangToolKits/grrt/reqRouter.go:84.28,85.55 1 1
107-
github.com/GolangToolKits/grrt/reqRouter.go:85.55,88.5 2 1
108-
github.com/GolangToolKits/grrt/reqRouter.go:90.3,90.12 1 1
109-
github.com/GolangToolKits/grrt/reqRouter.go:90.12,92.4 1 1
110-
github.com/GolangToolKits/grrt/reqRouter.go:94.2,94.11 1 1
111-
github.com/GolangToolKits/grrt/reqRouter.go:98.95,101.16 3 1
112-
github.com/GolangToolKits/grrt/reqRouter.go:101.16,103.3 1 1
113-
github.com/GolangToolKits/grrt/reqRouter.go:103.8,105.28 2 1
114-
github.com/GolangToolKits/grrt/reqRouter.go:105.28,106.55 1 1
115-
github.com/GolangToolKits/grrt/reqRouter.go:106.55,109.5 2 1
116-
github.com/GolangToolKits/grrt/reqRouter.go:111.3,111.12 1 1
117-
github.com/GolangToolKits/grrt/reqRouter.go:111.12,113.4 1 1
118-
github.com/GolangToolKits/grrt/reqRouter.go:115.2,115.11 1 1
119-
github.com/GolangToolKits/grrt/reqRouter.go:119.49,122.16 3 1
120-
github.com/GolangToolKits/grrt/reqRouter.go:122.16,124.3 1 1
121-
github.com/GolangToolKits/grrt/reqRouter.go:125.2,125.11 1 1
122-
github.com/GolangToolKits/grrt/reqRouter.go:129.34,131.2 1 1
123-
github.com/GolangToolKits/grrt/reqRouter.go:134.44,136.2 1 1
124-
github.com/GolangToolKits/grrt/reqRouter.go:139.55,142.28 3 1
125-
github.com/GolangToolKits/grrt/reqRouter.go:142.28,144.20 2 1
126-
github.com/GolangToolKits/grrt/reqRouter.go:144.20,145.12 1 1
127-
github.com/GolangToolKits/grrt/reqRouter.go:147.3,147.55 1 1
128-
github.com/GolangToolKits/grrt/reqRouter.go:152.55,156.2 3 1
129-
github.com/GolangToolKits/grrt/reqRouter.go:159.56,162.28 3 1
130-
github.com/GolangToolKits/grrt/reqRouter.go:162.28,164.20 2 1
131-
github.com/GolangToolKits/grrt/reqRouter.go:164.20,165.12 1 1
132-
github.com/GolangToolKits/grrt/reqRouter.go:167.3,167.55 1 1
133-
github.com/GolangToolKits/grrt/reqRouter.go:171.55,175.28 4 1
134-
github.com/GolangToolKits/grrt/reqRouter.go:175.28,177.3 1 1
135-
github.com/GolangToolKits/grrt/reqRouter.go:178.2,178.30 1 1
136-
github.com/GolangToolKits/grrt/reqRouter.go:181.49,185.2 3 1
137-
github.com/GolangToolKits/grrt/reqRouter.go:187.70,193.33 5 1
138-
github.com/GolangToolKits/grrt/reqRouter.go:193.33,195.3 1 1
139-
github.com/GolangToolKits/grrt/reqRouter.go:195.8,197.3 1 1
140-
github.com/GolangToolKits/grrt/reqRouter.go:198.2,202.23 4 1
141-
github.com/GolangToolKits/grrt/reqRouter.go:202.23,205.12 1 1
142-
github.com/GolangToolKits/grrt/reqRouter.go:205.12,206.9 1 1
143-
github.com/GolangToolKits/grrt/reqRouter.go:214.3,214.24 1 1
144-
github.com/GolangToolKits/grrt/reqRouter.go:214.24,216.4 1 1
145-
github.com/GolangToolKits/grrt/reqRouter.go:216.9,218.4 1 1
146-
github.com/GolangToolKits/grrt/reqRouter.go:220.3,221.17 2 1
147-
github.com/GolangToolKits/grrt/reqRouter.go:221.17,222.28 1 1
148-
github.com/GolangToolKits/grrt/reqRouter.go:222.28,223.38 1 1
149-
github.com/GolangToolKits/grrt/reqRouter.go:223.38,227.41 4 1
150-
github.com/GolangToolKits/grrt/reqRouter.go:227.41,229.7 1 1
151-
github.com/GolangToolKits/grrt/reqRouter.go:230.6,230.11 1 1
152-
github.com/GolangToolKits/grrt/reqRouter.go:231.11,231.77 1 1
153-
github.com/GolangToolKits/grrt/reqRouter.go:231.77,233.6 1 1
154-
github.com/GolangToolKits/grrt/reqRouter.go:235.9,237.4 1 1
155-
github.com/GolangToolKits/grrt/reqRouter.go:239.2,239.19 1 1
156-
github.com/GolangToolKits/grrt/reqRouter.go:242.96,244.36 2 1
157-
github.com/GolangToolKits/grrt/reqRouter.go:244.36,245.32 1 1
158-
github.com/GolangToolKits/grrt/reqRouter.go:245.32,247.4 1 1
159-
github.com/GolangToolKits/grrt/reqRouter.go:249.2,250.27 2 1
93+
github.com/GolangToolKits/grrt/reqRouter.go:42.8,48.13 4 1
94+
github.com/GolangToolKits/grrt/reqRouter.go:48.13,51.36 3 1
95+
github.com/GolangToolKits/grrt/reqRouter.go:51.36,54.5 1 1
96+
github.com/GolangToolKits/grrt/reqRouter.go:63.3,63.26 1 1
97+
github.com/GolangToolKits/grrt/reqRouter.go:63.26,65.4 1 1
98+
github.com/GolangToolKits/grrt/reqRouter.go:65.9,65.32 1 1
99+
github.com/GolangToolKits/grrt/reqRouter.go:65.32,67.4 1 1
100+
github.com/GolangToolKits/grrt/reqRouter.go:69.3,69.34 1 1
101+
github.com/GolangToolKits/grrt/reqRouter.go:69.34,71.4 1 1
102+
github.com/GolangToolKits/grrt/reqRouter.go:71.9,71.43 1 1
103+
github.com/GolangToolKits/grrt/reqRouter.go:71.43,73.4 1 1
104+
github.com/GolangToolKits/grrt/reqRouter.go:73.9,76.4 2 1
105+
github.com/GolangToolKits/grrt/reqRouter.go:81.38,85.2 3 1
106+
github.com/GolangToolKits/grrt/reqRouter.go:88.69,91.16 3 1
107+
github.com/GolangToolKits/grrt/reqRouter.go:91.16,93.3 1 1
108+
github.com/GolangToolKits/grrt/reqRouter.go:93.8,95.28 2 1
109+
github.com/GolangToolKits/grrt/reqRouter.go:95.28,96.55 1 1
110+
github.com/GolangToolKits/grrt/reqRouter.go:96.55,99.5 2 1
111+
github.com/GolangToolKits/grrt/reqRouter.go:101.3,101.12 1 1
112+
github.com/GolangToolKits/grrt/reqRouter.go:101.12,103.4 1 1
113+
github.com/GolangToolKits/grrt/reqRouter.go:105.2,105.11 1 1
114+
github.com/GolangToolKits/grrt/reqRouter.go:109.95,112.16 3 1
115+
github.com/GolangToolKits/grrt/reqRouter.go:112.16,114.3 1 1
116+
github.com/GolangToolKits/grrt/reqRouter.go:114.8,116.28 2 1
117+
github.com/GolangToolKits/grrt/reqRouter.go:116.28,117.55 1 1
118+
github.com/GolangToolKits/grrt/reqRouter.go:117.55,120.5 2 1
119+
github.com/GolangToolKits/grrt/reqRouter.go:122.3,122.12 1 1
120+
github.com/GolangToolKits/grrt/reqRouter.go:122.12,124.4 1 1
121+
github.com/GolangToolKits/grrt/reqRouter.go:126.2,126.11 1 1
122+
github.com/GolangToolKits/grrt/reqRouter.go:130.49,133.16 3 1
123+
github.com/GolangToolKits/grrt/reqRouter.go:133.16,135.3 1 1
124+
github.com/GolangToolKits/grrt/reqRouter.go:136.2,136.11 1 1
125+
github.com/GolangToolKits/grrt/reqRouter.go:140.34,142.2 1 1
126+
github.com/GolangToolKits/grrt/reqRouter.go:145.44,147.2 1 1
127+
github.com/GolangToolKits/grrt/reqRouter.go:150.55,153.28 3 1
128+
github.com/GolangToolKits/grrt/reqRouter.go:153.28,155.20 2 1
129+
github.com/GolangToolKits/grrt/reqRouter.go:155.20,156.12 1 1
130+
github.com/GolangToolKits/grrt/reqRouter.go:158.3,158.55 1 1
131+
github.com/GolangToolKits/grrt/reqRouter.go:163.55,167.2 3 1
132+
github.com/GolangToolKits/grrt/reqRouter.go:170.56,173.28 3 1
133+
github.com/GolangToolKits/grrt/reqRouter.go:173.28,175.20 2 1
134+
github.com/GolangToolKits/grrt/reqRouter.go:175.20,176.12 1 1
135+
github.com/GolangToolKits/grrt/reqRouter.go:178.3,178.55 1 1
136+
github.com/GolangToolKits/grrt/reqRouter.go:182.55,186.28 4 1
137+
github.com/GolangToolKits/grrt/reqRouter.go:186.28,188.3 1 1
138+
github.com/GolangToolKits/grrt/reqRouter.go:189.2,189.30 1 1
139+
github.com/GolangToolKits/grrt/reqRouter.go:192.49,196.2 3 1
140+
github.com/GolangToolKits/grrt/reqRouter.go:198.55,201.17 3 1
141+
github.com/GolangToolKits/grrt/reqRouter.go:201.17,203.17 2 1
142+
github.com/GolangToolKits/grrt/reqRouter.go:203.17,205.4 1 1
143+
github.com/GolangToolKits/grrt/reqRouter.go:207.2,207.12 1 1
144+
github.com/GolangToolKits/grrt/reqRouter.go:275.70,282.15 7 1
145+
github.com/GolangToolKits/grrt/reqRouter.go:282.15,284.12 1 1
146+
github.com/GolangToolKits/grrt/reqRouter.go:284.12,285.9 1 1
147+
github.com/GolangToolKits/grrt/reqRouter.go:287.3,288.17 2 1
148+
github.com/GolangToolKits/grrt/reqRouter.go:288.17,289.28 1 1
149+
github.com/GolangToolKits/grrt/reqRouter.go:289.28,290.28 1 1
150+
github.com/GolangToolKits/grrt/reqRouter.go:290.28,292.44 2 1
151+
github.com/GolangToolKits/grrt/reqRouter.go:292.44,295.12 3 1
152+
github.com/GolangToolKits/grrt/reqRouter.go:297.11,297.36 1 1
153+
github.com/GolangToolKits/grrt/reqRouter.go:297.36,300.11 3 1
154+
github.com/GolangToolKits/grrt/reqRouter.go:303.9,306.15 3 1
155+
github.com/GolangToolKits/grrt/reqRouter.go:306.15,308.5 1 1
156+
github.com/GolangToolKits/grrt/reqRouter.go:309.4,309.32 1 1
157+
github.com/GolangToolKits/grrt/reqRouter.go:312.2,312.19 1 1
158+
github.com/GolangToolKits/grrt/reqRouter.go:315.96,317.36 2 1
159+
github.com/GolangToolKits/grrt/reqRouter.go:317.36,318.32 1 1
160+
github.com/GolangToolKits/grrt/reqRouter.go:318.32,320.4 1 1
161+
github.com/GolangToolKits/grrt/reqRouter.go:322.2,323.27 2 1
162+
github.com/GolangToolKits/grrt/reqRouter.go:326.52,329.13 3 1
163+
github.com/GolangToolKits/grrt/reqRouter.go:329.13,331.15 2 1
164+
github.com/GolangToolKits/grrt/reqRouter.go:331.15,333.4 1 1
165+
github.com/GolangToolKits/grrt/reqRouter.go:335.2,335.12 1 1
160166
github.com/GolangToolKits/grrt/router.go:29.25,34.2 4 1
161167
github.com/GolangToolKits/grrt/router.go:37.46,39.49 2 1
162168
github.com/GolangToolKits/grrt/router.go:39.49,41.3 1 1

reqRouter.go

Lines changed: 125 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,26 @@ func (t *ReqRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4444
// var rt = t.findPrefix(path)
4545
var rt Route
4646
// if rt == nil {
47-
frt, fvars := t.findRouteAndVars(path)
48-
rt = frt
49-
if len(*fvars) > 0 {
50-
r = t.requestWithVars(r, rt.GetVarNames(), fvars)
51-
// rt = frt
47+
sfile := t.isStaticFile(path)
48+
if !sfile {
49+
frt, fvars := t.findRouteAndVars(path)
50+
rt = frt
51+
if rt != nil && len(*fvars) > 0 {
52+
r = t.requestWithVars(r, rt.GetVarNames(), fvars)
53+
// rt = frt
54+
}
5255
}
56+
// frt, fvars := t.findRouteAndVars(path)
57+
// rt = frt
58+
// if rt != nil && len(*fvars) > 0 {
59+
// r = t.requestWithVars(r, rt.GetVarNames(), fvars)
60+
// // rt = frt
61+
// }
5362
// }
54-
if rt == nil {
63+
if rt == nil && !sfile {
5564
rt = t.findPrefix(path)
65+
} else if rt == nil && sfile {
66+
rt = t.findFilePrefix(path)
5667
}
5768
// rt = t.findPrefix(path)
5869
if rt == nil || !rt.IsActive() {
@@ -184,56 +195,118 @@ func (t *ReqRouter) findPrefix(px string) Route {
184195
return rtn
185196
}
186197

198+
func (t *ReqRouter) findFilePrefix(path string) Route {
199+
var rtn Route
200+
sp := strings.Split(path, "/")
201+
if len(sp) > 1 {
202+
rtn = t.prefixRoutes[sp[1]]
203+
if rtn == nil {
204+
rtn = t.prefixRoutes["/"]
205+
}
206+
}
207+
return rtn
208+
}
209+
210+
// func (t *ReqRouter) findRouteAndVars(path string) (Route, *[]string) {
211+
// var rnt Route
212+
// sp := strings.Split(path, "/")
213+
// var vars []string
214+
// // var vcnt = len(sp) - 2
215+
// var vl int
216+
// if len(sp) == 2 && sp[1] == "" {
217+
// vl = len(sp) - 2
218+
// } else {
219+
// vl = len(sp) - 1
220+
// }
221+
// var vcnt = vl
222+
// //log.Println("sp:", sp)
223+
// var found = false
224+
// var searchPath = ""
225+
// for i, p := range sp {
226+
// //if i == 0 {
227+
// //continue
228+
// if found {
229+
// break
230+
// }
231+
// // if i == 0 {
232+
// // continue
233+
// // } else if found {
234+
// // break
235+
// // }
236+
// // searchPath += "/" + p
237+
// if searchPath == "/" {
238+
// searchPath += p
239+
// } else {
240+
// searchPath += "/" + p
241+
// }
242+
// // searchPath += "/" + p
243+
// rts := t.namedRoutes[searchPath]
244+
// if rts != nil {
245+
// var baser = true
246+
// for _, rt := range *rts {
247+
// if rt.GetPath() != "/" {
248+
// baser = false
249+
// break
250+
// }
251+
// }
252+
253+
// for _, rt := range *rts {
254+
// if rt.GetPathVarsCount() == vcnt {
255+
// rnt = rt
256+
// found = true
257+
// vars = sp[i+1:]
258+
// if len(vars) == 1 && vars[0] == "" {
259+
// vars = []string{}
260+
// }
261+
// break
262+
// // } else if rt.GetPath() == "/" && !rt.IsPathVarsUsed() {
263+
// // } else if len(*rts) == 1 && rt.GetPath() == "/" && !rt.IsPathVarsUsed() {
264+
// } else if baser {
265+
// vcnt--
266+
// }
267+
// }
268+
// } else {
269+
// vcnt--
270+
// }
271+
// }
272+
// return rnt, &vars
273+
// }
274+
187275
func (t *ReqRouter) findRouteAndVars(path string) (Route, *[]string) {
188276
var rnt Route
189277
sp := strings.Split(path, "/")
278+
plen := len(sp)
190279
var vars []string
191-
// var vcnt = len(sp) - 2
192-
var vl int
193-
if len(sp) == 2 && sp[1] == "" {
194-
vl = len(sp) - 2
195-
} else {
196-
vl = len(sp) - 1
197-
}
198-
var vcnt = vl
199-
//log.Println("sp:", sp)
280+
var searchPath = path
200281
var found = false
201-
var searchPath = ""
202-
for i, p := range sp {
203-
//if i == 0 {
204-
//continue
282+
for range sp {
283+
//fmt.Println(i)
205284
if found {
206285
break
207286
}
208-
// if i == 0 {
209-
// continue
210-
// } else if found {
211-
// break
212-
// }
213-
// searchPath += "/" + p
214-
if searchPath == "/" {
215-
searchPath += p
216-
} else {
217-
searchPath += "/" + p
218-
}
219-
// searchPath += "/" + p
220287
rts := t.namedRoutes[searchPath]
221288
if rts != nil {
222289
for _, rt := range *rts {
223-
if rt.GetPathVarsCount() == vcnt {
224-
rnt = rt
225-
found = true
226-
vars = sp[i+1:]
227-
if len(vars) == 1 && vars[0] == "" {
228-
vars = []string{}
290+
if rt.IsPathVarsUsed() {
291+
vars = sp[plen:]
292+
if len(vars) == rt.GetPathVarsCount() {
293+
found = true
294+
rnt = rt
295+
break
229296
}
297+
} else if rt.GetPath() == path {
298+
found = true
299+
rnt = rt
230300
break
231-
} else if len(*rts) == 1 && rt.GetPath() == "/" && !rt.IsPathVarsUsed() {
232-
vcnt--
233301
}
234302
}
235303
} else {
236-
vcnt--
304+
plen--
305+
si := strings.LastIndex(searchPath, "/")
306+
if si == 0 {
307+
si++
308+
}
309+
searchPath = searchPath[:si]
237310
}
238311
}
239312
return rnt, &vars
@@ -249,3 +322,15 @@ func (t *ReqRouter) requestWithVars(r *http.Request, pVarNames, pvars *[]string)
249322
ctx := context.WithValue(r.Context(), varsKey, vars)
250323
return r.WithContext(ctx)
251324
}
325+
326+
func (t *ReqRouter) isStaticFile(path string) bool {
327+
var rtn bool
328+
var ind = strings.LastIndex(path, ".")
329+
if ind > 0 {
330+
disp := len(path) - (ind + 1)
331+
if disp < 4 {
332+
rtn = true
333+
}
334+
}
335+
return rtn
336+
}

0 commit comments

Comments
 (0)