Doing some testing to make sure that Cloudflare works with the app
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -9,7 +10,14 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Mount static on the API mux (kept for compatibility; still serves under API port if you want)
|
||||
func init() {
|
||||
// Ensure common types are known (some distros are sparse by default)
|
||||
_ = mime.AddExtensionType(".js", "application/javascript; charset=utf-8")
|
||||
_ = mime.AddExtensionType(".css", "text/css; charset=utf-8")
|
||||
_ = mime.AddExtensionType(".html", "text/html; charset=utf-8")
|
||||
_ = mime.AddExtensionType(".map", "application/json; charset=utf-8")
|
||||
}
|
||||
|
||||
func (s *Server) MountStatic(dir string, baseURL string) {
|
||||
if dir == "" {
|
||||
return
|
||||
@@ -23,7 +31,6 @@ func (s *Server) MountStatic(dir string, baseURL string) {
|
||||
}
|
||||
}
|
||||
|
||||
// NEW: serve the same static handler on its own port (frontend).
|
||||
func (s *Server) ListenFrontendHTTP(addr, dir, baseURL string) error {
|
||||
if dir == "" || addr == "" {
|
||||
return nil
|
||||
@@ -48,6 +55,7 @@ func (s *Server) staticHandler(dir, baseURL string) http.Handler {
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
s.secureHeaders(w)
|
||||
|
||||
up := strings.TrimPrefix(r.URL.Path, baseURL)
|
||||
if up == "" || strings.HasSuffix(r.URL.Path, "/") {
|
||||
up = "index.html"
|
||||
@@ -57,12 +65,19 @@ func (s *Server) staticHandler(dir, baseURL string) http.Handler {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Serve file if it exists, else SPA-fallback to index.html
|
||||
if st, err := os.Stat(full); err == nil && !st.IsDir() {
|
||||
// Set Content-Type explicitly based on extension
|
||||
if ctype := mime.TypeByExtension(filepath.Ext(full)); ctype != "" {
|
||||
w.Header().Set("Content-Type", ctype)
|
||||
}
|
||||
http.ServeFile(w, r, full)
|
||||
return
|
||||
}
|
||||
fallback := filepath.Join(dir, "index.html")
|
||||
if _, err := os.Stat(fallback); err == nil {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
http.ServeFile(w, r, fallback)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user