Files
cairnquire/apps/server/internal/httpserver/app_files.go
2026-04-29 09:44:57 -04:00

27 lines
624 B
Go

package httpserver
import (
"net/http"
"os"
"path/filepath"
"strings"
)
func (s *Server) appFileServer() http.Handler {
files := http.FileServer(http.Dir(s.config.Web.DistDir))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cleanPath := filepath.Clean(strings.TrimPrefix(r.URL.Path, "/"))
if cleanPath == "." {
cleanPath = "index.html"
}
target := filepath.Join(s.config.Web.DistDir, cleanPath)
if info, err := os.Stat(target); err == nil && !info.IsDir() {
files.ServeHTTP(w, r)
return
}
http.ServeFile(w, r, filepath.Join(s.config.Web.DistDir, "index.html"))
})
}