3
votes

I am trying to write a proxy for my api and frontend using Golang and gin. If the request goes to anything except "/api" I want to proxy to svelte server. If goes the "/api/something" I want to handle it in gin. Currently my code is like this.

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")
}

Now if I go to http://localhost:8080 I am seeing my svelte app. But if a want to add any other route I get an error saying panic: catch-all conflicts with existing handle for the path segment root in path '/*proxyPath'

1

1 Answers

1
votes

You can mv proxy func in r.NoRoute r.NoRoute(proxy)