在我的情况下,需要捕获请求并检查是否是内部请求。如果不是,则将这些请求重定向到其他处理函数。
网址示例:
1. URL: http://localhost/internal/dosomething1
2. URL: http://localhost/internal/dosomething2/:id
3. URL: http://localhost/overview
4. URL: http://localhost/xxx
只有以开头的 URIinternal应该在我自己的句柄函数中处理(案例 1 和 2)。
具有任何请求方法的其他人将代理到另一个函数(案例 3 和 4)。
我正在尝试这样使用router.Any("/*uri", handlerExternal):
func handlerExternal(c *gin.Context) {
path := c.Param("uri")
if strings.HasPrefix(path, "/internal/") {
uri := strings.Split(path, "/")
switch uri[2] {
case "dosomething1":
doInternal(c)
}
} else {
doExternal(c)
}
}
但是使用此解决方案,doInternal(c)无法捕获路径参数,:id如http://localhost/internal/dosomething2/:id
这种情况有更好的解决方案吗?
温温酱