为了账号安全,请及时绑定邮箱和手机立即绑定

Golang gin proxy 来处理 svelte 前端和 Golang api

Golang gin proxy 来处理 svelte 前端和 Golang api

Go
繁花不似锦 2022-09-05 10:25:59
我正在尝试使用Golang和gin为我的api和前端编写代理。如果请求转到除“/api”之外的任何内容,我想代理到svelte服务器。如果去“/api/something”,我想在杜松子酒中处理它。目前我的代码是这样的。func proxy(c *gin.Context) {    remote, err := url.Parse("http://localhost:3000")    if err != nil {        panic(err)    }    proxy := httputil.NewSingleHostReverseProxy(remote)    proxy.Director = func(req *http.Request) {        req.Header = c.Request.Header        req.Host = remote.Host        req.URL.Scheme = remote.Scheme        req.URL.Host = remote.Host        req.URL.Path = c.Param("proxyPath")    }    proxy.ServeHTTP(c.Writer, c.Request)}func main() {    r := gin.Default()    r.Any("/*proxyPath", proxy)    r.Run(":8080")}现在,如果我去,我看到我的苗条的应用程序。但是,如果想要添加任何其他路由,我会收到一个错误http://localhost:8080panic: catch-all conflicts with existing handle for the path segment root in path '/*proxyPath'
查看完整描述

1 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

您可以在 r.NoRoute 中 mv proxy funcr.NoRoute(proxy)



查看完整回答
反对 回复 2022-09-05
  • 1 回答
  • 0 关注
  • 105 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信