英文:
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()
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论