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

我正在尝试使用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'


繁花不似锦
浏览 120回答 1
1回答

米琪卡哇伊

您可以在 r.NoRoute 中 mv proxy funcr.NoRoute(proxy)
打开App,查看更多内容
随时随地看视频慕课网APP