From 38119172959d124c0726ec79abe75aa29fb4670c Mon Sep 17 00:00:00 2001 From: Aryan <1111aryantiwari@gmail.com> Date: Tue, 22 Aug 2023 17:28:45 +0530 Subject: [PATCH 1/5] 11-aryan --- docs/guide/routing.md | 53 +++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/docs/guide/routing.md b/docs/guide/routing.md index 615d1aa2f2..1cde7f2e24 100644 --- a/docs/guide/routing.md +++ b/docs/guide/routing.md @@ -23,17 +23,16 @@ Route paths, combined with a request method, define the endpoints at which reque ```go // This route path will match requests to the root route, "/": -app.Get("/", func(c *fiber.Ctx) error { +app.Get(`/`, func(c *fiber.Ctx) error { return c.SendString("root") }) // This route path will match requests to "/about": -app.Get("/about", func(c *fiber.Ctx) error { +app.Get(`/about`, func(c *fiber.Ctx) error { return c.SendString("about") -}) - +})` // This route path will match requests to "/random.txt": -app.Get("/random.txt", func(c *fiber.Ctx) error { +app.Get(`/random.txt`, func(c *fiber.Ctx) error { return c.SendString("random.txt") }) ``` @@ -59,28 +58,28 @@ The routing also offers the possibility to use optional parameters, for the name ```go // Parameters -app.Get("/user/:name/books/:title", func(c *fiber.Ctx) error { +app.Get(`/user/:name/books/:title`, func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s\n", c.Params("name")) fmt.Fprintf(c, "%s\n", c.Params("title")) return nil }) // Plus - greedy - not optional -app.Get("/user/+", func(c *fiber.Ctx) error { +app.Get(`/user/+`, func(c *fiber.Ctx) error { return c.SendString(c.Params("+")) }) // Optional parameter -app.Get("/user/:name?", func(c *fiber.Ctx) error { +app.Get(`/user/:name?`, func(c *fiber.Ctx) error { return c.SendString(c.Params("name")) }) // Wildcard - greedy - optional -app.Get("/user/*", func(c *fiber.Ctx) error { +app.Get(`/user/*`, func(c *fiber.Ctx) error { return c.SendString(c.Params("*")) }) // This route path will match requests to "/v1/some/resource/name:customVerb", since the parameter character is escaped -app.Get("/v1/some/resource/name\\:customVerb", func(c *fiber.Ctx) error { +app.Get(`/v1/some/resource/name\\:customVerb`, func(c *fiber.Ctx) error { return c.SendString("Hello, Community") }) ``` @@ -90,12 +89,12 @@ Since the hyphen \(`-`\) and the dot \(`.`\) are interpreted literally, they can ::: :::info -All special parameter characters can also be escaped with `"\\"` and lose their value, so you can use them in the route if you want, like in the custom methods of the [google api design guide](https://cloud.google.com/apis/design/custom_methods). +All special parameter characters can also be escaped with `"\\"` and lose their value, so you can use them in the route if you want, like in the custom methods of the [google api design guide](https://cloud.google.com/apis/design/custom_methods). It's recommended to use backticks `` because in go's regex documentation, they always use backticks to make sure it is unambiguous and the escape character doesn't interfere with regex patterns in an unexpected way. ::: ```go // http://localhost:3000/plantae/prunus.persica -app.Get("/plantae/:genus.:species", func(c *fiber.Ctx) error { +app.Get(`/plantae/:genus.:species`, func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s.%s\n", c.Params("genus"), c.Params("species")) return nil // prunus.persica }) @@ -103,7 +102,7 @@ app.Get("/plantae/:genus.:species", func(c *fiber.Ctx) error { ```go // http://localhost:3000/flights/LAX-SFO -app.Get("/flights/:from-:to", func(c *fiber.Ctx) error { +app.Get(`/flights/:from-:to`, func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s-%s\n", c.Params("from"), c.Params("to")) return nil // LAX-SFO }) @@ -113,7 +112,7 @@ Our intelligent router recognizes that the introductory parameter characters sho ```go // http://localhost:3000/shop/product/color:blue/size:xs -app.Get("/shop/product/color::color/size::size", func(c *fiber.Ctx) error { +app.Get(`/shop/product/color::color/size::size`, func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s:%s\n", c.Params("color"), c.Params("size")) return nil // blue:xs }) @@ -124,19 +123,19 @@ In addition, several parameters in a row and several unnamed parameter character ```go // GET /@v1 // Params: "sign" -> "@", "param" -> "v1" -app.Get("/:sign:param", handler) +app.Get(`/:sign:param`, handler) // GET /api-v1 // Params: "name" -> "v1" -app.Get("/api-:name", handler) +app.Get(`/api-:name`, handler) // GET /customer/v1/cart/proxy // Params: "*1" -> "customer/", "*2" -> "/cart" -app.Get("/*v1*/proxy", handler) +app.Get(`/*v1*/proxy`, handler) // GET /v1/brand/4/shop/blue/xs // Params: "*1" -> "brand/4", "*2" -> "blue/xs" -app.Get("/v1/*/shop/*", handler) +app.Get(`/v1/*/shop/*`, handler) ``` We have adapted the routing strongly to the express routing, but currently without the possibility of the regular expressions, because they are quite slow. The possibilities can be tested with version 0.1.7 \(express 4\) in the online [Express route tester](http://forbeslindesay.github.io/express-route-tester/). @@ -170,7 +169,7 @@ Constraints aren't validation for parameters. If constraint aren't valid for par ```go -app.Get("/:test", func(c *fiber.Ctx) error { +app.Get(`/:test`, func(c *fiber.Ctx) error { return c.SendString(c.Params("test")) }) @@ -185,7 +184,7 @@ app.Get("/:test", func(c *fiber.Ctx) error { You can use `;` for multiple constraints. ```go -app.Get("/:test", func(c *fiber.Ctx) error { +app.Get(`/:test`, func(c *fiber.Ctx) error { return c.SendString(c.Params("test")) }) @@ -203,7 +202,7 @@ app.Get("/:test", func(c *fiber.Ctx) error { Fiber precompiles regex query when to register routes. So there're no performance overhead for regex constraint. ```go -app.Get("/:date", func(c *fiber.Ctx) error { +app.Get(`/:date`, func(c *fiber.Ctx) error { return c.SendString(c.Params("date")) }) @@ -229,7 +228,7 @@ You should use `\\` before routing-specific characters when to use datetime cons You can impose constraints on optional parameters as well. ```go -app.Get("/:test?", func(c *fiber.Ctx) error { +app.Get(`/:test?`, func(c *fiber.Ctx) error { return c.SendString(c.Params("test")) }) // curl -X GET http://localhost:3000/42 @@ -255,7 +254,7 @@ app.Use(func(c *fiber.Ctx) error { return c.Next() }) -app.Get("/", func(c *fiber.Ctx) error { +app.Get(`/`, func(c *fiber.Ctx) error { return c.SendString("Hello, World!") }) ``` @@ -273,12 +272,12 @@ func main() { api := app.Group("/api", middleware) // /api v1 := api.Group("/v1", middleware) // /api/v1 - v1.Get("/list", handler) // /api/v1/list - v1.Get("/user", handler) // /api/v1/user + v1.Get(`/list`, handler) // /api/v1/list + v1.Get(`/user`, handler) // /api/v1/user v2 := api.Group("/v2", middleware) // /api/v2 - v2.Get("/list", handler) // /api/v2/list - v2.Get("/user", handler) // /api/v2/user + v2.Get(`/list`, handler) // /api/v2/list + v2.Get(`/user`, handler) // /api/v2/user log.Fatal(app.Listen(":3000")) } From b3878ddd0a33907383ab0b56f39dd8fd29fa2355 Mon Sep 17 00:00:00 2001 From: Aryan <1111aryantiwari@gmail.com> Date: Sun, 3 Sep 2023 11:12:55 +0530 Subject: [PATCH 2/5] Removed the backticks where no special characters is used --- docs/guide/routing.md | 48 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/guide/routing.md b/docs/guide/routing.md index 1cde7f2e24..bfb93fb103 100644 --- a/docs/guide/routing.md +++ b/docs/guide/routing.md @@ -23,16 +23,16 @@ Route paths, combined with a request method, define the endpoints at which reque ```go // This route path will match requests to the root route, "/": -app.Get(`/`, func(c *fiber.Ctx) error { +app.Get("/", func(c *fiber.Ctx) error { return c.SendString("root") }) // This route path will match requests to "/about": -app.Get(`/about`, func(c *fiber.Ctx) error { +app.Get"`/about", func(c *fiber.Ctx) error { return c.SendString("about") })` // This route path will match requests to "/random.txt": -app.Get(`/random.txt`, func(c *fiber.Ctx) error { +app.Get("/random.txt", func(c *fiber.Ctx) error { return c.SendString("random.txt") }) ``` @@ -58,28 +58,28 @@ The routing also offers the possibility to use optional parameters, for the name ```go // Parameters -app.Get(`/user/:name/books/:title`, func(c *fiber.Ctx) error { +app.Get("/user/:name/books/:title", func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s\n", c.Params("name")) fmt.Fprintf(c, "%s\n", c.Params("title")) return nil }) // Plus - greedy - not optional -app.Get(`/user/+`, func(c *fiber.Ctx) error { +app.Get("/user/+", func(c *fiber.Ctx) error { return c.SendString(c.Params("+")) }) // Optional parameter -app.Get(`/user/:name?`, func(c *fiber.Ctx) error { +app.Get("/user/:name?", func(c *fiber.Ctx) error { return c.SendString(c.Params("name")) }) // Wildcard - greedy - optional -app.Get(`/user/*`, func(c *fiber.Ctx) error { +app.Get("/user/*", func(c *fiber.Ctx) error { return c.SendString(c.Params("*")) }) // This route path will match requests to "/v1/some/resource/name:customVerb", since the parameter character is escaped -app.Get(`/v1/some/resource/name\\:customVerb`, func(c *fiber.Ctx) error { +app.Get(`/v1/some/resource/name\:customVerb`, func(c *fiber.Ctx) error { return c.SendString("Hello, Community") }) ``` @@ -94,7 +94,7 @@ All special parameter characters can also be escaped with `"\\"` and lose their ```go // http://localhost:3000/plantae/prunus.persica -app.Get(`/plantae/:genus.:species`, func(c *fiber.Ctx) error { +app.Get("/plantae/:genus.:species", func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s.%s\n", c.Params("genus"), c.Params("species")) return nil // prunus.persica }) @@ -102,7 +102,7 @@ app.Get(`/plantae/:genus.:species`, func(c *fiber.Ctx) error { ```go // http://localhost:3000/flights/LAX-SFO -app.Get(`/flights/:from-:to`, func(c *fiber.Ctx) error { +app.Get("/flights/:from-:to", func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s-%s\n", c.Params("from"), c.Params("to")) return nil // LAX-SFO }) @@ -112,7 +112,7 @@ Our intelligent router recognizes that the introductory parameter characters sho ```go // http://localhost:3000/shop/product/color:blue/size:xs -app.Get(`/shop/product/color::color/size::size`, func(c *fiber.Ctx) error { +app.Get("/shop/product/color::color/size::size", func(c *fiber.Ctx) error { fmt.Fprintf(c, "%s:%s\n", c.Params("color"), c.Params("size")) return nil // blue:xs }) @@ -123,19 +123,19 @@ In addition, several parameters in a row and several unnamed parameter character ```go // GET /@v1 // Params: "sign" -> "@", "param" -> "v1" -app.Get(`/:sign:param`, handler) +app.Get("/:sign:param", handler) // GET /api-v1 // Params: "name" -> "v1" -app.Get(`/api-:name`, handler) +app.Get("/api-:name", handler) // GET /customer/v1/cart/proxy // Params: "*1" -> "customer/", "*2" -> "/cart" -app.Get(`/*v1*/proxy`, handler) +app.Get("/*v1*/proxy", handler) // GET /v1/brand/4/shop/blue/xs // Params: "*1" -> "brand/4", "*2" -> "blue/xs" -app.Get(`/v1/*/shop/*`, handler) +app.Get("/v1/*/shop/*", handler) ``` We have adapted the routing strongly to the express routing, but currently without the possibility of the regular expressions, because they are quite slow. The possibilities can be tested with version 0.1.7 \(express 4\) in the online [Express route tester](http://forbeslindesay.github.io/express-route-tester/). @@ -169,7 +169,7 @@ Constraints aren't validation for parameters. If constraint aren't valid for par ```go -app.Get(`/:test`, func(c *fiber.Ctx) error { +app.Get("/:test", func(c *fiber.Ctx) error { return c.SendString(c.Params("test")) }) @@ -184,7 +184,7 @@ app.Get(`/:test`, func(c *fiber.Ctx) error { You can use `;` for multiple constraints. ```go -app.Get(`/:test`, func(c *fiber.Ctx) error { +app.Get("/:test", func(c *fiber.Ctx) error { return c.SendString(c.Params("test")) }) @@ -202,7 +202,7 @@ app.Get(`/:test`, func(c *fiber.Ctx) error { Fiber precompiles regex query when to register routes. So there're no performance overhead for regex constraint. ```go -app.Get(`/:date`, func(c *fiber.Ctx) error { +app.Get(`/:date`, func(c *fiber.Ctx) error { return c.SendString(c.Params("date")) }) @@ -228,7 +228,7 @@ You should use `\\` before routing-specific characters when to use datetime cons You can impose constraints on optional parameters as well. ```go -app.Get(`/:test?`, func(c *fiber.Ctx) error { +app.Get("/:test?", func(c *fiber.Ctx) error { return c.SendString(c.Params("test")) }) // curl -X GET http://localhost:3000/42 @@ -254,7 +254,7 @@ app.Use(func(c *fiber.Ctx) error { return c.Next() }) -app.Get(`/`, func(c *fiber.Ctx) error { +app.Get("/", func(c *fiber.Ctx) error { return c.SendString("Hello, World!") }) ``` @@ -272,12 +272,12 @@ func main() { api := app.Group("/api", middleware) // /api v1 := api.Group("/v1", middleware) // /api/v1 - v1.Get(`/list`, handler) // /api/v1/list - v1.Get(`/user`, handler) // /api/v1/user + v1.Get("/list", handler) // /api/v1/list + v1.Get("/user", handler) // /api/v1/user v2 := api.Group("/v2", middleware) // /api/v2 - v2.Get(`/list`, handler) // /api/v2/list - v2.Get(`/user`, handler) // /api/v2/user + v2.Get("/list", handler) // /api/v2/list + v2.Get("/user", handler) // /api/v2/user log.Fatal(app.Listen(":3000")) } From 81bd90ebc45c3f5f1ffcf915d60a105f6e124274 Mon Sep 17 00:00:00 2001 From: Aryan <1111aryantiwari@gmail.com> Date: Sun, 3 Sep 2023 11:37:22 +0530 Subject: [PATCH 3/5] added backticks to path parameters where special characters are escaped --- path_testcases_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/path_testcases_test.go b/path_testcases_test.go index 5602a0284d..26ec5b748e 100644 --- a/path_testcases_test.go +++ b/path_testcases_test.go @@ -85,21 +85,21 @@ func init() { }, }, { - pattern: "/v1/some/resource/name\\:customVerb", + pattern: `/v1/some/resource/name\:customVerb`, testCases: []routeTestCase{ {url: "/v1/some/resource/name:customVerb", params: nil, match: true}, {url: "/v1/some/resource/name:test", params: nil, match: false}, }, }, { - pattern: "/v1/some/resource/:name\\:customVerb", + pattern: `/v1/some/resource/:name\:customVerb`, testCases: []routeTestCase{ {url: "/v1/some/resource/test:customVerb", params: []string{"test"}, match: true}, {url: "/v1/some/resource/test:test", params: nil, match: false}, }, }, { - pattern: "/v1/some/resource/name\\\\:customVerb?\\?/:param/*", + pattern: `/v1/some/resource/name\\:customVerb?\?/:param/*`, testCases: []routeTestCase{ {url: "/v1/some/resource/name:customVerb??/test/optionalWildCard/character", params: []string{"test", "optionalWildCard/character"}, match: true}, {url: "/v1/some/resource/name:customVerb??/test", params: []string{"test", ""}, match: true}, @@ -572,7 +572,7 @@ func init() { }, }, { - pattern: "/api/v1/:param", + pattern: `/api/v1/:param`, testCases: []routeTestCase{ {url: "/api/v1/entity", params: nil, match: false}, {url: "/api/v1/8728382", params: nil, match: false}, @@ -598,7 +598,7 @@ func init() { }, }, { - pattern: "/api/v1/:param", + pattern: `/api/v1/:param`, testCases: []routeTestCase{ {url: "/api/v1/ent", params: nil, match: false}, {url: "/api/v1/15", params: nil, match: false}, @@ -642,7 +642,7 @@ func init() { }, }, { - pattern: "/api/v1/:param", + pattern: `/api/v1/:param`, testCases: []routeTestCase{ {url: "/api/v1/entity", params: []string{"entity"}, match: true}, {url: "/api/v1/87283827683", params: []string{"87283827683"}, match: true}, @@ -651,7 +651,7 @@ func init() { }, }, { - pattern: "/api/v1/:param", + pattern: `/api/v1/:param`, testCases: []routeTestCase{ {url: "/api/v1/entity", params: nil, match: false}, {url: "/api/v1/87283827683", params: nil, match: false}, @@ -697,7 +697,7 @@ func init() { }, }, { - pattern: "/api/v1/:date/:regex", + pattern: `/api/v1/:date/:regex`, testCases: []routeTestCase{ {url: "/api/v1/2005-11-01/a", params: nil, match: false}, {url: "/api/v1/2005-1101/paach", params: nil, match: false}, From 7c0e6deef038b88a8a4f6090ae1f77839d84b1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Mon, 4 Sep 2023 05:22:04 +0200 Subject: [PATCH 4/5] Replaced double quotes with backticks in all route parameter strings #2591 --- docs/guide/routing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/guide/routing.md b/docs/guide/routing.md index bfb93fb103..d9efaadf3e 100644 --- a/docs/guide/routing.md +++ b/docs/guide/routing.md @@ -28,9 +28,9 @@ app.Get("/", func(c *fiber.Ctx) error { }) // This route path will match requests to "/about": -app.Get"`/about", func(c *fiber.Ctx) error { +app.Get("/about", func(c *fiber.Ctx) error { return c.SendString("about") -})` +}) // This route path will match requests to "/random.txt": app.Get("/random.txt", func(c *fiber.Ctx) error { return c.SendString("random.txt") @@ -89,7 +89,7 @@ Since the hyphen \(`-`\) and the dot \(`.`\) are interpreted literally, they can ::: :::info -All special parameter characters can also be escaped with `"\\"` and lose their value, so you can use them in the route if you want, like in the custom methods of the [google api design guide](https://cloud.google.com/apis/design/custom_methods). It's recommended to use backticks `` because in go's regex documentation, they always use backticks to make sure it is unambiguous and the escape character doesn't interfere with regex patterns in an unexpected way. +All special parameter characters can also be escaped with `"\\"` and lose their value, so you can use them in the route if you want, like in the custom methods of the [google api design guide](https://cloud.google.com/apis/design/custom_methods). It's recommended to use backticks `` ` `` because in go's regex documentation, they always use backticks to make sure it is unambiguous and the escape character doesn't interfere with regex patterns in an unexpected way. ::: ```go From 6ea0c770cd20945a36b35f9fd66476e4228829ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Mon, 4 Sep 2023 05:23:36 +0200 Subject: [PATCH 5/5] Replaced double quotes with backticks in all route parameter strings #2591 --- docs/guide/routing.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guide/routing.md b/docs/guide/routing.md index d9efaadf3e..48c2a8877c 100644 --- a/docs/guide/routing.md +++ b/docs/guide/routing.md @@ -31,6 +31,7 @@ app.Get("/", func(c *fiber.Ctx) error { app.Get("/about", func(c *fiber.Ctx) error { return c.SendString("about") }) + // This route path will match requests to "/random.txt": app.Get("/random.txt", func(c *fiber.Ctx) error { return c.SendString("random.txt")