Gin: Sub-groups of routes

Created on 20 Dec 2017  路  2Comments  路  Source: gin-gonic/gin

Is it possible to sub-group routes?
i.e.

func main (){
      r = gin.Default()
      v1 := r.Group("/api/v1"){
            auth := v1.Group("/auth/"){
                auth.GET("/users", userListHandler)
                auth.POST("/login", loginHandler)
            }
      }
}

This would allow mixing and matching api versions and functions.

Most helpful comment

That is easy to try:

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()

    v1 := r.Group("/api/v1")
    {
        auth := v1.Group("/auth")
        {
            auth.GET("/", func(c *gin.Context) {
                c.JSON(200, gin.H{
                    "v1-auth": true,
                })
            })
        }

    }
    r.Run() // listen and serve on 0.0.0.0:8080
}

Running this logs

[GIN-debug] GET    /api/v1/auth/             --> main.main.func1 (3 handlers)

So yes, this is possible.

All 2 comments

That is easy to try:

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()

    v1 := r.Group("/api/v1")
    {
        auth := v1.Group("/auth")
        {
            auth.GET("/", func(c *gin.Context) {
                c.JSON(200, gin.H{
                    "v1-auth": true,
                })
            })
        }

    }
    r.Run() // listen and serve on 0.0.0.0:8080
}

Running this logs

[GIN-debug] GET    /api/v1/auth/             --> main.main.func1 (3 handlers)

So yes, this is possible.

@devshell yes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lilee picture lilee  路  3Comments

rawoke083 picture rawoke083  路  3Comments

mdnight picture mdnight  路  3Comments

gplume picture gplume  路  3Comments

xpbliss picture xpbliss  路  3Comments