fix: serve built frontend from backend
This commit is contained in:
5
Makefile
5
Makefile
@@ -13,6 +13,9 @@ frontend-dev:
|
|||||||
frontend-build:
|
frontend-build:
|
||||||
npm --prefix apps/web run build
|
npm --prefix apps/web run build
|
||||||
|
|
||||||
|
full-build:
|
||||||
|
npm --prefix apps/web run build
|
||||||
|
go test ./...
|
||||||
|
|
||||||
dev:
|
dev:
|
||||||
@echo "Run backend and frontend in separate terminals."
|
@echo "Run backend and frontend in separate terminals."
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -104,8 +106,11 @@ func NewRouter(cfg config.Config, database *sql.DB, scanService *scanner.Service
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
fs := http.FileServer(http.Dir("./web"))
|
if frontendRoot := detectFrontendRoot(); frontendRoot != "" {
|
||||||
r.Handle("/*", spaFallback(fs))
|
r.NotFound(spaHandler(frontendRoot))
|
||||||
|
} else {
|
||||||
|
log.Printf("frontend assets not found; API routes are available, but web UI is not being served")
|
||||||
|
}
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@@ -644,12 +649,46 @@ func optionalBool(raw string) *bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func spaFallback(next http.Handler) http.HandlerFunc {
|
func spaHandler(root string) http.HandlerFunc {
|
||||||
|
fileServer := http.FileServer(http.Dir(root))
|
||||||
|
indexPath := filepath.Join(root, "index.html")
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if strings.HasPrefix(r.URL.Path, "/api") || strings.HasPrefix(r.URL.Path, "/rest") || strings.HasPrefix(r.URL.Path, "/health") {
|
if strings.HasPrefix(r.URL.Path, "/api") || strings.HasPrefix(r.URL.Path, "/rest") || strings.HasPrefix(r.URL.Path, "/health") {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
|
cleanPath := filepath.Clean(strings.TrimPrefix(r.URL.Path, "/"))
|
||||||
|
if cleanPath == "." {
|
||||||
|
http.ServeFile(w, r, indexPath)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fullPath := filepath.Join(root, cleanPath)
|
||||||
|
if info, err := os.Stat(fullPath); err == nil && !info.IsDir() {
|
||||||
|
fileServer.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.ServeFile(w, r, indexPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func detectFrontendRoot() string {
|
||||||
|
candidates := []string{
|
||||||
|
"./apps/web/dist",
|
||||||
|
"./web",
|
||||||
|
filepath.Join(".", "apps", "web", "dist"),
|
||||||
|
filepath.Join(".", "web"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, candidate := range candidates {
|
||||||
|
info, err := os.Stat(candidate)
|
||||||
|
if err == nil && info.IsDir() {
|
||||||
|
return candidate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user