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:
2026-07-14 16:32:21 +02:00
parent ac9d7eff1d
commit bd05edfb9c
9 changed files with 1669 additions and 30 deletions
+36 -9
View File
@@ -12,19 +12,24 @@ import (
"git.jlserver.de/goSportpunktetafel/internal/logik"
)
// SaveFunc is the type for saving the scoreboard state.
type SaveFunc func(state interface{}) error
// Server represents the HTTP API server.
type Server struct {
addr string
sb *logik.Scoreboard
mux *http.ServeMux
addr string
sb *logik.Scoreboard
mux *http.ServeMux
saveFunc SaveFunc
}
// NewServer creates a new HTTP API server.
func NewServer(addr string, sb *logik.Scoreboard) *Server {
func NewServer(addr string, sb *logik.Scoreboard, save SaveFunc) *Server {
s := &Server{
addr: addr,
sb: sb,
mux: http.NewServeMux(),
addr: addr,
sb: sb,
mux: http.NewServeMux(),
saveFunc: save,
}
s.setupRoutes()
return s
@@ -55,6 +60,15 @@ func (s *Server) setupRoutes() {
s.mux.HandleFunc("/api/next", s.handleNextRound)
s.mux.HandleFunc("/api/reset", s.handleReset)
s.mux.HandleFunc("/api/", s.handleAPI)
s.mux.HandleFunc("/ws", s.wsHandler)
}
func (s *Server) persist() {
if s.saveFunc != nil {
if err := s.saveFunc(s.sb); err != nil {
fmt.Fprintf(os.Stderr, "[persist] Save error: %v\n", err)
}
}
}
func (s *Server) handleScore(w http.ResponseWriter, r *http.Request) {
@@ -67,10 +81,14 @@ func (s *Server) handleScore(w http.ResponseWriter, r *http.Request) {
switch strings.ToLower(team) {
case "red", "rot":
s.sb.Score(logik.TeamRed)
s.persist()
s.broadcastState()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status":"ok","team":"rot","score":%d}`, s.sb.RedScore)
case "green", "gruen":
s.sb.Score(logik.TeamGreen)
s.persist()
s.broadcastState()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status":"ok","team":"gruen","score":%d}`, s.sb.GreenScore)
default:
@@ -102,6 +120,8 @@ func (s *Server) handleNextRound(w http.ResponseWriter, r *http.Request) {
}
s.sb.NextRound()
s.persist()
s.broadcastState()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status":"ok","round":%d}`, s.sb.CurrentRound)
}
@@ -113,6 +133,8 @@ func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
}
s.sb.Reset()
s.persist()
s.broadcastState()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status":"ok"}`)
}
@@ -120,8 +142,8 @@ func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleAPI(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"service": "sportscore",
"version": "1.0",
"service": "sportscore",
"version": "1.0",
"endpoints": []string{
"GET /api/state",
"POST /api/score?team=red|green",
@@ -139,3 +161,8 @@ func PrintFlags(port int, bindAddr string) {
}
fmt.Fprintf(os.Stderr, "[config] HTTP API: %s:%d\n", addr, port)
}
// SetupFrontend registers the frontend handler on the server's mux.
func (s *Server) SetupFrontend() {
SetupFrontend(s.mux)
}