diff options
| author | demo <demo@antix1> | 2026-05-07 13:04:10 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-07 13:04:10 -0400 |
| commit | 44911fb0c2ae616cf07f46e35192d7f297bccf4b (patch) | |
| tree | 4d50fae80be277f2831ee3556a9f65c250721fe8 | |
| parent | cc26c36c468cc5bdf360810a61aa73b55e0e99f1 (diff) | |
feat: set up endpoints to serve arc titles only
| -rw-r--r-- | main.go | 49 |
1 files changed, 43 insertions, 6 deletions
@@ -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)) } |
