Fixed up the .gitignore

More push to move towards a deployment stage
This commit is contained in:
2025-08-21 20:57:23 -04:00
parent 9502d1b1be
commit e2651456da
12 changed files with 379 additions and 265 deletions

View File

@@ -1,14 +1,15 @@
package api
import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
// MountStatic serves files from dir under baseURL. If baseURL == "/", it serves root.
// Directory listings are disabled. Unknown paths fall back to index.html (SPA).
// Mount static on the API mux (kept for compatibility; still serves under API port if you want)
func (s *Server) MountStatic(dir string, baseURL string) {
if dir == "" {
return
@@ -16,20 +17,46 @@ func (s *Server) MountStatic(dir string, baseURL string) {
if baseURL == "" {
baseURL = "/"
}
fs := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.mux.Handle(baseURL, s.staticHandler(dir, baseURL))
if !strings.HasSuffix(baseURL, "/") {
s.mux.Handle(baseURL+"/", s.staticHandler(dir, baseURL))
}
}
// 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
}
log.Printf("frontend listening on %s (dir=%s base=%s)", addr, dir, baseURL)
mx := http.NewServeMux()
mx.Handle(baseURL, s.staticHandler(dir, baseURL))
if !strings.HasSuffix(baseURL, "/") {
mx.Handle(baseURL+"/", s.staticHandler(dir, baseURL))
}
server := &http.Server{
Addr: addr,
Handler: mx,
ReadHeaderTimeout: 5 * time.Second,
}
return server.ListenAndServe()
}
func (s *Server) staticHandler(dir, baseURL string) http.Handler {
if baseURL == "" {
baseURL = "/"
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.secureHeaders(w)
// normalize path inside dir
up := strings.TrimPrefix(r.URL.Path, baseURL)
if up == "" || strings.HasSuffix(r.URL.Path, "/") {
up = "index.html"
}
full := filepath.Join(dir, filepath.FromSlash(up))
// prevent path escape
if !strings.HasPrefix(filepath.Clean(full), filepath.Clean(dir)) {
http.NotFound(w, r)
return
}
// serve if exists, else SPA fallback
if st, err := os.Stat(full); err == nil && !st.IsDir() {
http.ServeFile(w, r, full)
return
@@ -41,14 +68,4 @@ func (s *Server) MountStatic(dir string, baseURL string) {
}
http.NotFound(w, r)
})
// Root or subpath
if baseURL == "/" {
s.mux.Handle("/", fs)
} else {
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
s.mux.Handle(baseURL, fs)
s.mux.Handle(baseURL+"", fs) // ensure exact mount works
}
}