diff options
| author | demo <demo@antix1> | 2026-05-07 15:46:26 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-07 15:46:26 -0400 |
| commit | 5aecda4a8006c74e8c912d937fb9b282630bb883 (patch) | |
| tree | 3f6a60c5e7c6e174476c2816a361f4e9d8415695 | |
| parent | 9c1d8f97aae6454d86c6efdfde03e17cf7ea61ed (diff) | |
feat: implement basic page-infrastructure
| -rw-r--r-- | internal/types/types.go | 21 | ||||
| -rw-r--r-- | main.go | 9 | ||||
| -rw-r--r-- | views/index.gohtml | 16 | ||||
| -rw-r--r-- | views/page.gohtml | 11 |
4 files changed, 50 insertions, 7 deletions
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) + } } @@ -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" }} + <!DOCTYPE html> + <html lang="en"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <meta name="description" content="" /> + <meta name="author" content="" /> + <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width" /> + <title></title> + <link href="css/style.css" rel="stylesheet" /> + </head> + <body> + {{ template "page" . }} + </body> + </html> +{{ 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" }} + <h1>{{ .Title }}</h1> + + {{ range .Story }} + <p>{{ . }}</p> + {{ end }} + + {{ range .Options }} + <a href="/{{ .Arc }}">{{ .Text }}</a> + {{ end }} +{{ end }} |
