Golang Gin-Gonic 将路由拆分为多个文件

huangapple go评论148阅读模式
英文:

Golang Gin-Gonic Split Routes into Multiple Files

问题

我正在寻找将我的routes.go文件拆分为多个文件的方法,以便每个路由组都在自己的包中。有人可以给我指出一个使用Gin框架实现这个的代码示例吗?

例如:

package auth
...
auth = route.Group("/auth"){
    auth.GET(...
    auth.POST(...
}
...

package users
...
user = route.Group("/user"){
    user.GET(...
    user.POST(...
}
...

package main
import (
     "auth"
     "users"
)
...
router = gin.Default()
router.Register(auth.auth, users.user)
router.Run()
英文:

I am looking to split my routes.go into multiple files so that each group is in its own package. Can someone point me to an example of some code where someone has done this with Gin?

i.e.

package auth
...
auth = route.Group("/auth"){
    auth.GET(...
    auth.POST(...
}
...

package users
...
user = route.Group("/user"){
    user.GET(...
    user.POST(...
}
...

package main
import (
     "auth"
     "users"
)
...
router = gin.Default()
router.Register(auth.auth, users.user)
router.Run()
...

答案1

得分: 35

这样做的方法是在每个包中创建一个以路由为参数的函数,然后将路由添加到参数中:

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

func Routes(route *gin.Engine) {
    auth := route.Group("/auth")
    {
        auth.GET(...)
        auth.POST(...)
    }
}
...

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

func Routes(route *gin.Engine) {
    user := route.Group("/user")
    {
        user.GET(...)
        user.POST(...)
    }
}
...

package main
import (
    "github.com/username/package/sub/auth"
    "github.com/username/package/sub/users"
    "github.com/gin-gonic/gin"
)
...

router := gin.Default()
auth.Routes(router) // 添加所有 auth 路由
users.Routes(router) // 添加所有 user 路由
router.Run()

这段代码的作用是在使用 Gin 框架的应用程序中添加路由。auth.Routes(router) 将添加所有 auth 包中定义的路由,users.Routes(router) 将添加所有 users 包中定义的路由。最后,通过 router.Run() 启动应用程序的服务器。

英文:

The way to do this is to create a function in each that takes a route as a parameter, and then adds the routes to the parameter:

package auth
import "...gin"
func Routes(route *gin.Engine)
auth := route.Group("/auth"){
    auth.GET(...
    auth.POST(...
}
...

package users
import "...gin"
func Routes(route *gin.Engine)
user := route.Group("/user"){
    user.GET(...
    user.POST(...
}
...

package main
import (
     "github.com/username/package/sub/auth"
     "github.com/username/package/sub/users"
     "github.com/gin-gonic/gin"
)
...
router := gin.Default()
auth.Routes(router) //Added all auth routes
user.Routes(router) //Added all user routes
router.Run()
...

答案2

得分: 5

另一个例子,不同的视角... 主要组在主文件中,子组放置在一个名为groups的不同文件中

package groups
import "...gin"
func Customer(g *gin.RouterGroup) {
  g.GET("/authorize", customer.Authorize)
  g.POST("/register", customer.Register)
}
...

package groups
import "...gin"
func Info(g *gin.RouterGroup) {
  g.GET("/car-color", controllers.CarColorsList)
  g.GET("/car-type", controllers.CarTypesList)
  g.GET("/car-manufacturer", controllers.CarManufacturersList)
  g.GET("/car-model", controllers.CarModelsList)
}
...

package main
import (
     "github.com/gin-gonic/gin"
     "github.com/username/package/api/groups"
)
...
router := gin.Default()
v1 := router.Group("/v1")
{
 v1.Use(AuthMiddleware)
 groups.Info(v1.Group("/info"))
 groups.Customer(v1.Group("/customer"))
}
router.Run()
英文:

another example, different perspective... main group in main file, sub groups placed in different files in one directory groups

package groups
import "...gin"
func Customer(g *gin.RouterGroup) {
  g.GET("/authorize", customer.Authorize)
  g.POST("/register", customer.Register)
}
...

package groups
import "...gin"
func Info(g *gin.RouterGroup) {
  g.GET("/car-color", controllers.CarColorsList)
  g.GET("/car-type", controllers.CarTypesList)
  g.GET("/car-manufacturer", controllers.CarManufacturersList)
  g.GET("/car-model", controllers.CarModelsList)
}
...

package main
import (
     "github.com/gin-gonic/gin"
     "github.com/username/package/api/groups"
)
...
router := gin.Default()
v1 := router.Group("/v1")
{
 v1.Use(AuthMiddleware)
 groups.Info(v1.Group("/info"))
 groups.Customer(v1.Group("/customer"))
}
router.Run()
...

huangapple
  • 本文由 发表于 2017年3月23日 12:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/42967235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定