summaryrefslogtreecommitdiff
path: root/internal/types/types.go
blob: 4f70ed7e17f0046e0bde4f0c0904c69669647147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package types

import (
	"html/template"
	"log"
	"net/http"
)

type option struct {
	Text string
	Arc  string
}

type arc struct {
	Title   string
	Story   []string
	Options []option
}

type story map[string]arc

type Config struct {
	*template.Template
	Story story
}

func (cfg Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	arcName := r.PathValue("arc")
	arc, ok := cfg.Story[arcName]
	if !ok {
		log.Printf("Arc with name %s doesn't exist", arcName)
		http.Error(w, "Page not found", http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", "text/html")
	if err := cfg.ExecuteTemplate(w, "index", arc); err != nil {
		log.Print(err)
		http.Error(w, "Something went wrong!", http.StatusInternalServerError)
		return
	}
}