diff --git a/.github/README.md b/.github/README.md index fb82e22c..531a4d88 100644 --- a/.github/README.md +++ b/.github/README.md @@ -191,6 +191,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_de.md b/.github/README_de.md index dec2fb97..0994b756 100644 --- a/.github/README_de.md +++ b/.github/README_de.md @@ -189,6 +189,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_es.md b/.github/README_es.md index 6277ed4f..e364f8ed 100644 --- a/.github/README_es.md +++ b/.github/README_es.md @@ -189,6 +189,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Sirviendo Archivos Estรกticos**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_fa.md b/.github/README_fa.md index 03fe4676..e016fe7b 100644 --- a/.github/README_fa.md +++ b/.github/README_fa.md @@ -240,6 +240,41 @@ func main() { +
+ +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + +
+ + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static)
diff --git a/.github/README_fr.md b/.github/README_fr.md index 5d722808..6d6d2e79 100644 --- a/.github/README_fr.md +++ b/.github/README_fr.md @@ -191,6 +191,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_he.md b/.github/README_he.md index 6a133470..e1a10b83 100644 --- a/.github/README_he.md +++ b/.github/README_he.md @@ -246,6 +246,40 @@ func main() {
+#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +
+ +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + +
+ #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static)
diff --git a/.github/README_id.md b/.github/README_id.md index 66588b8f..b8327a6e 100644 --- a/.github/README_id.md +++ b/.github/README_id.md @@ -191,6 +191,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving File Static**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_it.md b/.github/README_it.md index b5024233..8fddc869 100644 --- a/.github/README_it.md +++ b/.github/README_it.md @@ -190,6 +190,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Servire File Statici**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_ja.md b/.github/README_ja.md index 07a97f59..620707a9 100644 --- a/.github/README_ja.md +++ b/.github/README_ja.md @@ -194,6 +194,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**้™็š„ใƒ•ใ‚กใ‚คใƒซใฎๆไพ›**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_ko.md b/.github/README_ko.md index c3a34ee6..8835fcce 100644 --- a/.github/README_ko.md +++ b/.github/README_ko.md @@ -195,6 +195,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_nl.md b/.github/README_nl.md index 3a4d8c0c..5a0dd6e9 100644 --- a/.github/README_nl.md +++ b/.github/README_nl.md @@ -195,6 +195,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_pt.md b/.github/README_pt.md index 5fed7e4f..f38315e7 100644 --- a/.github/README_pt.md +++ b/.github/README_pt.md @@ -191,6 +191,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Servindo arquivos estรกticos**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_ru.md b/.github/README_ru.md index acd41f18..ee4bb3eb 100644 --- a/.github/README_ru.md +++ b/.github/README_ru.md @@ -191,6 +191,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_sa.md b/.github/README_sa.md index 4cf470d7..6ebcf9c0 100644 --- a/.github/README_sa.md +++ b/.github/README_sa.md @@ -209,6 +209,40 @@ func main() {
+
+ +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + +
+ #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static)
diff --git a/.github/README_tr.md b/.github/README_tr.md index 4ec163ff..1e5f8187 100644 --- a/.github/README_tr.md +++ b/.github/README_tr.md @@ -190,6 +190,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route ฤฐsimlendirmesi**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ kayit + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Cikti: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Statik Dosya Sunumu**](https://docs.gofiber.io/api/app#static) ```go diff --git a/.github/README_zh-CN.md b/.github/README_zh-CN.md index 1afbc7ed..e53d9b14 100644 --- a/.github/README_zh-CN.md +++ b/.github/README_zh-CN.md @@ -191,6 +191,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**้™ๆ€ๆ–‡ไปถ**](https://docs.gofiber.io/api/app#static)ๆœๅŠก ```go diff --git a/.github/README_zh-TW.md b/.github/README_zh-TW.md index 024ffe47..538ce995 100644 --- a/.github/README_zh-TW.md +++ b/.github/README_zh-TW.md @@ -193,6 +193,36 @@ func main() { ``` +#### ๐Ÿ“– [**Route Naming**](https://docs.gofiber.io/api/app#name) + +```go +func main() { + app := fiber.New() + + // GET /api/register + app.Get("/api/*", func(c *fiber.Ctx) error { + msg := fmt.Sprintf("โœ‹ %s", c.Params("*")) + return c.SendString(msg) // => โœ‹ register + }).Name("api") + + data, _ := json.MarshalIndent(app.GetRoute("api"), "", " ") + fmt.Print(string(data)) + // Prints: + // { + // "method": "GET", + // "name": "api", + // "path": "/api/*", + // "params": [ + // "*1" + // ] + // } + + + log.Fatal(app.Listen(":3000")) +} + +``` + #### ๐Ÿ“– [**Serving Static Files**](https://docs.gofiber.io/api/app#static) ```go diff --git a/app.go b/app.go index b9c4413d..38e35d9d 100644 --- a/app.go +++ b/app.go @@ -398,6 +398,14 @@ const ( DefaultCompressedFileSuffix = ".fiber.gz" ) +// Variables for Name & GetRoute +var latestRoute struct { + route *Route + mu sync.Mutex +} + +var latestGroup Group + // DefaultErrorHandler that process return errors from handlers var DefaultErrorHandler = func(c *Ctx, err error) error { code := StatusInternalServerError @@ -537,6 +545,30 @@ func (app *App) Mount(prefix string, fiber *App) Router { return app } +// Assign name to specific route. +func (app *App) Name(name string) Router { + if strings.HasPrefix(latestRoute.route.path, latestGroup.prefix) { + latestRoute.route.Name = latestGroup.name + name + } else { + latestRoute.route.Name = name + } + + return app +} + +// Get route by name +func (app *App) GetRoute(name string) Route { + for _, routes := range app.stack { + for _, route := range routes { + if route.Name == name { + return *route + } + } + } + + return Route{} +} + // Use registers a middleware route that will match requests // with the provided prefix (which is optional and defaults to "/"). // diff --git a/app_test.go b/app_test.go index b244cc7c..7662fd1a 100644 --- a/app_test.go +++ b/app_test.go @@ -519,6 +519,31 @@ func Test_App_Methods(t *testing.T) { testStatus200(t, app, "/john/doe", MethodGet) } +func Test_App_Route_Naming(t *testing.T) { + app := New() + handler := func(c *Ctx) error { + return c.SendStatus(StatusOK) + } + app.Get("/john", handler).Name("john") + app.Delete("/doe", handler) + app.Name("doe") + + jane := app.Group("/jane").Name("jane.") + jane.Get("/test", handler).Name("test") + jane.Trace("/trace", handler).Name("trace") + + group := app.Group("/group") + group.Get("/test", handler).Name("test") + + app.Post("/post", handler).Name("post") + + utils.AssertEqual(t, "post", app.GetRoute("post").Name) + utils.AssertEqual(t, "john", app.GetRoute("john").Name) + utils.AssertEqual(t, "jane.test", app.GetRoute("jane.test").Name) + utils.AssertEqual(t, "jane.trace", app.GetRoute("jane.trace").Name) + utils.AssertEqual(t, "test", app.GetRoute("test").Name) +} + func Test_App_New(t *testing.T) { app := New() app.Get("/", testEmptyHandler) diff --git a/client.go b/client.go index 252c270e..c7134234 100644 --- a/client.go +++ b/client.go @@ -656,7 +656,7 @@ func (a *Agent) Reuse() *Agent { func (a *Agent) InsecureSkipVerify() *Agent { if a.HostClient.TLSConfig == nil { /* #nosec G402 */ - a.HostClient.TLSConfig = &tls.Config{InsecureSkipVerify: true} + a.HostClient.TLSConfig = &tls.Config{InsecureSkipVerify: true} // #nosec G402 } else { /* #nosec G402 */ a.HostClient.TLSConfig.InsecureSkipVerify = true diff --git a/group.go b/group.go index 241545fd..1c649680 100644 --- a/group.go +++ b/group.go @@ -14,6 +14,7 @@ import ( type Group struct { app *App prefix string + name string } // Mount attaches another app instance as a sub-router along a routing path. @@ -33,6 +34,14 @@ func (grp *Group) Mount(prefix string, fiber *App) Router { return grp } +// Assign name to specific route. +func (grp *Group) Name(name string) Router { + grp.name = name + latestGroup = *grp + + return grp +} + // Use registers a middleware route that will match requests // with the provided prefix (which is optional and defaults to "/"). // diff --git a/helpers.go b/helpers.go index 691b0e07..7aa493ea 100644 --- a/helpers.go +++ b/helpers.go @@ -68,7 +68,8 @@ func getTlsConfig(ln net.Listener) *tls.Config { // Get private field from value if field := val.FieldByName("config"); field.Type() != nil { // Copy value from pointer field (unsafe) - if newval := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())); newval.Type() != nil { + newval := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())) // #nosec G103 + if newval.Type() != nil { // Get element from pointer if elem := newval.Elem(); elem.Type() != nil { // Cast value to *tls.Config diff --git a/prefork.go b/prefork.go index 6adfe40f..459e4089 100644 --- a/prefork.go +++ b/prefork.go @@ -79,7 +79,7 @@ func (app *App) prefork(network, addr string, tlsConfig *tls.Config) (err error) // launch child procs for i := 0; i < max; i++ { /* #nosec G204 */ - cmd := exec.Command(os.Args[0], os.Args[1:]...) + cmd := exec.Command(os.Args[0], os.Args[1:]...) // #nosec G204 if testPreforkMaster { // When test prefork master, // just start the child process with a dummy cmd, diff --git a/router.go b/router.go index dcbda997..3eb6794e 100644 --- a/router.go +++ b/router.go @@ -37,6 +37,8 @@ type Router interface { Group(prefix string, handlers ...Handler) Router Mount(prefix string, fiber *App) Router + + Name(name string) Router } // Route is a struct that holds all metadata for each registered handler @@ -51,6 +53,7 @@ type Route struct { // Public fields Method string `json:"method"` // HTTP method + Name string `json:"name"` // Route's name Path string `json:"path"` // Original registered route path Params []string `json:"params"` // Case sensitive param keys Handlers []Handler `json:"-"` // Ctx handlers @@ -425,6 +428,10 @@ func (app *App) addRoute(method string, route *Route) { app.stack[m] = append(app.stack[m], route) app.routesRefreshed = true } + + latestRoute.mu.Lock() + latestRoute.route = route + latestRoute.mu.Unlock() } // buildTree build the prefix tree from the previously registered routes