With node/express its possible to do something like this
app.use(rewrite('/*', '/index.html'));
What would the equivalent in go be? I've tried using the httputil.ReverseProxy, but that seems entirely impractical.
With node/express its possible to do something like this
app.use(rewrite('/*', '/index.html'));
What would the equivalent in go be? I've tried using the httputil.ReverseProxy, but that seems entirely impractical.
For a simple "catch all" you can just do
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
The "/" pattern matches everything.
For a more complex pattern, you need to wrap your mux with a handler that rewrites the url.
// register your handlers on a new mux
mux := http.NewServeMux()
mux.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
// your handler code
})
...
rewrite := func(path string) string {
// your rewrite code, returns the new path
}
...
// rewrite URL.Path in here before calling mux.ServeHTTP
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = rewrite(r.URL.Path)
mux.ServeHTTP(w,r)
})