feat: rewrite to stateless web app with IndexedDB frontend
Replace CLI + SQLite architecture with a Go web server + vanilla JS frontend using IndexedDB for all client-side data storage. - Remove: cli, store, report, static packages - Add: compute engine (BuildDashboard), server package, web UI - Add: setup page with CRUD for profiles, rooms, devices, occupants, AC - Add: dashboard with SVG temperature timeline, risk analysis, care checklist - Add: i18n support (English/German) with server-side Go templates - Add: LLM provider selection UI with client-side API key storage - Add: per-room indoor temperature, edit buttons, language-aware AI summary
This commit is contained in:
205
internal/server/server.go
Normal file
205
internal/server/server.go
Normal file
@@ -0,0 +1,205 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/cnachtigall/heatwave-autopilot/internal/config"
|
||||
"github.com/cnachtigall/heatwave-autopilot/internal/llm"
|
||||
)
|
||||
|
||||
// Server holds the HTTP server state.
|
||||
type Server struct {
|
||||
mux *http.ServeMux
|
||||
trans *translations
|
||||
cfg config.Config
|
||||
llmProvider llm.Provider
|
||||
devMode bool
|
||||
}
|
||||
|
||||
// Options configures the server.
|
||||
type Options struct {
|
||||
Port int
|
||||
DevMode bool
|
||||
Config config.Config
|
||||
}
|
||||
|
||||
// New creates a new Server and sets up routes.
|
||||
func New(opts Options) (*Server, error) {
|
||||
s := &Server{
|
||||
mux: http.NewServeMux(),
|
||||
cfg: opts.Config,
|
||||
devMode: opts.DevMode,
|
||||
}
|
||||
|
||||
// Load translations
|
||||
var enJSON, deJSON []byte
|
||||
var err error
|
||||
if opts.DevMode {
|
||||
enJSON, err = os.ReadFile(filepath.Join("web", "i18n", "en.json"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read en.json: %w", err)
|
||||
}
|
||||
deJSON, err = os.ReadFile(filepath.Join("web", "i18n", "de.json"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read de.json: %w", err)
|
||||
}
|
||||
} else {
|
||||
if WebFS == nil {
|
||||
return nil, fmt.Errorf("WebFS not set — call server.WebFS = ... before server.New()")
|
||||
}
|
||||
enJSON, err = fs.ReadFile(WebFS, "i18n/en.json")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read embedded en.json: %w", err)
|
||||
}
|
||||
deJSON, err = fs.ReadFile(WebFS, "i18n/de.json")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read embedded de.json: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
s.trans, err = loadTranslations(enJSON, deJSON)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load translations: %w", err)
|
||||
}
|
||||
|
||||
// Set up LLM provider
|
||||
s.llmProvider = buildLLMProvider(s.cfg)
|
||||
|
||||
// Static assets
|
||||
if opts.DevMode {
|
||||
s.mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("web"))))
|
||||
} else {
|
||||
s.mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.FS(WebFS))))
|
||||
}
|
||||
|
||||
// Page routes
|
||||
s.mux.HandleFunc("/", s.handleDashboard)
|
||||
s.mux.HandleFunc("/setup", s.handleSetup)
|
||||
s.mux.HandleFunc("/guide", s.handleGuide)
|
||||
|
||||
// API routes
|
||||
s.mux.HandleFunc("/api/compute/dashboard", s.handleComputeDashboard)
|
||||
s.mux.HandleFunc("/api/weather/forecast", s.handleWeatherForecast)
|
||||
s.mux.HandleFunc("/api/weather/warnings", s.handleWeatherWarnings)
|
||||
s.mux.HandleFunc("/api/llm/summarize", s.handleLLMSummarize)
|
||||
s.mux.HandleFunc("/api/llm/config", s.handleLLMConfig)
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Handler returns the HTTP handler.
|
||||
func (s *Server) Handler() http.Handler {
|
||||
return s.mux
|
||||
}
|
||||
|
||||
// ListenAndServe starts the server.
|
||||
func (s *Server) ListenAndServe(addr string) error {
|
||||
return http.ListenAndServe(addr, s.mux)
|
||||
}
|
||||
|
||||
type pageData struct {
|
||||
Lang string
|
||||
Page string
|
||||
Title string
|
||||
}
|
||||
|
||||
func (s *Server) renderPage(w http.ResponseWriter, r *http.Request, page, templateFile string) {
|
||||
lang := detectLanguage(r)
|
||||
|
||||
// Set language cookie if query param was provided
|
||||
if qLang := r.URL.Query().Get("lang"); isSupported(qLang) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "heatguard_lang",
|
||||
Value: qLang,
|
||||
Path: "/",
|
||||
MaxAge: 365 * 24 * 3600,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
})
|
||||
}
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"t": func(key string) string {
|
||||
return s.trans.get(lang, key)
|
||||
},
|
||||
}
|
||||
|
||||
var tmpl *template.Template
|
||||
var err error
|
||||
|
||||
if s.devMode {
|
||||
tmpl, err = template.New("layout.html").Funcs(funcMap).ParseFiles(
|
||||
filepath.Join("web", "templates", "layout.html"),
|
||||
filepath.Join("web", "templates", templateFile),
|
||||
)
|
||||
} else {
|
||||
tmpl, err = template.New("layout.html").Funcs(funcMap).ParseFS(WebFS,
|
||||
"templates/layout.html",
|
||||
"templates/"+templateFile,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("template error: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
title := s.trans.get(lang, "nav."+page)
|
||||
data := pageData{
|
||||
Lang: lang,
|
||||
Page: page,
|
||||
Title: title,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
http.Error(w, fmt.Sprintf("render error: %v", err), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
s.renderPage(w, r, "dashboard", "dashboard.html")
|
||||
}
|
||||
|
||||
func (s *Server) handleSetup(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderPage(w, r, "setup", "setup.html")
|
||||
}
|
||||
|
||||
func (s *Server) handleGuide(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderPage(w, r, "guide", "guide.html")
|
||||
}
|
||||
|
||||
func buildLLMProvider(cfg config.Config) llm.Provider {
|
||||
switch cfg.LLM.Provider {
|
||||
case "anthropic":
|
||||
key := os.Getenv("ANTHROPIC_API_KEY")
|
||||
if key == "" {
|
||||
return llm.NewNoop()
|
||||
}
|
||||
return llm.NewAnthropic(key, cfg.LLM.Model, nil)
|
||||
case "openai":
|
||||
key := os.Getenv("OPENAI_API_KEY")
|
||||
if key == "" {
|
||||
return llm.NewNoop()
|
||||
}
|
||||
return llm.NewOpenAI(key, cfg.LLM.Model, nil)
|
||||
case "gemini":
|
||||
key := os.Getenv("GEMINI_API_KEY")
|
||||
if key == "" {
|
||||
return llm.NewNoop()
|
||||
}
|
||||
return llm.NewGemini(key, cfg.LLM.Model, nil)
|
||||
case "ollama":
|
||||
return llm.NewOllama(cfg.LLM.Model, cfg.LLM.Endpoint, nil)
|
||||
default:
|
||||
return llm.NewNoop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user