Fiber: 馃 How to base URL for whole app

Created on 3 Aug 2020  路  5Comments  路  Source: gofiber/fiber

Question description
How to add base URL for whole app ?

Code snippet _Optional_

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  // .. How to add base URL for whole app ?
  // app.setBaseUrl("/api/v1");

  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Hello, World 馃憢!")
  })
}
curl http://localhost/ # 404
curl http://localhost/api/v1/ # 200 "Hello, World 馃憢!"
馃 Question

Most helpful comment

func main() {
  app := fiber.New()

  api := app.Group("/api", handler) // /api

  v1 := api.Group("/v1", handler)   // /api/v1
  v1.Get("/list", handler)          // /api/v1/list
  v1.Get("/user", handler)          // /api/v1/user

  v2 := api.Group("/v2", handler)   // /api/v2
  v2.Get("/list", handler)          // /api/v2/list
  v2.Get("/user", handler)          // /api/v2/user

  app.Listen(3000)
}

All 5 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Try app.Group()

i use that but it's seems be anti pattern.

Try

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  // .. How to add base URL for whole app ?
  // app.setBaseUrl("/api/v1");
  api := app.Group("/api/v1", func(c *fiber.Ctx) {
    c.Send("Hello, World 馃憢!")
    c.Next()
  })
  // app.Get().....
}
func main() {
  app := fiber.New()

  api := app.Group("/api", handler) // /api

  v1 := api.Group("/v1", handler)   // /api/v1
  v1.Get("/list", handler)          // /api/v1/list
  v1.Get("/user", handler)          // /api/v1/user

  v2 := api.Group("/v2", handler)   // /api/v2
  v2.Get("/list", handler)          // /api/v2/list
  v2.Get("/user", handler)          // /api/v2/user

  app.Listen(3000)
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ivan-Feofanov picture Ivan-Feofanov  路  3Comments

Terisback picture Terisback  路  3Comments

Badrouu17 picture Badrouu17  路  4Comments

mewben picture mewben  路  3Comments

ahan picture ahan  路  3Comments