phase 3 bug fixing
This commit is contained in:
+28
-1
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
@@ -95,11 +97,36 @@ func New(cfg *config.Config, engine *xorm.Engine, store sessions.Store, staticFi
|
||||
})
|
||||
|
||||
r.With(auth.Optional).Get("/ws", wsH.Hub)
|
||||
r.Handle("/*", spaHandler(staticFiles))
|
||||
|
||||
// In debug mode proxy non-API routes to the Vite dev server so :8080 works too.
|
||||
// In production the built React app is embedded and served from staticFiles.
|
||||
if cfg.Debug {
|
||||
r.Handle("/*", viteProxy("http://localhost:5173"))
|
||||
} else {
|
||||
r.Handle("/*", spaHandler(staticFiles))
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func viteProxy(target string) http.Handler {
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Rewrite: func(r *httputil.ProxyRequest) {
|
||||
r.SetURL(mustParseURL(target))
|
||||
r.Out.Host = r.In.Host
|
||||
},
|
||||
}
|
||||
return proxy
|
||||
}
|
||||
|
||||
func mustParseURL(raw string) *url.URL {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func spaHandler(staticFiles fs.FS) http.Handler {
|
||||
fileServer := http.FileServer(http.FS(staticFiles))
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user