11
votes

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()
...
2
I had looked at that question and its answer. However, the question and answer were unclear, and somewhat incorrect, as they used local import paths. The answer was also unclear as to what the solution was. I felt my question & answer are more clear.watr
This is beside the point. If you have a better answer to an existing question, post it as an answer to that question. If the existing question is unclear, edit it. As it is, your question is clearly a duplicate.Zoyd
Upon deeper review of the question that was noted as being a prior similar quesiton, and upon review of the answers, I find my question and answer are different enough, that there is sufficient good derived from it by the community for it to exist as a separate question.watr
I like your questions and answers. It's really better and clearer than in duplicate question, thank you.Carson
I'm voting to reopen because as it currently stands, this question is at 6k views with a 25-score answer, whereas the dupe target is at 2k views with a 1-score answer. The dupes should be inverted. The answer quality also is much better hereblackgreen

2 Answers

30
votes

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()
...
3
votes

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()
...