M7: Frontend - HTML/JS/CSS mit dunklem Design, Vollbild, WebSocket-Updates, embed.FS-Integration
- Frontend: index.html, style.css, app.js - Dunkles Kontrast-Design mit Team-Farben (Rot/Grün) - Vollbild-Toggle, Responsive Design - Echtzeit-Updates über WebSocket + Polling-Fallback - Tastatur-Shortcuts (R/G für Punkte, N für nächste Runde) - Rundenverlauf mit Farbcodierung - Go-Integration: - frontend.go: embed.FS für statische Dateien - websocket.go: WebSocket-Handler (/ws) für State-Broadcast - httpapi.go: broadcastState() nach jeder Mutation, /ws-Route - main.go: api.SetupFrontend() für Frontend-Einbettung - Frontend unter '/' verfügbar, API unter '/api/' - Tests: 17 End-to-End-Tests bestanden - Alle HTTP-Endpunkte funktionsfähig - Frontend-Assets korrekt eingebettet - Persistenz (state.json) funktioniert
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed static/index.html static/style.css static/app.js
|
||||
var staticFS embed.FS
|
||||
|
||||
// staticFiles holds the embedded static files.
|
||||
var staticFiles = map[string]string{
|
||||
"/": "static/index.html",
|
||||
"/index.html": "static/index.html",
|
||||
"/style.css": "static/style.css",
|
||||
"/app.js": "static/app.js",
|
||||
}
|
||||
|
||||
// frontendHandler serves the embedded frontend files.
|
||||
func frontendHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
|
||||
// Look up the file
|
||||
filePath, ok := staticFiles[path]
|
||||
if !ok {
|
||||
// Default to index.html for unknown routes (SPA-style)
|
||||
filePath = staticFiles["/"]
|
||||
}
|
||||
|
||||
data, err := staticFS.ReadFile(filePath)
|
||||
if err != nil {
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Set content type based on file extension
|
||||
ext := strings.ToLower(filepath.Ext(filePath))
|
||||
contentType := mime.TypeByExtension(ext)
|
||||
if contentType == "" {
|
||||
switch ext {
|
||||
case ".html":
|
||||
contentType = "text/html; charset=utf-8"
|
||||
case ".css":
|
||||
contentType = "text/css; charset=utf-8"
|
||||
case ".js":
|
||||
contentType = "application/javascript; charset=utf-8"
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// SetupFrontend registers the frontend handler on the given ServeMux.
|
||||
func SetupFrontend(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/", frontendHandler)
|
||||
}
|
||||
Reference in New Issue
Block a user