Make proxy work with websockets

This commit is contained in:
2021-11-02 10:17:10 +01:00
parent e7f0d1a879
commit 0d78078f8e

View File

@@ -102,20 +102,28 @@ func New(
} }
if proxyAPIRequests { if proxyAPIRequests {
proxPath := strings.TrimSuffix(apiURLInternal, "/api") remoteURL, err := url.Parse(strings.TrimSuffix(apiURLInternal, "/api"))
proxURL, err := url.Parse(proxPath)
if err != nil { if err != nil {
panic(fmt.Errorf("failed to parse reverse proxy URL '%s': %w", proxPath, err)) panic(fmt.Errorf("failed to parse reverse proxy URL '%s': %w", apiURLInternal, err))
} }
var prox = httputil.NewSingleHostReverseProxy(proxURL) log.Info("Starting API proxy to %s", remoteURL)
var prox = httputil.NewSingleHostReverseProxy(remoteURL)
prox.Transport = wc.httpClient.Transport prox.Transport = wc.httpClient.Transport
r.Handler("OPTIONS", "/api/*p", prox)
r.Handler("POST", "/api/*p", prox) var proxyHandler = func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
r.Handler("GET", "/api/*p", prox) log.Info("Proxying request to %s", r.URL)
r.Handler("PUT", "/api/*p", prox) r.Host = remoteURL.Host
r.Handler("PATCH", "/api/*p", prox) r.Header.Set("Origin", remoteURL.String())
r.Handler("DELETE", "/api/*p", prox) prox.ServeHTTP(w, r)
}
r.Handle("OPTIONS", "/api/*p", proxyHandler)
r.Handle("POST", "/api/*p", proxyHandler)
r.Handle("GET", "/api/*p", proxyHandler)
r.Handle("PUT", "/api/*p", proxyHandler)
r.Handle("PATCH", "/api/*p", proxyHandler)
r.Handle("DELETE", "/api/*p", proxyHandler)
} }
r.NotFound = http.HandlerFunc(wc.serveNotFound) r.NotFound = http.HandlerFunc(wc.serveNotFound)
@@ -134,10 +142,8 @@ func New(
{GET, "api" /* */, wc.serveMarkdown("api.md", handlerOpts{})}, {GET, "api" /* */, wc.serveMarkdown("api.md", handlerOpts{})},
{GET, "history" /* */, wc.serveTemplate("history_cookies", handlerOpts{})}, {GET, "history" /* */, wc.serveTemplate("history_cookies", handlerOpts{})},
{GET, "u/:id/preview" /* */, wc.serveFilePreview}, {GET, "u/:id/preview" /* */, wc.serveFilePreview},
{GET, "u/:id" /* */, wc.serveSvelteFile}, {GET, "u/:id" /* */, wc.serveFileViewer},
{GET, "l/:id" /* */, wc.serveSvelteList}, {GET, "l/:id" /* */, wc.serveListViewer},
{GET, "u_old/:id" /* */, wc.serveFileViewer},
{GET, "l_old/:id" /* */, wc.serveListViewer},
{GET, "d/*path" /* */, wc.serveDirectory}, {GET, "d/*path" /* */, wc.serveDirectory},
{GET, "t" /* */, wc.serveTemplate("text_editor", handlerOpts{})}, {GET, "t" /* */, wc.serveTemplate("text_editor", handlerOpts{})},
{GET, "donation" /* */, wc.serveMarkdown("donation.md", handlerOpts{})}, {GET, "donation" /* */, wc.serveMarkdown("donation.md", handlerOpts{})},