Skip to content

Commit a053452

Browse files
committed
go-restful: Fix Params handling
1 parent 3ea0878 commit a053452

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

bench_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func BenchmarkGoJsonRest_Param(b *testing.B) {
157157
benchRequest(b, router, r)
158158
}
159159
func BenchmarkGoRestful_Param(b *testing.B) {
160-
router := loadGoRestfulSingle("GET", "/user/:name", goRestfulHandler)
160+
router := loadGoRestfulSingle("GET", "/user/{name}", goRestfulHandler)
161161

162162
r, _ := http.NewRequest("GET", "/user/gordon", nil)
163163
benchRequest(b, router, r)
@@ -325,7 +325,7 @@ func BenchmarkGoJsonRest_Param5(b *testing.B) {
325325
benchRequest(b, handler, r)
326326
}
327327
func BenchmarkGoRestful_Param5(b *testing.B) {
328-
router := loadGoRestfulSingle("GET", fiveColon, goRestfulHandler)
328+
router := loadGoRestfulSingle("GET", fiveBrace, goRestfulHandler)
329329

330330
r, _ := http.NewRequest("GET", fiveRoute, nil)
331331
benchRequest(b, router, r)
@@ -493,7 +493,7 @@ func BenchmarkGoJsonRest_Param20(b *testing.B) {
493493
benchRequest(b, handler, r)
494494
}
495495
func BenchmarkGoRestful_Param20(b *testing.B) {
496-
handler := loadGoRestfulSingle("GET", twentyColon, goRestfulHandler)
496+
handler := loadGoRestfulSingle("GET", twentyBrace, goRestfulHandler)
497497

498498
r, _ := http.NewRequest("GET", twentyRoute, nil)
499499
benchRequest(b, handler, r)
@@ -657,7 +657,7 @@ func BenchmarkGoJsonRest_ParamWrite(b *testing.B) {
657657
benchRequest(b, handler, r)
658658
}
659659
func BenchmarkGoRestful_ParamWrite(b *testing.B) {
660-
handler := loadGoRestfulSingle("GET", "/user/:name", goRestfulHandlerWrite)
660+
handler := loadGoRestfulSingle("GET", "/user/{name}", goRestfulHandlerWrite)
661661

662662
r, _ := http.NewRequest("GET", "/user/gordon", nil)
663663
benchRequest(b, handler, r)

routers.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,27 +459,31 @@ func loadGoJsonRestSingle(method, path string, hfunc rest.HandlerFunc) http.Hand
459459

460460
// go-restful
461461
func goRestfulHandlerWrite(r *restful.Request, w *restful.Response) {
462-
io.WriteString(w, r.Request.URL.Query().Get("name"))
462+
io.WriteString(w, r.PathParameter("name"))
463463
}
464464

465465
func goRestfulHandler(r *restful.Request, w *restful.Response) {}
466466

467467
func loadGoRestful(routes []route) http.Handler {
468+
re := regexp.MustCompile(":([^/]*)")
469+
468470
wsContainer := restful.NewContainer()
469471
ws := new(restful.WebService)
470472

471473
for _, route := range routes {
474+
path := re.ReplaceAllString(route.path, "{$1}")
475+
472476
switch route.method {
473477
case "GET":
474-
ws.Route(ws.GET(route.path).To(goRestfulHandler))
478+
ws.Route(ws.GET(path).To(goRestfulHandler))
475479
case "POST":
476-
ws.Route(ws.POST(route.path).To(goRestfulHandler))
480+
ws.Route(ws.POST(path).To(goRestfulHandler))
477481
case "PUT":
478-
ws.Route(ws.PUT(route.path).To(goRestfulHandler))
482+
ws.Route(ws.PUT(path).To(goRestfulHandler))
479483
case "PATCH":
480-
ws.Route(ws.PATCH(route.path).To(goRestfulHandler))
484+
ws.Route(ws.PATCH(path).To(goRestfulHandler))
481485
case "DELETE":
482-
ws.Route(ws.DELETE(route.path).To(goRestfulHandler))
486+
ws.Route(ws.DELETE(path).To(goRestfulHandler))
483487
default:
484488
panic("Unknow HTTP method: " + route.method)
485489
}

0 commit comments

Comments
 (0)