/
echo.go
1096 lines (955 loc) · 22.5 KB
/
echo.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 xgfone
//
// 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 echo supplies a customized Router implementation by referring to
// github.com/labstack/echo.
package echo
import (
"bytes"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
)
// PROPFIND Method can be used on collection and property resources.
const PROPFIND = "PROPFIND"
// REPORT Method can be used to get information about a resource, see rfc 3253
const REPORT = "REPORT"
// removeTrailingSlash is used to replace the stdlib fucntion strings.TrimRight
// to improve the performance when finding the handler by the route path.
func removeTrailingSlash(path string) string {
_len := len(path) - 1
var i int
for i = _len; i >= 0; i-- {
if path[i] != '/' {
break
}
}
if i == _len {
return path
} else if i < 0 {
return ""
}
return path[:i+1]
}
type methodHandler struct {
get interface{}
put interface{}
post interface{}
head interface{}
patch interface{}
trace interface{}
delete interface{}
options interface{}
connect interface{}
propfind interface{}
report interface{}
}
func newMethodHandler() *methodHandler { return &methodHandler{} }
func (mh *methodHandler) Range(f func(string, interface{})) {
if mh.get != nil {
f(http.MethodGet, mh.get)
}
if mh.put != nil {
f(http.MethodPut, mh.put)
}
if mh.post != nil {
f(http.MethodPost, mh.post)
}
if mh.head != nil {
f(http.MethodHead, mh.head)
}
if mh.patch != nil {
f(http.MethodPatch, mh.patch)
}
if mh.trace != nil {
f(http.MethodTrace, mh.trace)
}
if mh.delete != nil {
f(http.MethodDelete, mh.delete)
}
if mh.options != nil {
f(http.MethodOptions, mh.options)
}
if mh.connect != nil {
f(http.MethodConnect, mh.connect)
}
if mh.propfind != nil {
f(PROPFIND, mh.propfind)
}
if mh.report != nil {
f(REPORT, mh.report)
}
}
func (mh *methodHandler) Methods() []string {
ms := make([]string, 0, 11)
mh.Range(func(method string, _ interface{}) { ms = append(ms, method) })
return ms
}
func (mh *methodHandler) DelHandler(method string) { mh.AddHandler(method, nil) }
func (mh *methodHandler) AddHandler(method string, handler interface{}) {
switch method {
case "": // For Any Method
*mh = methodHandler{
get: handler,
put: handler,
post: handler,
head: handler,
patch: handler,
trace: handler,
delete: handler,
options: handler,
connect: handler,
propfind: handler,
report: handler,
}
case http.MethodGet:
mh.get = handler
case http.MethodPut:
mh.put = handler
case http.MethodPost:
mh.post = handler
case http.MethodHead:
mh.head = handler
case http.MethodPatch:
mh.patch = handler
case http.MethodDelete:
mh.delete = handler
case http.MethodOptions:
mh.options = handler
case http.MethodConnect:
mh.connect = handler
case http.MethodTrace:
mh.trace = handler
case PROPFIND:
mh.propfind = handler
case REPORT:
mh.report = handler
}
}
func (mh *methodHandler) FindHandler(method string) interface{} {
switch method {
case http.MethodGet:
return mh.get
case http.MethodPut:
return mh.put
case http.MethodPost:
return mh.post
case http.MethodHead:
return mh.head
case http.MethodPatch:
return mh.patch
case http.MethodDelete:
return mh.delete
case http.MethodOptions:
return mh.options
case http.MethodConnect:
return mh.connect
case http.MethodTrace:
return mh.trace
case PROPFIND:
return mh.propfind
case REPORT:
return mh.report
default:
return nil
}
}
func (mh *methodHandler) HasHandler() bool {
if mh.get != nil {
return true
} else if mh.put != nil {
return true
} else if mh.post != nil {
return true
} else if mh.head != nil {
return true
} else if mh.patch != nil {
return true
} else if mh.delete != nil {
return true
} else if mh.options != nil {
return true
} else if mh.connect != nil {
return true
} else if mh.trace != nil {
return true
} else if mh.propfind != nil {
return true
} else if mh.report != nil {
return true
}
return false
}
/// *********************************************************************** ///
const (
skind kind = iota
pkind
akind
)
type kind uint8
func (k kind) String() string {
switch k {
case skind:
return "static"
case pkind:
return "param"
case akind:
return "any"
default:
panic("unknown node kind")
}
}
/// *********************************************************************** ///
type node struct {
name string // The name of the node
ppath string // The Pristine full path
kind kind // The kind of the current node
label byte // The first byte of the prefix
prefix string // The same prefix of the paths of all the children
pnames []string // The parameters in the registered path
children []*node
handlers *methodHandler
parent *node
}
func newNode(t kind, name, prefix, ppath string, parent *node, children []*node,
mh *methodHandler, pnames []string) *node {
n := &node{
kind: t,
name: name,
label: prefix[0],
prefix: prefix,
parent: parent,
children: children,
ppath: ppath,
pnames: pnames,
handlers: mh,
}
// Fix the parent node of the children nodes.
for _, c := range n.children {
c.parent = n
}
return n
}
func (n *node) Reset() {
n.name = ""
n.ppath = ""
n.pnames = nil
*n.handlers = methodHandler{}
}
func (n *node) AddChild(c *node) { n.children = append(n.children, c) }
func (n *node) DelChild(c *node) {
for i, cn := range n.children {
if cn == c {
copy(n.children[i:], n.children[i+1:])
n.children = n.children[:len(n.children)-1]
break
}
}
}
func (n *node) FindChildByLabel(label byte) *node {
for _, c := range n.children {
if c.label == label {
return c
}
}
return nil
}
func (n *node) FindChild(label byte, t kind) *node {
for _, c := range n.children {
if c.label == label && c.kind == t {
return c
}
}
return nil
}
func (n *node) FindChildByKind(t kind) *node {
for _, c := range n.children {
if c.kind == t {
return c
}
}
return nil
}
func (n *node) CheckMethodNotAllowed(r *Router) interface{} {
if r.conf.MethodNotAllowedHandler == nil || !n.handlers.HasHandler() {
return r.conf.NotFoundHandler
}
if f, ok := r.conf.MethodNotAllowedHandler.(func([]string) interface{}); ok {
return f(n.handlers.Methods())
}
return r.conf.MethodNotAllowedHandler
}
/// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var errInconsistentRouteName = fmt.Errorf("inconsistent route name")
// Config is used to configure the router.
type Config struct {
// NotFoundHandler is returned when not finding the route handler.
//
// Default: nil
NotFoundHandler interface{}
// MethodNotAllowedHandler is returned when not finding the route handler
// with the given method and path, which will be called with the allowed
// methods if it is the function func(allowedMethods []string) interface{}.
//
// Default: nil
MethodNotAllowedHandler interface{}
// If true, the trailing slash will be removed before adding, deleting
// and finding the route.
//
// Default: false.
RemoveTrailingSlash bool
}
// Router is the registry of all registered routes to match the request
// with the url method and path, which supports one or more path parameters.
//
// For the single parameter, it starts with ":" followed by the parameter name,
// such as "/v1/:first/path/:second/to/:third".
//
// For the wildcard parameter, its starts with "*" followed by the optional
// parameter name. If no the parameter name, it is "*" by default. such as
// "/v1/path/to/*" or "/v1/path/to/*wildcard".
//
// Moreover, the single and wildcard parameters may used in combination.
// But the wildcard parameter must be the last.
//
// Supported methods:
// - GET
// - PUT
// - HEAD
// - POST
// - PATCH
// - TRACE
// - DELETE
// - CONNECT
// - OPTIONS
// - PROPFIND
// - REPORT
//
type Router struct {
conf Config
tree *node
bufpool sync.Pool
maxnum int // The maximum number of the parameter
routes map[string]string // Name -> Path
}
// NewRouter returns a new Router instance with the config.
//
// If c is nil, use the default configuration.
func NewRouter(c *Config) *Router {
var conf Config
if c != nil {
conf = *c
}
return &Router{
conf: conf,
tree: &node{handlers: new(methodHandler)},
routes: make(map[string]string, 32),
bufpool: sync.Pool{New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 64))
}},
}
}
// Path returns a url path by the path name and the parameters.
func (r *Router) Path(name string, params ...interface{}) (url string) {
path := r.routes[name]
if path == "" {
return ""
}
n := 0
ln := len(params)
buf := r.bufpool.Get().(*bytes.Buffer)
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' && n < ln {
for ; i < l && path[i] != '/'; i++ {
}
switch v := params[n].(type) {
case error:
buf.WriteString(v.Error())
case fmt.Stringer:
buf.WriteString(v.String())
case io.WriterTo:
v.WriteTo(buf)
case float32:
buf.WriteString(strconv.FormatFloat(float64(v), 'f', -1, 32))
case float64:
buf.WriteString(strconv.FormatFloat(v, 'f', -1, 32))
default:
fmt.Fprintf(buf, "%v", v)
}
n++
}
if i < l {
buf.WriteByte(path[i])
}
}
url = buf.String()
buf.Reset()
r.bufpool.Put(buf)
return
}
// Range traverses all the registered routes.
func (r *Router) Range(f func(name, path, method string, handler interface{})) {
r.rangef(r.tree, f)
}
func (r *Router) rangef(n *node, f func(string, string, string, interface{})) {
if n.ppath != "" {
n.handlers.Range(func(method string, handler interface{}) {
f(n.name, n.ppath, method, handler)
})
}
for i, _len := 0, len(n.children); i < _len; i++ {
r.rangef(n.children[i], f)
}
}
/// ----------------------------------------------------------------------- ///
// Add registers a new route for method and path with matching handler.
//
// If method is empty, it'll override the handlers of all the supported methods
// with h.
func (r *Router) Add(name, path, method string, h interface{}) (n int, err error) {
if h == nil {
return 0, fmt.Errorf("route handler must not be nil")
}
// Validate path
if r.conf.RemoveTrailingSlash {
path = strings.TrimRight(path, "/")
}
if path == "" {
path = "/"
}
if path[0] != '/' {
path = "/" + path
}
var addRoute bool
if name != "" {
if orig, ok := r.routes[name]; ok {
if orig != path {
return 0, errInconsistentRouteName
}
} else {
addRoute = true
}
for n, p := range r.routes {
if p == path {
if n != name {
return 0, errInconsistentRouteName
}
break
}
}
}
notend := true
pnames := []string{} // Param names
ppath := path // Pristine path
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' {
j := i + 1
r.insert("", method, "", path[:i], skind, nil, nil)
for ; i < l && path[i] != '/'; i++ {
}
pnames = append(pnames, path[j:i])
path = path[:j] + path[i:]
i, l = j, len(path)
if i == l {
r.insert(name, method, ppath, path[:i], pkind, h, pnames)
notend = false
break
} else {
r.insert("", method, "", path[:i], pkind, nil, nil)
}
} else if path[i] == '*' {
r.insert("", method, "", path[:i], skind, nil, nil)
name := strings.TrimRight(path[i+1:], "/ ")
if name == "" {
name = "*"
}
pnames = append(pnames, name)
r.insert(name, method, ppath, path[:i+1], akind, h, pnames)
notend = false
break
}
}
if notend {
r.insert(name, method, ppath, path, skind, h, pnames)
}
if addRoute {
r.routes[name] = ppath
}
return r.maxnum, nil
}
func (r *Router) insert(name, method, ppath, prefix string,
t kind, h interface{}, pnames []string) {
// Adjust max param
l := len(pnames)
if r.maxnum < l {
r.maxnum = l
}
cn := r.tree
search := prefix
for {
sl := len(search)
pl := len(cn.prefix)
l := 0
// LCP: Longest Common Prefix
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
if l == 0 { // No Common Prefix, only for the first route.
// At root node
cn.label = search[0]
cn.prefix = search
if h != nil {
cn.kind = t
cn.name = name
cn.ppath = ppath
cn.pnames = pnames
cn.handlers.AddHandler(method, h)
}
} else if l < pl {
// The inserted path is the full LCP of the current node.
// Split node
n := newNode(cn.kind, cn.name, cn.prefix[l:], cn.ppath, cn,
cn.children, cn.handlers, cn.pnames)
// Reset parent node
cn.name = ""
cn.kind = skind
cn.ppath = ""
cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l]
cn.pnames = nil
cn.children = []*node{n}
cn.handlers = newMethodHandler()
if l == sl {
// At parent node, that's, the inserted path is the new parent node.
cn.kind = t
cn.name = name
cn.ppath = ppath
cn.pnames = pnames
cn.handlers.AddHandler(method, h)
} else {
// Create child node, that's, the path of the new parent node
// is the full LCP of the inserted path.
n = newNode(t, name, search[l:], ppath, cn, nil,
newMethodHandler(), pnames)
n.handlers.AddHandler(method, h)
cn.AddChild(n)
}
} else if l < sl {
// The path of the current node is the full LCP of the inserted path.
search = search[l:]
c := cn.FindChildByLabel(search[0])
if c != nil {
// Go deeper
cn = c
continue
}
// Create child node
n := newNode(t, name, search, ppath, cn, nil,
newMethodHandler(), pnames)
n.handlers.AddHandler(method, h)
cn.AddChild(n)
} else {
// Node already exists, that's, the insert path is the current node.
// We override it with the new inserted route, but the parameters.
if h != nil {
cn.ppath = ppath
cn.handlers.AddHandler(method, h)
if len(cn.pnames) == 0 { // Issue #729
cn.pnames = pnames
}
if cn.name == "" {
cn.name = name
}
}
}
return
}
}
// Match lookups a handler registered for method and path,
// which also parses the path for the parameters.
func (r *Router) Match(path, method string, pnames, pvalues []string) (
h interface{}, pn int) {
if r.conf.RemoveTrailingSlash {
// path = strings.TrimRight(path, "/")
path = removeTrailingSlash(path)
}
if path == "" {
path = "/"
}
var (
hasp = len(pnames) > 0 && len(pvalues) > 0
cn = r.tree
search = path
child *node // Child node
n int // Param counter
nk kind // Next kind
nn *node // Next node
ns string // Next search
)
// Search order static > param > any
for {
if search == "" {
break
}
pl := 0 // Prefix length
l := 0 // LCP length
if cn.label != ':' {
pl = len(cn.prefix)
// LCP
max := pl
if sl := len(search); sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
}
if l == pl {
// Continue search
search = search[l:]
} else {
if nn == nil { // Issue #1348
return r.conf.NotFoundHandler, 0 // Not found
}
cn = nn
search = ns
switch nk {
case pkind:
goto Param
case akind:
goto Any
}
}
if search == "" {
break
}
// Search Static Node
if child = cn.FindChild(search[0], skind); child != nil {
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nn = cn // For backtrack
nk = pkind
ns = search
}
cn = child
continue
}
// Search Param Node
Param:
if child = cn.FindChildByKind(pkind); child != nil {
if hasp && len(pvalues) == n { // Issue #378
continue
}
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nk = akind
nn = cn
ns = search
}
cn = child
var i int
for l := len(search); i < l && search[i] != '/'; i++ {
}
if hasp {
pvalues[n] = search[:i]
}
n++
search = search[i:]
continue
}
// Search Any Node
Any:
if cn = cn.FindChildByKind(akind); cn == nil {
if nn != nil {
cn = nn
nn = cn.parent // Next (Issue #954)
if nn != nil {
nk = nn.kind
}
search = ns
switch nk {
case pkind:
goto Param
case akind:
goto Any
}
}
return r.conf.NotFoundHandler, 0 // Not found
}
if hasp {
pvalues[len(cn.pnames)-1] = search
}
break
}
if h = cn.handlers.FindHandler(method); h == nil { // NOTE: Slow zone...
// Dig further for any, might have an empty value for *,
// e.g. serving a directory. Issue #207.
if n := cn.FindChildByKind(akind); n == nil {
h = cn.CheckMethodNotAllowed(r)
} else {
if h = n.handlers.FindHandler(method); h == nil {
h = n.CheckMethodNotAllowed(r)
}
if pn = len(n.pnames); pn > 0 && hasp {
copy(pnames, n.pnames)
pvalues[pn-1] = ""
}
}
} else if pn = len(cn.pnames); pn > 0 && hasp {
copy(pnames, cn.pnames)
}
return
}
/// ----------------------------------------------------------------------- ///
// Del deletes the given route.
func (r *Router) Del(path, method string) (err error) {
if path != "" {
err = r.delRoute(path, method)
}
return
}
func (r *Router) delRoute(path, method string) (err error) {
if r.conf.RemoveTrailingSlash {
// path = strings.TrimRight(path, "/")
path = removeTrailingSlash(path)
}
if path == "" {
path = "/"
}
var (
cn = r.tree
search = path
child *node // Child node
nk kind // Next kind
nn *node // Next node
ns string // Next search
)
// Search order static > param > any
for {
if search == "" {
break
}
pl := 0 // Prefix length
l := 0 // LCP length
if cn.label != ':' {
pl = len(cn.prefix)
// LCP
max := pl
if sl := len(search); sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
}
if l == pl {
// Continue search
search = search[l:]
} else {
if nn == nil { // Issue #1348
return // Not found
}
cn = nn
search = ns
switch nk {
case pkind:
goto Param
case akind:
goto Any
}
}
if search == "" {
break
}
// Search Static Node
if child = cn.FindChild(search[0], skind); child != nil {
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nn = cn // For backtrack
nk = pkind
ns = search
}
cn = child
continue
}
// Search Param Node
Param:
if child = cn.FindChildByKind(pkind); child != nil {
// Save next
if cn.prefix[len(cn.prefix)-1] == '/' { // Issue #623
nn = cn // For backtrack
nk = akind
ns = search
}
cn = child
var i int
for _len := len(search); i < _len && search[i] != '/'; i++ {
}
search = search[i:]
continue
}
// Search Any Node
Any:
if cn = cn.FindChildByKind(akind); cn == nil {
if nn != nil {
cn = nn
nn = cn.parent // Next (Issue #954)
if nn != nil {
nk = nn.kind
}
search = ns
switch nk {
case pkind:
goto Param
case akind:
goto Any
}
}
return // Not found
}
break
}
// Delete the found node.
r.removeNode(cn, method)
return
}
func (r *Router) removeNode(cn *node, method string) {
if cn == nil {
return
}
if method != "" {
cn.handlers.DelHandler(method)
if cn.handlers.HasHandler() {
return
}
}
r.removeWholeNode(cn)
}
func (r *Router) replaceParentWithChild(cn *node) {
child := cn.children[0]
if cn.parent == nil {
// The removed node is root and use the child to replace it.
prefix := cn.prefix
r.tree = child
r.tree.parent = nil
r.tree.prefix = prefix + r.tree.prefix
r.tree.label = r.tree.prefix[0]
r.removeRouteNameByPath(cn.ppath)
} else {
parent := cn.parent
child.parent = parent
child.prefix = cn.prefix + child.prefix
child.label = child.prefix[0]
parent.DelChild(cn)
parent.AddChild(child)
r.removeRouteNameByPath(cn.ppath)
}
}
func (r *Router) removeRouteNameByPath(path string) {
if path != "" {
for n, p := range r.routes {
if p == path {
delete(r.routes, n)
break
}
}
}