Add complete Web UI infrastructure: Go backend: - REST API handlers for messages, agents, channels (internal/api/) - SSE hub for real-time event streaming - Session-to-owner middleware bridging auth sessions to API context - SPA file server with go:embed for static assets - GetMessageByID on MessagingService, RevokeKey on AgentService - Updated router with RouterConfig for full service wiring Svelte 5 SPA (web/): - SvelteKit with static adapter for SPA mode - Tailwind CSS with dark mode (class-based, localStorage persisted) - API client with auto-redirect on 401 - SSE client with exponential backoff reconnect - Pages: Login, Dashboard, Conversations, Channels, Agents, Settings - Components: Sidebar, Header, MessageList, ComposeForm, AgentCard, TraceViewer - Responsive layout with mobile sidebar toggle Build: - Placeholder index.html in internal/web/dist/ for go:embed compilation - Updated Makefile web target to copy build output - All existing Go tests pass, CGO_ENABLED=0 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package web
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// NewSPAHandler returns an http.Handler that serves the embedded SPA.
|
|
// Static files are served directly; all other routes serve index.html
|
|
// to support client-side routing.
|
|
func NewSPAHandler() http.Handler {
|
|
subFS, err := fs.Sub(DistFS, "dist")
|
|
if err != nil {
|
|
// Should never happen — if dist/ is missing the binary won't compile.
|
|
panic("web: embedded dist/ directory not found: " + err.Error())
|
|
}
|
|
|
|
fileServer := http.FileServer(http.FS(subFS))
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
path := r.URL.Path
|
|
|
|
// Strip leading slash for fs.Open
|
|
fsPath := strings.TrimPrefix(path, "/")
|
|
if fsPath == "" {
|
|
fsPath = "index.html"
|
|
}
|
|
|
|
// Try to open the file in the embedded FS
|
|
f, err := subFS.Open(fsPath)
|
|
if err == nil {
|
|
f.Close()
|
|
// File exists — serve it directly
|
|
fileServer.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
// File doesn't exist — serve index.html for SPA routing
|
|
r.URL.Path = "/"
|
|
fileServer.ServeHTTP(w, r)
|
|
})
|
|
}
|