From 5aecda4a8006c74e8c912d937fb9b282630bb883 Mon Sep 17 00:00:00 2001 From: demo Date: Thu, 7 May 2026 15:46:26 -0400 Subject: feat: implement basic page-infrastructure --- internal/types/types.go | 21 ++++++++++++++------- main.go | 9 +++++++++ views/index.gohtml | 16 ++++++++++++++++ views/page.gohtml | 11 +++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 views/index.gohtml create mode 100644 views/page.gohtml diff --git a/internal/types/types.go b/internal/types/types.go index 3772f1f..b431eaf 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -1,8 +1,8 @@ package types import ( + "fmt" "html/template" - "io" "net/http" ) @@ -20,14 +20,21 @@ type arc struct { type story map[string]arc type Config struct { - Story story - Template *template.Template + *template.Template + Story story } func (cfg Config) ServeHTTP(w http.ResponseWriter, r *http.Request) { - arc := r.PathValue("arc") - title := cfg.Story[arc].Title + arcName := r.PathValue("arc") + arc, ok := cfg.Story[arcName] + if !ok { + msg := fmt.Sprintf("Arc with name %s doesn't exist", arcName) + http.Error(w, msg, http.StatusNotFound) + return + } - w.Header().Set("Content-Type", "text/plain") - io.WriteString(w, title) + w.Header().Set("Content-Type", "text/html") + if err := cfg.ExecuteTemplate(w, "index", arc); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } diff --git a/main.go b/main.go index c185441..f070c9b 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "flag" + "html/template" "io" "log" "net/http" @@ -36,6 +37,14 @@ func main() { log.Fatal(err) } + // Set up all the templates needed for the story arcs. + indexTmpl, err := template.ParseGlob("views/*.gohtml") + if err != nil { + log.Fatal(err) + } + + cfg.Template = indexTmpl + // Set up the handlers and server. mux := http.NewServeMux() mux.Handle("/{arc}", &cfg) diff --git a/views/index.gohtml b/views/index.gohtml new file mode 100644 index 0000000..752f928 --- /dev/null +++ b/views/index.gohtml @@ -0,0 +1,16 @@ +{{ define "index" }} + + + + + + + + + + + + {{ template "page" . }} + + +{{ end }} diff --git a/views/page.gohtml b/views/page.gohtml new file mode 100644 index 0000000..5eb49ac --- /dev/null +++ b/views/page.gohtml @@ -0,0 +1,11 @@ +{{ define "page" }} +

{{ .Title }}

+ + {{ range .Story }} +

{{ . }}

+ {{ end }} + + {{ range .Options }} + {{ .Text }} + {{ end }} +{{ end }} -- cgit v1.2.3