如何在杜松子酒中组合路线组? [英] How to combine group of routes in gin?

查看:52
本文介绍了如何在杜松子酒中组合路线组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的包中为 gin 路由创建了两个不同的组,分别是/user /todo 一个文件.这是我的 userroutes.go 文件.

I have created two different Groups for gin routing specifically /user and /todo in two different packages and I want to merge them into one file . Here is my userroutes.go file.

package userrouter

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

//UserRoutes for user
func UserRoutes() *gin.RouterGroup {

    r := gin.Default()

    v1 := r.Group("/user")
    {

        v1.GET("/hello", func(c *gin.Context) {
            c.JSON(200, gin.H{
                "message": "pong",
            })
        })

    }
    return v1

}

todoroutes.go

package todorouter

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

//TodoRoutes for creating Todo
func TodoRoutes() *gin.RouterGroup {

    r := gin.Default()

    v2 := r.Group("/todo")
    {

        v2.GET("/hello", func(c *gin.Context) {
            c.JSON(200, gin.H{
                "message": "pong",
            })
        })

    }
    return v2

}

,然后要将它们合并到 routes.go 中.

and then want to merge them in routes.go.

package router

import (
    todo "TODO/api/Routes/Todo/TodoRoutes"
    user "TODO/api/Routes/User/UserRoutes"
    "fmt"

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


//Router for all routes
func Router() {
    // r := gin.Default()
    userRoutes := user.UserRoutes()
    todoRoutes := todo.TodoRoutes()

}

如何将它们合并到一个文件中,以便可以使用一些前缀/api 来获取它们,例如 localhost:8081/api/user/create 本地主机:8081/api/todo/create .另外,我需要在我创建的每条路线中都创建 r:= gin.Default()还是可以一次完成?谢谢.

How can I merge them into one file so that they could be fetched with some prefix /api like localhost:8081/api/user/create or localhost:8081/api/todo/create. Additionaly do I need to create r := gin.Default() in every route I've been creating or it can be accomplished using once? Thanks.

推荐答案

只需创建一个超级组,然后在其下创建您的组.显示我的意思的代码示例:

Just create a super-group and then create your groups under it. Code example to show what I mean:

    router := gin.New()
    
    superGroup := router.Group("/api")
    {
        userGroup := superGroup.Group("/user")
        {
            // user group handlers
        }
    
        todoGroup := superGroup.Group("/todo")
        {
            // todo group handlers
        }
    }

使用此代码,您无法像现在一样创建组,因为您的函数 todorouter.TodoRoutes userrouter.UserRoutes 都实例化了自己的gin.Engine .因此,您必须将顶级引擎和/api 组移动到一个公共位置.

With this code you can't create your groups as you are doing now, because both your functions todorouter.TodoRoutes and userrouter.UserRoutes instantiate their own gin.Engine. So you have to move the top-level engine and /api group to a common place.

如果需要,可以将api组对象作为函数参数传递.示例:

If you want, you can then pass the api group object as a function argument. Example:

//TodoRoutes for creating Todo
func TodoRoutes(apiGroup *gin.RouterGroup) {

    v2 := apiGroup.Group("/todo")
    {

        v2.GET("/hello", func(c *gin.Context) {
            c.JSON(200, gin.H{
                "message": "pong",
            })
        })

    }
}

不过,我建议保持代码井井有条的做法是将所有路由器实例化在同一位置,然后将处理程序放置在单独的程序包中.

However my recommendation to keep your code well organized is to instantiate all routers in the same place, and then place the handlers in separate packages.

// package todoroutes
func HandleHello(c *gin.Context) {
            c.JSON(200, gin.H{
                "message": "pong",
            })
}

// package router
    router := gin.New()
    
    superGroup := router.Group("/api")
    {
        userGroup := superGroup.Group("/user")
        {
             userGroup.GET("/hello", todoroutes.HandleHello)
        }
    }

这篇关于如何在杜松子酒中组合路线组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆