From feb1a1f61dcd1075db2effe330a86fccc96c1285 Mon Sep 17 00:00:00 2001 From: Haitao Chen Date: Tue, 21 May 2024 16:39:58 +0000 Subject: [PATCH] path template casing example --- examples/hello/restful-hello-world.go | 5 ++- .../path-case-sensitive/case-sensitive.go | 33 +++++++++++++++++++ examples/path-case-sensitive/go.mod | 5 +++ examples/path-case-sensitive/go.sum | 2 ++ examples/path-case-sensitive/test.sh | 3 ++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 examples/path-case-sensitive/case-sensitive.go create mode 100644 examples/path-case-sensitive/go.mod create mode 100644 examples/path-case-sensitive/go.sum create mode 100644 examples/path-case-sensitive/test.sh diff --git a/examples/hello/restful-hello-world.go b/examples/hello/restful-hello-world.go index d2854af5..1ab8c78a 100644 --- a/examples/hello/restful-hello-world.go +++ b/examples/hello/restful-hello-world.go @@ -16,7 +16,10 @@ func main() { ws := new(restful.WebService) ws.Route(ws.GET("/hello").To(hello)) restful.Add(ws) - log.Fatal(http.ListenAndServe(":8080", nil)) + + // DO NOT wrap http.ListenAndServe with log.Fatal in production + // or you won't be able to drain in-flight request gracefully, even you handle sigterm + log.Fatal(http.ListenAndServe(":8080", nil)) } func hello(req *restful.Request, resp *restful.Response) { diff --git a/examples/path-case-sensitive/case-sensitive.go b/examples/path-case-sensitive/case-sensitive.go new file mode 100644 index 00000000..2673d898 --- /dev/null +++ b/examples/path-case-sensitive/case-sensitive.go @@ -0,0 +1,33 @@ +package main + +import ( + "io" + "log" + "net/http" + + restful "github.com/emicklei/go-restful/v3" +) + +// This example shows how to handle different casing of path template. +// +// GET http://localhost:8080/hola/Juan +// GET http://localhost:8080/HOLA/Juan +// GET http://localhost:8080/Hola/Juan + +func main() { + ws := new(restful.WebService) + + // hola is path template, to accept different casing of hola, we use regex matching with syntax {name:regex} + // - {: is nesserary to trigger the regex matching. + // - (?i) is to make the regex case-insensitive. + // it seems solve the issue, but there is a issue you might hit: https://github.com/emicklei/go-restful/issues/545 + // to avoid partial matching, another regex pattern + // - ^$ is needed to match the whole route token. + ws.Route(ws.GET("/{:(?i)^hola$}/{name:*}").To(hola)) + restful.Add(ws) + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +func hola(req *restful.Request, resp *restful.Response) { + io.WriteString(resp, "hola "+req.PathParameter("name")) +} diff --git a/examples/path-case-sensitive/go.mod b/examples/path-case-sensitive/go.mod new file mode 100644 index 00000000..be698ff4 --- /dev/null +++ b/examples/path-case-sensitive/go.mod @@ -0,0 +1,5 @@ +module github.com/emicklei/go-restful/examples/pathtemplatecasing + +go 1.14 + +require github.com/emicklei/go-restful/v3 v3.12.0 diff --git a/examples/path-case-sensitive/go.sum b/examples/path-case-sensitive/go.sum new file mode 100644 index 00000000..58dfe6cb --- /dev/null +++ b/examples/path-case-sensitive/go.sum @@ -0,0 +1,2 @@ +github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapwQtU84iWk= +github.com/emicklei/go-restful/v3 v3.12.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= diff --git a/examples/path-case-sensitive/test.sh b/examples/path-case-sensitive/test.sh new file mode 100644 index 00000000..123b8c9a --- /dev/null +++ b/examples/path-case-sensitive/test.sh @@ -0,0 +1,3 @@ +GET http://localhost:8080/hola/Juan +GET http://localhost:8080/HOLA/Juan +GET http://localhost:8080/Hola/Juan \ No newline at end of file