-
Notifications
You must be signed in to change notification settings - Fork 238
/
routers.go
1664 lines (1439 loc) · 37.2 KB
/
routers.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 2014 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"runtime"
// If you add new routers please:
// - Keep the benchmark functions etc. alphabetically sorted
// - Make a pull request (without benchmark results) at
// https://github.com/julienschmidt/go-http-routing-benchmark
"github.com/ant0ine/go-json-rest/rest"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/bmizerany/pat"
"github.com/go-playground/lars"
// "github.com/daryl/zeus"
cloudykitrouter "github.com/cloudykit/router"
"github.com/dimfeld/httptreemux"
"github.com/emicklei/go-restful"
"github.com/gin-gonic/gin"
"github.com/go-chi/chi"
"github.com/go-martini/martini"
"github.com/go-zoo/bone"
"github.com/gocraft/web"
"github.com/gorilla/mux"
gowwwrouter "github.com/gowww/router"
"github.com/julienschmidt/httprouter"
"github.com/labstack/echo/v4"
llog "github.com/lunny/log"
"github.com/lunny/tango"
vulcan "github.com/mailgun/route"
"github.com/mikespook/possum"
possumrouter "github.com/mikespook/possum/router"
possumview "github.com/mikespook/possum/view"
"github.com/naoina/denco"
urlrouter "github.com/naoina/kocha-urlrouter"
_ "github.com/naoina/kocha-urlrouter/doublearray"
"github.com/pilu/traffic"
"github.com/plimble/ace"
"github.com/rcrowley/go-tigertonic"
// "github.com/revel/pathtree"
// "github.com/revel/revel"
"github.com/aerogo/aero"
"github.com/typepress/rivet"
"github.com/ursiform/bear"
"github.com/vanng822/r2router"
goji "github.com/zenazn/goji/web"
gojiv2 "goji.io"
gojiv2pat "goji.io/pat"
"gopkg.in/macaron.v1"
)
type route struct {
method string
path string
}
type mockResponseWriter struct{}
func (m *mockResponseWriter) Header() (h http.Header) {
return http.Header{}
}
func (m *mockResponseWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (m *mockResponseWriter) WriteString(s string) (n int, err error) {
return len(s), nil
}
func (m *mockResponseWriter) WriteHeader(int) {}
var nullLogger *log.Logger
// flag indicating if the normal or the test handler should be loaded
var loadTestHandler = false
func init() {
// beego sets it to runtime.NumCPU()
// Currently none of the contesters does concurrent routing
runtime.GOMAXPROCS(1)
// makes logging 'webscale' (ignores them)
log.SetOutput(new(mockResponseWriter))
nullLogger = log.New(new(mockResponseWriter), "", 0)
initBeego()
initGin()
initMartini()
// initRevel()
initTango()
initTraffic()
}
// Common
func httpHandlerFunc(_ http.ResponseWriter, _ *http.Request) {}
func httpHandlerFuncTest(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, r.RequestURI)
}
// Ace
func aceHandle(_ *ace.C) {}
func aceHandleWrite(c *ace.C) {
io.WriteString(c.Writer, c.Param("name"))
}
func aceHandleTest(c *ace.C) {
io.WriteString(c.Writer, c.Request.RequestURI)
}
func loadAce(routes []route) http.Handler {
h := []ace.HandlerFunc{aceHandle}
if loadTestHandler {
h = []ace.HandlerFunc{aceHandleTest}
}
router := ace.New()
for _, route := range routes {
router.Handle(route.method, route.path, h)
}
return router
}
func loadAceSingle(method, path string, handle ace.HandlerFunc) http.Handler {
router := ace.New()
router.Handle(method, path, []ace.HandlerFunc{handle})
return router
}
// Aero
func aeroHandler(c aero.Context) error {
return nil
}
func aeroHandlerWrite(ctx aero.Context) error {
io.WriteString(ctx.Response().Internal(), ctx.Get("name"))
return nil
}
func aeroHandlerTest(ctx aero.Context) error {
io.WriteString(ctx.Response().Internal(), ctx.Request().Path())
return nil
}
func loadAero(routes []route) http.Handler {
var h aero.Handler = aeroHandler
if loadTestHandler {
h = aeroHandlerTest
}
app := aero.New()
for _, r := range routes {
switch r.method {
case "GET":
app.Get(r.path, h)
case "POST":
app.Post(r.path, h)
case "PUT":
app.Put(r.path, h)
case "PATCH":
app.Router().Add(http.MethodPatch, r.path, h)
case "DELETE":
app.Delete(r.path, h)
default:
panic("Unknow HTTP method: " + r.method)
}
}
return app
}
func loadAeroSingle(method, path string, h aero.Handler) http.Handler {
app := aero.New()
switch method {
case "GET":
app.Get(path, h)
case "POST":
app.Post(path, h)
case "PUT":
app.Put(path, h)
case "PATCH":
app.Router().Add(http.MethodPatch, path, h)
case "DELETE":
app.Delete(path, h)
default:
panic("Unknow HTTP method: " + method)
}
// }
return app
}
// bear
func bearHandler(_ http.ResponseWriter, _ *http.Request, _ *bear.Context) {}
func bearHandlerWrite(w http.ResponseWriter, _ *http.Request, ctx *bear.Context) {
io.WriteString(w, ctx.Params["name"])
}
func bearHandlerTest(w http.ResponseWriter, r *http.Request, _ *bear.Context) {
io.WriteString(w, r.RequestURI)
}
func loadBear(routes []route) http.Handler {
h := bearHandler
if loadTestHandler {
h = bearHandlerTest
}
router := bear.New()
re := regexp.MustCompile(":([^/]*)")
for _, route := range routes {
switch route.method {
case "GET", "POST", "PUT", "PATCH", "DELETE":
router.On(route.method, re.ReplaceAllString(route.path, "{$1}"), h)
default:
panic("Unknown HTTP method: " + route.method)
}
}
return router
}
func loadBearSingle(method string, path string, handler bear.HandlerFunc) http.Handler {
router := bear.New()
switch method {
case "GET", "POST", "PUT", "PATCH", "DELETE":
router.On(method, path, handler)
default:
panic("Unknown HTTP method: " + method)
}
return router
}
// beego
func beegoHandler(ctx *context.Context) {}
func beegoHandlerWrite(ctx *context.Context) {
ctx.WriteString(ctx.Input.Param(":name"))
}
func beegoHandlerTest(ctx *context.Context) {
ctx.WriteString(ctx.Request.RequestURI)
}
func initBeego() {
beego.BConfig.RunMode = beego.PROD
beego.BeeLogger.Close()
}
func loadBeego(routes []route) http.Handler {
h := beegoHandler
if loadTestHandler {
h = beegoHandlerTest
}
re := regexp.MustCompile(":([^/]*)")
app := beego.NewControllerRegister()
for _, route := range routes {
route.path = re.ReplaceAllString(route.path, ":$1")
switch route.method {
case "GET":
app.Get(route.path, h)
case "POST":
app.Post(route.path, h)
case "PUT":
app.Put(route.path, h)
case "PATCH":
app.Patch(route.path, h)
case "DELETE":
app.Delete(route.path, h)
default:
panic("Unknow HTTP method: " + route.method)
}
}
return app
}
func loadBeegoSingle(method, path string, handler beego.FilterFunc) http.Handler {
app := beego.NewControllerRegister()
switch method {
case "GET":
app.Get(path, handler)
case "POST":
app.Post(path, handler)
case "PUT":
app.Put(path, handler)
case "PATCH":
app.Patch(path, handler)
case "DELETE":
app.Delete(path, handler)
default:
panic("Unknow HTTP method: " + method)
}
return app
}
// bone
func boneHandlerWrite(rw http.ResponseWriter, req *http.Request) {
io.WriteString(rw, bone.GetValue(req, "name"))
}
func loadBone(routes []route) http.Handler {
h := http.HandlerFunc(httpHandlerFunc)
if loadTestHandler {
h = http.HandlerFunc(httpHandlerFuncTest)
}
router := bone.New()
for _, route := range routes {
switch route.method {
case "GET":
router.Get(route.path, h)
case "POST":
router.Post(route.path, h)
case "PUT":
router.Put(route.path, h)
case "PATCH":
router.Patch(route.path, h)
case "DELETE":
router.Delete(route.path, h)
default:
panic("Unknow HTTP method: " + route.method)
}
}
return router
}
func loadBoneSingle(method, path string, handler http.Handler) http.Handler {
router := bone.New()
switch method {
case "GET":
router.Get(path, handler)
case "POST":
router.Post(path, handler)
case "PUT":
router.Put(path, handler)
case "PATCH":
router.Patch(path, handler)
case "DELETE":
router.Delete(path, handler)
default:
panic("Unknow HTTP method: " + method)
}
return router
}
// chi
// chi
func chiHandleWrite(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, chi.URLParam(r, "name"))
}
func loadChi(routes []route) http.Handler {
h := httpHandlerFunc
if loadTestHandler {
h = httpHandlerFuncTest
}
re := regexp.MustCompile(":([^/]*)")
mux := chi.NewRouter()
for _, route := range routes {
path := re.ReplaceAllString(route.path, "{$1}")
switch route.method {
case "GET":
mux.Get(path, h)
case "POST":
mux.Post(path, h)
case "PUT":
mux.Put(path, h)
case "PATCH":
mux.Patch(path, h)
case "DELETE":
mux.Delete(path, h)
default:
panic("Unknown HTTP method: " + route.method)
}
}
return mux
}
func loadChiSingle(method, path string, handler http.HandlerFunc) http.Handler {
mux := chi.NewRouter()
switch method {
case "GET":
mux.Get(path, handler)
case "POST":
mux.Post(path, handler)
case "PUT":
mux.Put(path, handler)
case "PATCH":
mux.Patch(path, handler)
case "DELETE":
mux.Delete(path, handler)
default:
panic("Unknown HTTP method: " + method)
}
return mux
}
// CloudyKit Router
func cloudyKitRouterHandler(_ http.ResponseWriter, _ *http.Request, _ cloudykitrouter.Parameter) {}
func cloudyKitRouterHandlerWrite(w http.ResponseWriter, _ *http.Request, ps cloudykitrouter.Parameter) {
io.WriteString(w, ps.ByName("name"))
}
func cloudyKitRouterHandlerTest(w http.ResponseWriter, r *http.Request, _ cloudykitrouter.Parameter) {
io.WriteString(w, r.RequestURI)
}
func loadCloudyKitRouter(routes []route) http.Handler {
h := cloudyKitRouterHandler
if loadTestHandler {
h = cloudyKitRouterHandlerTest
}
router := cloudykitrouter.New()
for _, route := range routes {
router.AddRoute(route.method, route.path, h)
}
return router
}
func loadCloudyKitRouterSingle(method, path string, handler cloudykitrouter.Handler) http.Handler {
router := cloudykitrouter.New()
router.AddRoute(method, path, handler)
return router
}
// Denco
func dencoHandler(w http.ResponseWriter, r *http.Request, params denco.Params) {}
func dencoHandlerWrite(w http.ResponseWriter, r *http.Request, params denco.Params) {
io.WriteString(w, params.Get("name"))
}
func dencoHandlerTest(w http.ResponseWriter, r *http.Request, params denco.Params) {
io.WriteString(w, r.RequestURI)
}
func loadDenco(routes []route) http.Handler {
h := dencoHandler
if loadTestHandler {
h = dencoHandlerTest
}
mux := denco.NewMux()
handlers := make([]denco.Handler, 0, len(routes))
for _, route := range routes {
handler := mux.Handler(route.method, route.path, h)
handlers = append(handlers, handler)
}
handler, err := mux.Build(handlers)
if err != nil {
panic(err)
}
return handler
}
func loadDencoSingle(method, path string, h denco.HandlerFunc) http.Handler {
mux := denco.NewMux()
handler, err := mux.Build([]denco.Handler{mux.Handler(method, path, h)})
if err != nil {
panic(err)
}
return handler
}
// Echo
func echoHandler(c echo.Context) error {
return nil
}
func echoHandlerWrite(c echo.Context) error {
io.WriteString(c.Response(), c.Param("name"))
return nil
}
func echoHandlerTest(c echo.Context) error {
io.WriteString(c.Response(), c.Request().RequestURI)
return nil
}
func loadEcho(routes []route) http.Handler {
var h echo.HandlerFunc = echoHandler
if loadTestHandler {
h = echoHandlerTest
}
e := echo.New()
for _, r := range routes {
switch r.method {
case "GET":
e.GET(r.path, h)
case "POST":
e.POST(r.path, h)
case "PUT":
e.PUT(r.path, h)
case "PATCH":
e.PATCH(r.path, h)
case "DELETE":
e.DELETE(r.path, h)
default:
panic("Unknow HTTP method: " + r.method)
}
}
return e
}
func loadEchoSingle(method, path string, h echo.HandlerFunc) http.Handler {
e := echo.New()
switch method {
case "GET":
e.GET(path, h)
case "POST":
e.POST(path, h)
case "PUT":
e.PUT(path, h)
case "PATCH":
e.PATCH(path, h)
case "DELETE":
e.DELETE(path, h)
default:
panic("Unknow HTTP method: " + method)
}
return e
}
// Gin
func ginHandle(_ *gin.Context) {}
func ginHandleWrite(c *gin.Context) {
io.WriteString(c.Writer, c.Params.ByName("name"))
}
func ginHandleTest(c *gin.Context) {
io.WriteString(c.Writer, c.Request.RequestURI)
}
func initGin() {
gin.SetMode(gin.ReleaseMode)
}
func loadGin(routes []route) http.Handler {
h := ginHandle
if loadTestHandler {
h = ginHandleTest
}
router := gin.New()
for _, route := range routes {
router.Handle(route.method, route.path, h)
}
return router
}
func loadGinSingle(method, path string, handle gin.HandlerFunc) http.Handler {
router := gin.New()
router.Handle(method, path, handle)
return router
}
// gocraft/web
type gocraftWebContext struct{}
func gocraftWebHandler(w web.ResponseWriter, r *web.Request) {}
func gocraftWebHandlerWrite(w web.ResponseWriter, r *web.Request) {
io.WriteString(w, r.PathParams["name"])
}
func gocraftWebHandlerTest(w web.ResponseWriter, r *web.Request) {
io.WriteString(w, r.RequestURI)
}
func loadGocraftWeb(routes []route) http.Handler {
h := gocraftWebHandler
if loadTestHandler {
h = gocraftWebHandlerTest
}
router := web.New(gocraftWebContext{})
for _, route := range routes {
switch route.method {
case "GET":
router.Get(route.path, h)
case "POST":
router.Post(route.path, h)
case "PUT":
router.Put(route.path, h)
case "PATCH":
router.Patch(route.path, h)
case "DELETE":
router.Delete(route.path, h)
default:
panic("Unknow HTTP method: " + route.method)
}
}
return router
}
func loadGocraftWebSingle(method, path string, handler interface{}) http.Handler {
router := web.New(gocraftWebContext{})
switch method {
case "GET":
router.Get(path, handler)
case "POST":
router.Post(path, handler)
case "PUT":
router.Put(path, handler)
case "PATCH":
router.Patch(path, handler)
case "DELETE":
router.Delete(path, handler)
default:
panic("Unknow HTTP method: " + method)
}
return router
}
// goji
func gojiFuncWrite(c goji.C, w http.ResponseWriter, r *http.Request) {
io.WriteString(w, c.URLParams["name"])
}
func loadGoji(routes []route) http.Handler {
h := httpHandlerFunc
if loadTestHandler {
h = httpHandlerFuncTest
}
mux := goji.New()
for _, route := range routes {
switch route.method {
case "GET":
mux.Get(route.path, h)
case "POST":
mux.Post(route.path, h)
case "PUT":
mux.Put(route.path, h)
case "PATCH":
mux.Patch(route.path, h)
case "DELETE":
mux.Delete(route.path, h)
default:
panic("Unknown HTTP method: " + route.method)
}
}
return mux
}
func loadGojiSingle(method, path string, handler interface{}) http.Handler {
mux := goji.New()
switch method {
case "GET":
mux.Get(path, handler)
case "POST":
mux.Post(path, handler)
case "PUT":
mux.Put(path, handler)
case "PATCH":
mux.Patch(path, handler)
case "DELETE":
mux.Delete(path, handler)
default:
panic("Unknow HTTP method: " + method)
}
return mux
}
// goji v2 (github.com/goji/goji)
func gojiv2Handler(w http.ResponseWriter, r *http.Request) {}
func gojiv2HandlerWrite(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, gojiv2pat.Param(r, "name"))
}
func gojiv2HandlerTest(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, r.RequestURI)
}
func loadGojiv2(routes []route) http.Handler {
h := gojiv2Handler
if loadTestHandler {
h = gojiv2HandlerTest
}
mux := gojiv2.NewMux()
for _, route := range routes {
switch route.method {
case "GET":
mux.HandleFunc(gojiv2pat.Get(route.path), h)
case "POST":
mux.HandleFunc(gojiv2pat.Post(route.path), h)
case "PUT":
mux.HandleFunc(gojiv2pat.Put(route.path), h)
case "PATCH":
mux.HandleFunc(gojiv2pat.Patch(route.path), h)
case "DELETE":
mux.HandleFunc(gojiv2pat.Delete(route.path), h)
default:
panic("Unknown HTTP method: " + route.method)
}
}
return mux
}
func loadGojiv2Single(method, path string, handler func(http.ResponseWriter, *http.Request)) http.Handler {
mux := gojiv2.NewMux()
switch method {
case "GET":
mux.HandleFunc(gojiv2pat.Get(path), handler)
case "POST":
mux.HandleFunc(gojiv2pat.Post(path), handler)
case "PUT":
mux.HandleFunc(gojiv2pat.Put(path), handler)
case "PATCH":
mux.HandleFunc(gojiv2pat.Patch(path), handler)
case "DELETE":
mux.HandleFunc(gojiv2pat.Delete(path), handler)
default:
panic("Unknow HTTP method: " + method)
}
return mux
}
// go-json-rest/rest
func goJsonRestHandler(w rest.ResponseWriter, req *rest.Request) {}
func goJsonRestHandlerWrite(w rest.ResponseWriter, req *rest.Request) {
io.WriteString(w.(io.Writer), req.PathParam("name"))
}
func goJsonRestHandlerTest(w rest.ResponseWriter, req *rest.Request) {
io.WriteString(w.(io.Writer), req.RequestURI)
}
func loadGoJsonRest(routes []route) http.Handler {
h := goJsonRestHandler
if loadTestHandler {
h = goJsonRestHandlerTest
}
api := rest.NewApi()
restRoutes := make([]*rest.Route, 0, len(routes))
for _, route := range routes {
restRoutes = append(restRoutes,
&rest.Route{route.method, route.path, h},
)
}
router, err := rest.MakeRouter(restRoutes...)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
return api.MakeHandler()
}
func loadGoJsonRestSingle(method, path string, hfunc rest.HandlerFunc) http.Handler {
api := rest.NewApi()
router, err := rest.MakeRouter(
&rest.Route{method, path, hfunc},
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
return api.MakeHandler()
}
// go-restful
func goRestfulHandler(r *restful.Request, w *restful.Response) {}
func goRestfulHandlerWrite(r *restful.Request, w *restful.Response) {
io.WriteString(w, r.PathParameter("name"))
}
func goRestfulHandlerTest(r *restful.Request, w *restful.Response) {
io.WriteString(w, r.Request.RequestURI)
}
func loadGoRestful(routes []route) http.Handler {
h := goRestfulHandler
if loadTestHandler {
h = goRestfulHandlerTest
}
re := regexp.MustCompile(":([^/]*)")
wsContainer := restful.NewContainer()
ws := new(restful.WebService)
for _, route := range routes {
path := re.ReplaceAllString(route.path, "{$1}")
switch route.method {
case "GET":
ws.Route(ws.GET(path).To(h))
case "POST":
ws.Route(ws.POST(path).To(h))
case "PUT":
ws.Route(ws.PUT(path).To(h))
case "PATCH":
ws.Route(ws.PATCH(path).To(h))
case "DELETE":
ws.Route(ws.DELETE(path).To(h))
default:
panic("Unknow HTTP method: " + route.method)
}
}
wsContainer.Add(ws)
return wsContainer
}
func loadGoRestfulSingle(method, path string, handler restful.RouteFunction) http.Handler {
wsContainer := restful.NewContainer()
ws := new(restful.WebService)
switch method {
case "GET":
ws.Route(ws.GET(path).To(handler))
case "POST":
ws.Route(ws.POST(path).To(handler))
case "PUT":
ws.Route(ws.PUT(path).To(handler))
case "PATCH":
ws.Route(ws.PATCH(path).To(handler))
case "DELETE":
ws.Route(ws.DELETE(path).To(handler))
default:
panic("Unknow HTTP method: " + method)
}
wsContainer.Add(ws)
return wsContainer
}
// gorilla/mux
func gorillaHandlerWrite(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
io.WriteString(w, params["name"])
}
func loadGorillaMux(routes []route) http.Handler {
h := httpHandlerFunc
if loadTestHandler {
h = httpHandlerFuncTest
}
re := regexp.MustCompile(":([^/]*)")
m := mux.NewRouter()
for _, route := range routes {
m.HandleFunc(
re.ReplaceAllString(route.path, "{$1}"),
h,
).Methods(route.method)
}
return m
}
func loadGorillaMuxSingle(method, path string, handler http.HandlerFunc) http.Handler {
m := mux.NewRouter()
m.HandleFunc(path, handler).Methods(method)
return m
}
// gowww/router
func gowwwRouterHandleWrite(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, gowwwrouter.Parameter(r, "name"))
}
func loadGowwwRouter(routes []route) http.Handler {
h := httpHandlerFunc
if loadTestHandler {
h = httpHandlerFuncTest
}
router := gowwwrouter.New()
for _, route := range routes {
router.Handle(route.method, route.path, http.HandlerFunc(h))
}
return router
}
func loadGowwwRouterSingle(method, path string, handler http.Handler) http.Handler {
router := gowwwrouter.New()
router.Handle(method, path, handler)
return router
}
// HttpRouter
func httpRouterHandle(_ http.ResponseWriter, _ *http.Request, _ httprouter.Params) {}
func httpRouterHandleWrite(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
io.WriteString(w, ps.ByName("name"))
}
func httpRouterHandleTest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
io.WriteString(w, r.RequestURI)
}
func loadHttpRouter(routes []route) http.Handler {
h := httpRouterHandle
if loadTestHandler {
h = httpRouterHandleTest
}
router := httprouter.New()
for _, route := range routes {
router.Handle(route.method, route.path, h)
}
return router
}
func loadHttpRouterSingle(method, path string, handle httprouter.Handle) http.Handler {
router := httprouter.New()
router.Handle(method, path, handle)
return router
}
// httpTreeMux
func httpTreeMuxHandler(_ http.ResponseWriter, _ *http.Request, _ map[string]string) {}
func httpTreeMuxHandlerWrite(w http.ResponseWriter, _ *http.Request, vars map[string]string) {
io.WriteString(w, vars["name"])
}
func httpTreeMuxHandlerTest(w http.ResponseWriter, r *http.Request, _ map[string]string) {
io.WriteString(w, r.RequestURI)
}
func loadHttpTreeMux(routes []route) http.Handler {
h := httpTreeMuxHandler
if loadTestHandler {
h = httpTreeMuxHandlerTest
}
router := httptreemux.New()
for _, route := range routes {
router.Handle(route.method, route.path, h)
}
return router
}
func loadHttpTreeMuxSingle(method, path string, handler httptreemux.HandlerFunc) http.Handler {
router := httptreemux.New()
router.Handle(method, path, handler)
return router
}
// Kocha-urlrouter
type kochaHandler struct {
routerMap map[string]urlrouter.URLRouter
params []urlrouter.Param
}
func (h *kochaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
meth, params := h.routerMap[r.Method].Lookup(r.URL.Path)
h.params = params
meth.(http.HandlerFunc).ServeHTTP(w, r)
}
func (h *kochaHandler) Get(w http.ResponseWriter, r *http.Request) {}
func (h *kochaHandler) Post(w http.ResponseWriter, r *http.Request) {}
func (h *kochaHandler) Put(w http.ResponseWriter, r *http.Request) {}
func (h *kochaHandler) Patch(w http.ResponseWriter, r *http.Request) {}
func (h *kochaHandler) Delete(w http.ResponseWriter, r *http.Request) {}
func (h *kochaHandler) kochaHandlerWrite(w http.ResponseWriter, r *http.Request) {
var name string
for _, param := range h.params {
if param.Name == "name" {
name = param.Value
break
}
}
io.WriteString(w, name)
}
func loadKocha(routes []route) http.Handler {
/*h := httpRouterHandle
if loadTestHandler {
h = httpRouterHandleTest
}*/
handler := &kochaHandler{routerMap: map[string]urlrouter.URLRouter{
"GET": urlrouter.NewURLRouter("doublearray"),
"POST": urlrouter.NewURLRouter("doublearray"),
"PUT": urlrouter.NewURLRouter("doublearray"),
"PATCH": urlrouter.NewURLRouter("doublearray"),
"DELETE": urlrouter.NewURLRouter("doublearray"),
}}
recordMap := make(map[string][]urlrouter.Record)