Fiber: 馃殌 Add Routing Middleware

Created on 23 Aug 2020  路  9Comments  路  Source: gofiber/fiber

Add Routing Middleware

package router

import "github.com/gofiber/fiber"

func Routes() *fiber.App{
    app := fiber.New()
    api := app.Group("/api")

    app.Use(Web)

    v1 := api.Group("/v1", func(c *fiber.Ctx){
        c.JSON(fiber.Map{
          "message":"v1",
        })

        c.Next()
    }) 

    v1.Use(Api)

    return app
}

so we can easily and cleaner to do routing.

Like this :
https://expressjs.com/en/guide/using-middleware.html#middleware.router

馃彿 Wait for Release

Most helpful comment

If I'm correct, you want to be able to register/mount another fiber instance?
This is currently not possible, but this is something we might implement in v2.x

package main

import (
    "log"

    "github.com/gofiber/fiber"
)

var handler = func(c *fiber.Ctx) {}

func v1() fiber.Router {
    app := fiber.New()
    app.Get("/john", handler)
    return app
}

func v2() fiber.Router {
    app := fiber.New()
    app.Get("/john", handler)
    app.Post("/doe", handler)
    return app
}

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

    app.Use("/v1", v1()) // GET  /v1/john

    app.Use("/v2", v2()) // GET  /v2/john
                 // POST /v2/doe

    log.Fatal(app.Listen(":3000"))
}

All 9 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

If I'm correct, you want to be able to register/mount another fiber instance?
This is currently not possible, but this is something we might implement in v2.x

package main

import (
    "log"

    "github.com/gofiber/fiber"
)

var handler = func(c *fiber.Ctx) {}

func v1() fiber.Router {
    app := fiber.New()
    app.Get("/john", handler)
    return app
}

func v2() fiber.Router {
    app := fiber.New()
    app.Get("/john", handler)
    app.Post("/doe", handler)
    return app
}

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

    app.Use("/v1", v1()) // GET  /v1/john

    app.Use("/v2", v2()) // GET  /v2/john
                 // POST /v2/doe

    log.Fatal(app.Listen(":3000"))
}

If I'm correct, you want to be able to register/mount another fiber instance?
This is currently not possible, but this is something we might implement in v2.x

package main

import (
  "log"

  "github.com/gofiber/fiber"
)

var handler = func(c *fiber.Ctx) {}

func v1() fiber.Router {
  app := fiber.New()
  app.Get("/john", handler)
  return app
}

func v2() fiber.Router {
  app := fiber.New()
  app.Get("/john", handler)
  app.Post("/doe", handler)
  return app
}

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

  app.Use("/v1", v1()) // GET  /v1/john

  app.Use("/v2", v2()) // GET  /v2/john
               // POST /v2/doe

  log.Fatal(app.Listen(":3000"))
}

yes, that will be good to. can't wait for it.

I also want this to be in fiber 馃槏.

Just a suggestion, instead of calling it fiber.New() could it be called fiber.Router(), just like express.

I also want this to be in fiber 馃槏.

Just a suggestion, instead of calling it fiber.New() could it be called fiber.Router(), just like express.

This is not possible because Router is a reserved interface for *App and *Group

This is not possible because Router is a reserved interface for *App and *Group

Yeah, I forgot about that.

Maybe something else can be used. IMO calling fiber.New() for a router, looks like spawning a new fiber app. Thoughts?

Calling fiber.New() will create a new instance that can be used as a router.
We should document the behavior if a Group or App is registered using Use

// Use registers a middleware handler or Router. that will match requests
// that contain the provided prefix ( which is optional and defaults to "/" ).
//
// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...

@fauzan121002, unfortunately, this idea did not make it with the v2 release. We are working on to have a similar functionality ready for v2.1.x, I will keep this issue open for development updates.

@fauzan121002, a quick update regarding this issue.

https://github.com/gofiber/fiber/pull/830 introduces the new Mount method to embed another Fiber instance to a prefix path.

    // some micro service
    micro := fiber.New()
    micro.Get("/doe", func(c *fiberCtx) error {
        return c.SendStatus(StatusOK)
    })

    // main.go
    app := fiber.New()
    app.Mount("/john", micro)

    // GET /john/doe -> 200 OK
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahan picture ahan  路  3Comments

koddr picture koddr  路  4Comments

lucasmdomingues picture lucasmdomingues  路  3Comments

mewben picture mewben  路  3Comments

Ivan-Feofanov picture Ivan-Feofanov  路  3Comments