Files
Willie ad34dbde2d fix(server): load the dashboard when opened at a nested route
The build links its files as ./assets/..., so a page reloaded at
/targets/claude asked for /targets/assets/... and got index.html back.
The browser refused the chunks as text/html and the page stayed on its
skeleton. The server now injects a <base> for the base path.
2026-09-27 23:03:21 +08:00

91 lines
3.3 KiB
Go

package server
import (
"encoding/json"
"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.
// A <base> tag for basePath, plus a <script> injecting window.__BASE_PATH__ when
// basePath is non-empty, is inserted into index.html after <head>.
func spaHandlerFromDisk(dir, basePath string) http.Handler {
fileServer := http.FileServer(http.Dir(dir))
// Always cache index.html at startup (avoids per-request disk reads on SPA fallback).
// When basePath is set, inject window.__BASE_PATH__ for the frontend.
var cachedIndex []byte
indexPath := filepath.Join(dir, "index.html")
if raw, err := os.ReadFile(indexPath); err == nil {
// The build links its files as ./assets/...; without a base, a page opened at a
// nested route such as /targets/claude asks for /targets/assets/... instead.
encodedHref, _ := json.Marshal(basePath + "/")
injection := "<base href=" + string(encodedHref) + ">"
if basePath != "" {
encoded, _ := json.Marshal(basePath)
injection += "<script>window.__BASE_PATH__=" + string(encoded) + "</script>"
}
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
}
// The file server takes a type from the system, and on Windows the registry
// can get .svg wrong. The browser then refuses to draw the image.
if strings.HasSuffix(path, ".svg") {
w.Header().Set("Content-Type", "image/svg+xml")
}
fileServer.ServeHTTP(w, r)
return
}
// SPA fallback: serve cached index.html
if cachedIndex != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(cachedIndex)
return
}
http.Error(w, "UI assets not found", http.StatusNotFound)
})
}