summaryrefslogtreecommitdiff
path: root/src/http/tpl.go
blob: 57d7cfd9f14813dc388163bfe63b23e5967b1ecc (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package http

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

	"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 (a *api) blogURL(base *url.URL, path string, abs bool) string {
	// filepath.Join strips trailing slash, but we want to keep it
	trailingSlash := strings.HasSuffix(path, "/")

	path = filepath.Join("/", base.Path, path)

	if trailingSlash && path != "/" {
		path += "/"
	}

	if !abs {
		return path
	}

	u := *base
	u.Path = path
	return u.String()
}

func (a *api) postURL(id string, abs bool) string {
	path := filepath.Join("posts", id)
	return a.blogURL(a.params.PublicURL, path, abs)
}

func (a *api) editPostURL(id string, abs bool) string {
	return a.postURL(id, abs) + "?method=edit"
}

func (a *api) managePostsURL(abs bool) string {
	return a.blogURL(a.params.PublicURL, "posts?method=manage", abs)
}

func (a *api) manageAssetsURL(abs bool) string {
	return a.blogURL(a.params.PublicURL, "assets?method=manage", abs)
}

func (a *api) assetURL(id string, abs bool) string {
	path := filepath.Join("assets", id)
	return a.blogURL(a.params.PublicURL, path, false)
}

func (a *api) draftPostURL(id string, abs bool) string {
	path := filepath.Join("drafts", id)
	return a.blogURL(a.params.PublicURL, path, abs)
}

func (a *api) editDraftPostURL(id string, abs bool) string {
	return a.draftPostURL(id, abs) + "?method=edit"
}

func (a *api) manageDraftPostsURL(abs bool) string {
	return a.blogURL(a.params.PublicURL, "drafts", abs) + "?method=manage"
}

func (a *api) draftsURL(abs bool) string {
	return a.blogURL(a.params.PublicURL, "drafts", abs)
}

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.params.PublicURL,
			a.params.PublicURL, // httpURL
			a.params.GeminiPublicURL,
			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
		}
	})
}