Gin Framework – Part 4

What is a Routing Group?

From the literal meaning, a routing group refers to a group of routes. So what kind of routes can be classified into a group? In actual use, the more common scenario is to group according to the version. Such as the following figure:

Hori Systems – Go Gin – Routing Group

Use Grammar

A function is provided in the Gin framework to Group manage routes in groups. The usage syntax is as follows:

  • No nested groups:
v1 := engine.Group("v1")
{
	v1.GET("/path/xxx", func(context *gin.Context) {
		...
	})
	v1.POST("/user/xxx", func(context *gin.Context) {
		...
	})
}
  • Nested grouping:
api := engine.Group("api")
{
	user := api.Group("user")
	{
		user.GET("path", func(context *gin.Context) {
			...
		})
    user.POST("path", func(context *gin.Context) {
			...
		})
	}
}

Multi-version Combat (without nested routing)

Code example

//-- main.go ------- 
package main

import (
	 "github.com/gin-gonic/gin"  // Import Gin framework 
	"go_use/practise"  // Code sample package
)

func  main () {
	 // create a default routing engine 
	 engine := gin.Default()
	 // register routes 
	practise.TestRouteGroup(engine)
	_ = engine.Run(":9090")
}

//--- go_use/practise/routing_use.go ------- 
// --- routing group using 
func  TestRouteGroup(engine * gin.Engine) {
	 // routing group A (v1 version) 
	 v1 : = engine.Group("v1")
	{
		v1.GET("/user/login", func(context *gin.Context) {
			context.JSON(200, gin.H{"msg": "v1-login successful"})
		})
		v1.GET("/user/register", func(context *gin.Context) {
			context.JSON(200, gin.H{"msg": "v1-registered successfully"})
		})
		v1.GET("/user/del", func(context *gin.Context) {
			context.JSON(200, gin.H{"msg": "v1-logout successful"})
		})
	}
	// routing group B (v2 version) 
	v2 := engine.Group("v2")
	{
		v2.GET("/user/login", func(context *gin.Context) {
			context.JSON(200, gin.H{"msg": "v2-login successful"})
		})
		v2.GET("/user/out", func(context *gin.Context) {
			context.JSON(200, gin.H{"msg": "v2-user logout"})
		})
	}
}
  • Service startup
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)
#Registered routes are as follows 
[GIN-debug] GET /v1/user/login -- > go_use/practise.TestRouteGroup.func1 (3 handlers)
[GIN-debug] GET    /v1/user/register         --> go_use/practise.TestRouteGroup.func2 (3 handlers)
[GIN-debug] GET    /v1/user/del              --> go_use/practise.TestRouteGroup.func3 (3 handlers)
[GIN-debug] GET    /v2/user/login            --> go_use/practise.TestRouteGroup.func4 (3 handlers)
[GIN-debug] GET    /v2/user/out              --> go_use/practise.TestRouteGroup.func5 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :9090 by default
[GIN-debug] Listening and serving HTTP on :9090
  • Request return
$ curl -X GET http://127.0.0.1:9090/v1/user/login
{"msg": "v1-login successful"}
$ curl -X GET http://127.0.0.1:9090/v1/user/register
{"msg": "v1-registered successfully" }
$ curl -X GET http://127.0.0.1:9090/v1/user/del
{"msg": "v1-logout successful" }
$ curl -X GET http://127.0.0.1:9090/v2/user/login
{"msg": "v2-login successful" }
$ curl -X GET http://127.0.0.1:9090/v2/user/out
{"msg": "v2-user-logout"}
$ curl -X GET http://127.0.0.1:9090/v2/user/del
404 page not found

Through the above example, we confirmed the Gin framework can perform routing grouping by version, it does not support API version inheritance, such as accessing: http://127.0.0.1:9090/v2/user/del, expecting to return{“msg”:”v1-logout successful”}

Module separation (nested routing)

  • Code Example
//-- main.go -------
package main

import (
	"go_use/practise"

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

func main() {
	// Create a default routing engine
	engine := gin.Default()
	// Register route
	practise.TestRouteGroupNested(engine)
	_ = engine.Run(":9090")
}

//--- go_use/practise/routing_use.go -------
// Route Nested Groups
func TestRouteGroupNested(engine *gin.Engine) {
	// Interface
	api := engine.Group("api")
	{
		user := api.Group("user")
		{
			user.GET("login", func(context *gin.Context) {
				context.JSON(200, gin.H{"msg": "api-login successful"})
			})
		}
	}
	// Backstage
	admin := engine.Group("admin")
	{
		user := admin.Group("user")
		{
			user.GET("login", func(context *gin.Context) {
				context.JSON(200, gin.H{"msg": "admin-login successful"})
			})
		}
	}
}
  • Service startup
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)
#Registered routes are as follows 
[GIN-debug] GET /api/user/login -- > go_use/practise.TestRouteGroupNested.func1 (3 handlers)
[GIN-debug] GET    /admin/user/login         --> go_use/practise.TestRouteGroupNested.func2 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :9090 by default
[GIN-debug] Listening and serving HTTP on :9090
  • Request return
$ curl  - X  GET http: //127.0.0.1:9090/api/user/login 
{"msg": "api-login successful"}
$ curl  - X  GET http: //127.0.0.1:9090/admin/user/login 
{"msg": "admin-login successful"}