杜松子酒通配符路线与现有孩子发生冲突 [英] Gin wildcard route conflicts with existing children

查看:106
本文介绍了杜松子酒通配符路线与现有孩子发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个 gin 程序,有以下路线:

I'd like to build a gin program which serves the following routes:

r.GET("/special", ... // Serves a special resource.
r.Any("/*", ...       // Serves a default resource.

但是,这样的程序在运行时会出现紧急情况:

However, such a program panics at runtime:

[GIN-debug] GET    /special                  --> main.main.func1 (2 handlers)
[GIN-debug] GET    /*                        --> main.main.func2 (2 handlers)
panic: wildcard route '*' conflicts with existing children in path '/*'

是否可以创建一个gin程序,该程序为每个路由除了的一个默认资源,而为单个路由提供一个不同的资源?

Is it possible to create a gin program which serves a default resource for every route except for a single one which serves a different resource?

Web上的许多页面使我相信无法使用默认的gin路由器,那么从gin程序提供这些路由的最简单方法是什么?

Many pages on the web lead me to believe it is not possible using the default gin router, so what is the easiest way to serve these routes from a gin program?

推荐答案

在gin.NoRoute()无法接受的情况下,也许其他人(例如我)会收到此错误消息.我从github上获取了以下代码 对于此问题:

Maybe someone else (like me) will have this error message in a situation where gin.NoRoute() won't be an acceptable fix. I took the following code from github in search for a workaround for this issue:

    router.GET("/v1/images/:path1", GetHandler)           //      /v1/images/detail
    router.GET("/v1/images/:path1/:path2", GetHandler)    //      /v1/images/<id>/history

func GetHandler(c *gin.Context) {
    path1 := c.Param("path1")
    path2 := c.Param("path2")

    if path1 == "detail" && path2 == "" {
        Detail(c)
    } else if path1 != "" && path2 == "history" {
        imageId := path1
        History(c, imageId)
    } else {
        HandleHttpError(c, NewHttpError(404, "Page not found"))
    }
}

这篇关于杜松子酒通配符路线与现有孩子发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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