summaryrefslogtreecommitdiff
path: root/src/http/tpl.go
blob: 47cea804c7f3a2bc57a7a9873c9d65a62440ce1b (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package http

import (
	"bytes"
	"embed"
	"errors"
	"fmt"
	"html/template"
	"io"
	"io/fs"
	"net/http"
	"path/filepath"

	"dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil"
	"dev.mediocregopher.com/mediocre-blog.git/src/post"
	"dev.mediocregopher.com/mediocre-blog.git/src/render"
)

//go:embed tpl
var tplFS embed.FS

func mustReadTplFile(fileName string) string {
	path := filepath.Join("tpl", fileName)

	b, err := fs.ReadFile(tplFS, path)
	if err != nil {
		panic(fmt.Errorf("reading file %q from tplFS: %w", path, err))
	}

	return string(b)
}

func mustParseTpl(tpl *template.Template, name string) *template.Template {
	return template.Must(tpl.New(name).Parse(mustReadTplFile(name)))
}

func (a *api) mustParseBasedTpl(name string) *template.Template {
	tpl := template.New("")
	tpl = mustParseTpl(tpl, "gemini-cta.html")
	tpl = mustParseTpl(tpl, "base.html")
	tpl = mustParseTpl(tpl, name)
	return tpl
}

type tplData struct {
	*render.Methods
	Payload interface{}
	Title   string
}

// WithTitlePrefix returns a copy of tplData but with the given string prefixed
// to the page title. This is intended for use within templates, when nesting
// the base template.
func (d tplData) WithTitlePrefix(prefix string) tplData {
	d.Title = prefix + " - " + d.Title
	return d
}

func (a *api) newTPLData(r *http.Request, payload interface{}) tplData {
	return tplData{
		Methods: render.NewMethods(
			r.Context(),
			r.URL,
			a.urlBuilder,
			a.params.GeminiGatewayURL,
			a.params.PostStore,
			a.params.PostAssetStore,
			a.params.PostDraftStore,
			a.postPreprocessFuncs(),
		),
		Payload: payload,
		Title:   "mediocregopher's lil web corner",
	}
}

// executeTemplate expects to be the final action in an http.Handler
func (a *api) executeTemplate(
	rw http.ResponseWriter,
	r *http.Request,
	tpl *template.Template,
	payload interface{},
) {

	tplData := a.newTPLData(r, payload)

	buf := new(bytes.Buffer)

	err := tpl.Execute(buf, tplData)
	if errors.Is(err, post.ErrPostNotFound) {
		http.Error(rw, "Post not found", 404)
		return
	} else if err != nil {
		apiutil.InternalServerError(
			rw, r, fmt.Errorf("rendering template: %w", err),
		)
		return
	}

	io.Copy(rw, buf)
}

func (a *api) executeRedirectTpl(
	rw http.ResponseWriter, r *http.Request, url string,
) {
	a.executeTemplate(rw, r, a.redirectTpl, struct {
		URL string
	}{
		URL: url,
	})
}

func (a *api) renderDumbTplHandler(tplName string) http.Handler {

	tpl := a.mustParseBasedTpl(tplName)

	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

		tplData := a.newTPLData(r, nil)

		if err := tpl.Execute(rw, tplData); err != nil {
			apiutil.InternalServerError(
				rw, r, fmt.Errorf("rendering %q: %w", tplName, err),
			)
			return
		}
	})
}