all repos — homestead @ bd77f727dca007014f42269b9dd48b92df28f657

Code for my website

internal/website/mux.go (view raw)

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package website

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"regexp"
	"slices"
	"strings"

	"gitlab.com/tozd/go/errors"
	"go.alanpearce.eu/website/internal/builder"
	"go.alanpearce.eu/website/internal/config"
	ihttp "go.alanpearce.eu/website/internal/http"
	"go.alanpearce.eu/website/internal/server"
	"go.alanpearce.eu/website/internal/storage"
	"go.alanpearce.eu/website/internal/storage/files"
	"go.alanpearce.eu/website/internal/vcs"
	"go.alanpearce.eu/website/internal/watcher"
	"go.alanpearce.eu/website/templates"
	"go.alanpearce.eu/x/log"

	"github.com/benpate/digit"
	"github.com/kevinpollet/nego"
	"github.com/osdevisnot/sorvor/pkg/livereload"
)

type Options struct {
	Source      string     `conf:"default:../website"`
	Destination string     `conf:"default:public"`
	Redirect    bool       `conf:"default:true"`
	Development bool       `conf:"default:false,flag:dev"`
	BaseURL     config.URL `conf:"default:localhost"`
}

type Website struct {
	config *config.Config
	log    *log.Logger
	reader storage.Reader
	*server.App
}

type webHandler func(http.ResponseWriter, *http.Request) *ihttp.Error

type WrappedWebHandler struct {
	config  *config.Config
	handler webHandler
	log     *log.Logger
}

func wrapHandler(cfg *config.Config, webHandler webHandler, log *log.Logger) WrappedWebHandler {
	return WrappedWebHandler{
		config:  cfg,
		handler: webHandler,
		log:     log,
	}
}

func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer func() {
		if fail := recover(); fail != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fn.log.Error("runtime panic!", "error", fail)
		}
	}()
	if err := fn.handler(w, r); err != nil {
		if strings.Contains(r.Header.Get("Accept"), "text/html") {
			w.WriteHeader(err.Code)
			err := templates.Error(fn.config, r.URL.Path, err).Render(r.Context(), w)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
		} else {
			http.Error(w, err.Message, err.Code)
		}
	}
}

func New(
	opts *Options,
	log *log.Logger,
) (*Website, error) {
	mux := http.NewServeMux()
	website := &Website{
		log: log,
		App: &server.App{
			Handler: mux,
		},
	}
	builderOptions := &builder.Options{
		Source:      opts.Source,
		Development: opts.Development,
		Destination: opts.Destination,
	}

	templates.Setup()

	log.Debug("getting config from source", "source", opts.Source)
	cfg, err := config.GetConfig(opts.Source, log)
	if err != nil {
		return nil, errors.WithMessage(err, "could not load configuration")
	}

	if opts.Development {
		tmpdir, err := os.MkdirTemp("", "website")
		if err != nil {
			log.Fatal("could not create temporary directory", "error", err)
		}
		log.Info("using temporary directory", "dir", tmpdir)
		website.App.Shutdown = func() {
			os.RemoveAll(tmpdir)
		}
		builderOptions.Destination = tmpdir

		cfg.CSP.ScriptSrc = slices.Insert(cfg.CSP.ScriptSrc, 0, "'unsafe-inline'")
		cfg.CSP.ConnectSrc = slices.Insert(cfg.CSP.ConnectSrc, 0, "'self'")

		cfg.BaseURL = opts.BaseURL
	}

	website.Domain = cfg.BaseURL.Hostname()

	repo, err := vcs.CloneOrOpen(&vcs.Options{
		LocalPath: opts.Source,
		RemoteURL: cfg.VCS.RemoteURL,
		Branch:    cfg.VCS.Branch,
	}, log.Named("vcs"))
	if err != nil {
		return nil, errors.WithMessage(err, "could not update repository")
	}

	_ = repo
	// config may have changed, reload

	err = rebuild(builderOptions, cfg, log)
	if err != nil {
		return nil, errors.WithMessage(err, "could not build site")
	}

	if opts.Development {
		liveReload := livereload.New()
		mux.Handle("/_/reload", liveReload)
		liveReload.Start()
		fw, err := watcher.New(log.Named("watcher"))
		if err != nil {
			return nil, errors.WithMessage(err, "could not create file watcher")
		}
		err = fw.AddRecursive(opts.Source)
		if err != nil {
			return nil, errors.WithMessage(
				err,
				"could not add directory to file watcher",
			)
		}

		go fw.Start(func(filename string) {
			log.Info("rebuilding site", "changed_file", filename)
			err := rebuild(builderOptions, cfg, log)
			if err != nil {
				log.Error("error rebuilding site", "error", err)
			}
		})
	}

	website.reader, err = files.NewReader(builderOptions.Destination, log.Named("reader"))
	if err != nil {
		return nil, errors.WithMessage(err, "error creating sqlite reader")
	}

	mux.Handle("/", wrapHandler(cfg, func(w http.ResponseWriter, r *http.Request) *ihttp.Error {
		urlPath, shouldRedirect := website.reader.CanonicalisePath(r.URL.Path)
		if shouldRedirect {
			http.Redirect(w, r, urlPath, 302)

			return nil
		}
		file, err := website.reader.GetFile(urlPath)
		if err != nil {
			return &ihttp.Error{
				Message: "Error reading file",
				Code:    http.StatusInternalServerError,
			}
		}
		if file == nil {
			return &ihttp.Error{
				Message: "File not found",
				Code:    http.StatusNotFound,
			}
		}
		w.Header().Add("ETag", file.Etag)
		w.Header().Add("Vary", "Accept-Encoding")
		w.Header().Add("Content-Security-Policy", cfg.CSP.String())
		for k, v := range cfg.Extra.Headers {
			w.Header().Add(k, v)
		}
		enc := nego.NegotiateContentEncoding(r, file.AvailableEncodings()...)
		switch enc {
		case "br", "gzip":
			w.Header().Add("Content-Encoding", enc)
		}
		w.Header().Add("Content-Type", file.ContentType)
		http.ServeContent(w, r, file.Path, file.LastModified, file.Encodings[enc])

		return nil
	}, log))

	var acctResource = "acct:" + cfg.Email
	me := digit.NewResource(acctResource).
		Link("http://openid.net/specs/connect/1.0/issuer", "", cfg.OIDCHost.String())

	mux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Query().Get("resource") == acctResource {
			obj, err := json.Marshal(me)
			if err != nil {
				http.Error(
					w,
					http.StatusText(http.StatusInternalServerError),
					http.StatusInternalServerError,
				)

				return
			}

			w.Header().Add("Content-Type", "application/jrd+json")
			w.Header().Add("Access-Control-Allow-Origin", "*")
			_, err = w.Write(obj)
			if err != nil {
				log.Warn("error writing webfinger request", "error", err)
			}
		}
	})
	const oidcPath = "/.well-known/openid-configuration"
	mux.HandleFunc(
		oidcPath,
		func(w http.ResponseWriter, r *http.Request) {
			u := cfg.OIDCHost.JoinPath(oidcPath)
			http.Redirect(w, r, u.String(), 302)
		})

	website.config = cfg

	return website, nil
}

func (website *Website) MakeRedirectorApp() *server.App {
	mux := http.NewServeMux()

	re := regexp.MustCompile(
		"^(.*)\\." + strings.ReplaceAll(website.config.WildcardDomain, ".", `\.`) + "$",
	)
	replace := "${1}." + website.config.Domains[0]
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		switch {
		case slices.Contains(website.config.Domains, r.Host):
			path, _ := website.reader.CanonicalisePath(r.URL.Path)
			http.Redirect(
				w,
				r,
				website.config.BaseURL.JoinPath(path).String(),
				http.StatusMovedPermanently,
			)
		case re.MatchString(r.Host):
			url := website.config.BaseURL.JoinPath()
			url.Host = re.ReplaceAllString(r.Host, replace)
			http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect)
		case true:
			http.NotFound(w, r)
		}
	})

	return &server.App{
		Handler: mux,
	}
}

func updateCSPHashes(config *config.Config, r *builder.Result) {
	for i, h := range r.Hashes {
		config.CSP.StyleSrc[i] = fmt.Sprintf("'%s'", h)
	}
}

func rebuild(builderConfig *builder.Options, config *config.Config, log *log.Logger) error {
	r, err := builder.BuildSite(builderConfig, config, log.Named("builder"))
	if err != nil {
		return errors.WithMessage(err, "could not build site")
	}
	updateCSPHashes(config, r)

	return nil
}