Let’s take a look at the route
package routes
//routes/routes.go
import (
"github.com/gin-gonic/gin"
"myGin/context"
"myGin/controller"
"myGin/response"
)
func Load(r *gin.Engine) {
api := r.Group("/api")
user := api.Group("/user")
user.GET("/info", convert(controller.Index))
}
Copy the code
This writing method of routing group hierarchy is not very clear, such as the late routing is definitely a headache
The ideal model would be this
package routes
//routes/routes.go
import (
"github.com/gin-gonic/gin"
"myGin/context"
"myGin/controller"
"myGin/response"
)
func Load(r *gin.Engine) {
r.Group("/api".func(api router) {
api.Group("/user".func(user router) {
user.GET("/info", convert(controller.Index))
user.GET("/order", convert(controller.Index))
user.GET("/money", convert(controller.Index))
})
})
}
Copy the code
This will implement a Router module
Add a router.go file to the Routes folder
package routes
import "github.com/gin-gonic/gin"
type router struct {
engine *gin.Engine
}
func newRouter(engine *gin.Engine) *router {
return &router{
engine: engine,
}
}
Copy the code
The first step is to instantiate a Router object
The caller looks like this
func Load(r *gin.Engine) {
router:=newRouter(r)
}
Copy the code
The next step is to add a routing group, which is continued at router.go
package routes
////routes/router.go
import "github.com/gin-gonic/gin"
type router struct {
engine *gin.Engine
}
type group struct {
engine *gin.Engine
path string
}
func newRouter(engine *gin.Engine) *router {
return &router{
engine: engine,
}
}
func (r *router) Group(path string, callback func(group)) {
callback(group{
engine: r.engine,
path: path,
})
}
Copy the code
The caller
func Load(r *gin.Engine) {
router := newRouter(r)
router.Group("/api".func(api group){})}Copy the code
Groups can also continue nesting after that
Continue to implement
package routes
////routes/router.go
import "github.com/gin-gonic/gin"
type router struct {
engine *gin.Engine
}
type group struct {
engine *gin.Engine
path string
}
func newRouter(engine *gin.Engine) *router {
return &router{
engine: engine,
}
}
func (r *router) Group(path string, callback func(group)) {
callback(group{
engine: r.engine,
path: path,
})
}
func (g group) Group(path string, callback func(group)) {
g.path += path
callback(g)
}
Copy the code
The caller
func Load(r *gin.Engine) {
router := newRouter(r)
router.Group("/api".func(api group) {
api.Group("/user".func(user group){})})}Copy the code
The last step is to implement the function binding
package routes
////routes/router.go
import (
"github.com/gin-gonic/gin"
"myGin/context"
"myGin/response"
)
type router struct {
engine *gin.Engine
}
type group struct {
engine *gin.Engine
path string
}
type method int
const (
GET method = 0x000000
POST method = 0x000001
PUT method = 0x000002
DELETE method = 0x000003
ANY method = 0x000004
)
func newRouter(engine *gin.Engine) *router {
return &router{
engine: engine,
}
}
func (r *router) Group(path string, callback func(group)) {
callback(group{
engine: r.engine,
path: path,
})
}
func (g group) Group(path string, callback func(group)) {
g.path += path
callback(g)
}
func (g group) Registered(method method, url string, action func(*context.Context) *response.Response) {
handlerFunc := convert(action)
finalUrl := g.path + url
switch method {
case GET:
g.engine.GET(finalUrl, handlerFunc)
case POST:
g.engine.GET(finalUrl, handlerFunc)
case PUT:
g.engine.PUT(finalUrl, handlerFunc)
case DELETE:
g.engine.DELETE(finalUrl, handlerFunc)
case ANY:
g.engine.Any(finalUrl, handlerFunc)
}
}
func convert(f func(*context.Context) *response.Response) gin.HandlerFunc {
return func(c *gin.Context) {
resp := f(&context.Context{Context: c})
data := resp.GetData()
switch item := data.(type) {
case string:
c.String(200, item)
case gin.H:
c.JSON(200, item)
}
}
}
Copy the code
The caller
package routes
//routes/routes.go
import (
"github.com/gin-gonic/gin"
"myGin/controller"
)
func Load(r *gin.Engine) {
router := newRouter(r)
router.Group("/api".func(api group) {
api.Group("/user".func(user group) {
user.Registered(GET, "/info", controller.Index)
user.Registered(GET, "/order", controller.Index)
user.Registered(GET, "/money", controller.Index)
})
})
}
Copy the code
Browser access