diff --git a/router/handlers/simulate.go b/router/handlers/game.go similarity index 100% rename from router/handlers/simulate.go rename to router/handlers/game.go diff --git a/router/handlers/mode.go b/router/handlers/mode.go new file mode 100644 index 0000000..dbe547c --- /dev/null +++ b/router/handlers/mode.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "html/template" + "net/http" +) + +type ModeHandler struct { + Tmpl *template.Template +} + +func (m *ModeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + data := struct { + Title string + }{ + Title: "Title", + } + + if err := m.Tmpl.ExecuteTemplate(w, "game-mode.html", data); err != nil { + http.Error(w, "Template Error", http.StatusInternalServerError) + } +} diff --git a/router/handlers/simulation.go b/router/handlers/simulation.go new file mode 100644 index 0000000..532124a --- /dev/null +++ b/router/handlers/simulation.go @@ -0,0 +1,49 @@ +package handlers + +import ( + "encoding/json" + "net/http" + "systemdesigngame/internal/design" +) + +type SimulationHandler struct{} + +type SimulationResponse struct { + Success bool `json:"success"` + Metrics map[string]interface{} `json:"metrics,omitempty"` + Timeline []interface{} `json:"timeline,omitempty"` + Error string `json:"error,omitempty"` +} + +func (h *SimulationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + var design design.Design + if err := json.NewDecoder(r.Body).Decode(&design); err != nil { + http.Error(w, "Invalid design JSON: "+err.Error(), http.StatusBadRequest) + return + } + + // TODO: Run simulation engine here + + // For now, return a mock successful response but eventually, we want to go to the results page(s) + response := SimulationResponse{ + Success: true, + Metrics: map[string]interface{}{ + "throughput": 250, + "latency_p95": 85, + "cost_monthly": 120, + "availability": 99.5, + }, + Timeline: []interface{}{}, // Will contain TickSnapshots later + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + return + } +} diff --git a/router/router.go b/router/router.go index 38f786a..0fc8d1b 100644 --- a/router/router.go +++ b/router/router.go @@ -12,7 +12,10 @@ func SetupRoutes(tmpl *template.Template) *http.ServeMux { mux := http.NewServeMux() mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) mux.Handle("/", &handlers.HomeHandler{Tmpl: tmpl}) + mux.Handle("/mode", auth.RequireAuth(&handlers.PlayHandler{Tmpl: tmpl})) + mux.Handle("/play/", auth.RequireAuth(&handlers.PlayHandler{Tmpl: tmpl})) + mux.Handle("/simulate", auth.RequireAuth(&handlers.SimulationHandler{})) mux.HandleFunc("/login", auth.LoginHandler) mux.HandleFunc("/callback", auth.CallbackHandler)