Files
skillshare/internal/server/spa_disk.go
T
Willie 5aae64539e fix(server): serve SVG icons as images whatever the system says
Go takes a file's type from the Windows registry when it has one, and a
machine whose registry gets .svg wrong was served icons the browser
refused to draw. Only logos too large to inline are files at all, which is
why Antigravity's broke while the rest showed.

Refs #289
2026-09-21 22:23:12 +08:00

89 lines
3.1 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.
// 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))
// 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 {
if basePath != "" {
encoded, _ := json.Marshal(basePath)
injection := "<script>window.__BASE_PATH__=" + string(encoded) + "</script>"
cachedIndex = []byte(strings.Replace(string(raw), "<head>", "<head>"+injection, 1))
} else {
cachedIndex = raw
}
}
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)
})
}