Files
skillshare/internal/server/spa_disk.go
T
Willie 768c9f4a67 feat(server): inject __BASE_PATH__ into SPA index.html when base path is set
When spaHandlerFromDisk receives a non-empty basePath, it pre-computes
a modified index.html with a <script> tag injecting window.__BASE_PATH__.
This allows the frontend to discover its base path at runtime without
build-time configuration. The injection applies to both direct index.html
requests and SPA fallback routes.
2026-03-18 22:35:02 +08:00

87 lines
2.9 KiB
Go

package server
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
// uiPlaceholderHandler returns a handler for when no UI dist is available.
// Used in dev mode (Vite on :5173) or API-only scenarios.
func uiPlaceholderHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`<!DOCTYPE html>
<html>
<head><title>Skillshare UI (dev mode)</title>
<style>body{font-family:system-ui,sans-serif;max-width:600px;margin:60px auto;padding:0 20px;color:#333}
code{background:#f0f0f0;padding:2px 6px;border-radius:3px;font-size:0.9em}
pre{background:#f0f0f0;padding:12px 16px;border-radius:6px;overflow-x:auto}</style>
</head>
<body>
<h1>Skillshare UI — Dev Mode</h1>
<p>This is the API server. The frontend needs to be started separately:</p>
<pre>cd ui && pnpm run dev</pre>
<p>Then open <a href="http://localhost:5173">http://localhost:5173</a> (Vite proxies <code>/api</code> to this server).</p>
<p>Or use <code>make ui-dev</code> to start both together.</p>
<hr>
<p style="color:#888;font-size:0.85em">In production builds, <code>skillshare ui</code> downloads and serves the frontend automatically.</p>
</body>
</html>`))
})
}
// spaHandlerFromDisk serves a SPA from a directory on disk.
// Unknown paths fall back to index.html for client-side routing.
// When basePath is non-empty, a <script> tag injecting window.__BASE_PATH__
// is inserted into index.html after <head>.
func spaHandlerFromDisk(dir, basePath string) http.Handler {
fileServer := http.FileServer(http.Dir(dir))
// Pre-compute injected index.html if basePath is set
var cachedIndex []byte
if basePath != "" {
indexPath := filepath.Join(dir, "index.html")
if raw, err := os.ReadFile(indexPath); err == nil {
injection := fmt.Sprintf("<script>window.__BASE_PATH__='%s'</script>", basePath)
cachedIndex = []byte(strings.Replace(string(raw), "<head>", "<head>"+injection, 1))
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Try to serve the file directly
path := strings.TrimPrefix(r.URL.Path, "/")
if path == "" {
path = "index.html"
}
fullPath := filepath.Join(dir, path)
if _, err := os.Stat(fullPath); err == nil {
if path == "index.html" && cachedIndex != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(cachedIndex)
return
}
fileServer.ServeHTTP(w, r)
return
}
// SPA fallback: serve index.html (possibly injected)
if cachedIndex != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(cachedIndex)
return
}
indexPath := filepath.Join(dir, "index.html")
index, err := os.ReadFile(indexPath)
if err != nil {
http.Error(w, "UI assets not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(index)
})
}