From 44911fb0c2ae616cf07f46e35192d7f297bccf4b Mon Sep 17 00:00:00 2001 From: demo Date: Thu, 7 May 2026 13:04:10 -0400 Subject: feat: set up endpoints to serve arc titles only --- main.go | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index bda2d87..14dec49 100644 --- a/main.go +++ b/main.go @@ -1,26 +1,63 @@ package main import ( + "encoding/json" "flag" "io" "log" "net/http" + "os" ) -type handler struct{} +type option struct { + Text string + Arc string +} + +type arc struct { + Title string + Story []string + Options []option +} + +type story map[string]arc + +func (s story) ServeHTTP(w http.ResponseWriter, r *http.Request) { + arc := r.PathValue("arc") + title := s[arc].Title -func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") - io.WriteString(w, "Hello world!") + io.WriteString(w, title) } func main() { - port := flag.String("port", "8080", "Default localhost port") + // Set up log flags. + log.SetFlags(log.LstdFlags | log.Lshortfile) + + // Get any command-line flags (port, etc.) + port := flag.String("port", "8080", "Server port") + filename := flag.String("json", "adventure.json", "Adventure file (JSON)") flag.Parse() - mux := http.NewServeMux() + // Get the JSON. + f, err := os.Open(*filename) + if err != nil { + log.Fatal(err) + } + defer f.Close() + jsonBytes, err := io.ReadAll(f) + if err != nil { + log.Fatal(err) + } - mux.Handle("/{$}", &handler{}) + var s story + if err := json.Unmarshal(jsonBytes, &s); err != nil { + log.Fatal(err) + } + + // Set up the handlers and server. + mux := http.NewServeMux() + mux.Handle("/{arc}", &s) log.Fatal(runServer(mux, *port)) } -- cgit v1.2.3