all repos — homestead @ 8e31c2454617b413cc672c7561bb512555496927

Code for my website

domain/web/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
package website

import (
	"fmt"
	"net/http"
	"regexp"
	"slices"
	"strings"

	"alin.ovh/homestead/domain/analytics"
	calendar "alin.ovh/homestead/domain/calendar/templates"
	"alin.ovh/homestead/domain/web/server"
	"alin.ovh/homestead/domain/web/templates"
	ihttp "alin.ovh/homestead/shared/http"

	"github.com/kevinpollet/nego"
)

func (website *Website) ErrorHandler(err ihttp.Error, w http.ResponseWriter, r *http.Request) {
	if strings.Contains(r.Header.Get("Accept"), "text/html") {
		w.WriteHeader(err.StatusCode())
		err := templates.Error(*website.siteSettings, err).Render(w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	} else {
		err.WriteHTTP(w)
	}
}

func (website *Website) ServeHTTP(w http.ResponseWriter, r *http.Request) ihttp.Error {
	urlPath := r.URL.Path
	if r.URL.Query().Has("go-get") && r.URL.Query().Get("go-get") == "1" {
		urlPath = "/go" + r.URL.Path
	}
	urlPath, shouldRedirect := website.reader.CanonicalisePath(urlPath)
	if shouldRedirect {
		analytics.WithCountKey(r, "302")
		http.Redirect(w, r, urlPath, http.StatusFound)

		return nil
	}
	file, err := website.reader.GetFile(urlPath)
	if err != nil {
		website.log.Warn("Error reading file", "error", err)

		return ihttp.InternalServerError("Error reading file", err)
	}
	if file == nil {
		analytics.WithCountKey(r, "404")

		return ihttp.NotFound("File not found")
	}
	analytics.WithCountKey(r, file.Title)
	w.Header().Add("ETag", file.Etag)
	w.Header().Add("Vary", "Accept-Encoding")
	if file.StyleHash != "" {
		CSPHeader.StyleSrc = append(CSPHeader.StyleSrc, fmt.Sprintf("'%s'", file.StyleHash))
	}
	w.Header().Add("Content-Security-Policy", CSPHeader.String())
	for k, v := range ExtraHeaders {
		w.Header().Add(k, v)
	}
	enc := nego.NegotiateContentEncoding(r, file.AvailableEncodings()...)
	if enc != "" {
		w.Header().Add("Content-Encoding", enc)
	}
	w.Header().Add("Content-Type", file.ContentType)
	if file.Headers != nil {
		for k, v := range file.Headers {
			w.Header().Add(k, v)
		}
	}
	http.ServeContent(w, r, file.Path, file.LastModified, file.Encodings[enc])

	return nil
}

func (website *Website) Calendar(w http.ResponseWriter, r *http.Request) ihttp.Error {
	analytics.WithCountKey(r, "Calendar")
	err := calendar.CalendarPage(*website.siteSettings, templates.PageSettings{
		Title:      "Calendar",
		TitleAttrs: templates.Attrs{},
		BodyAttrs:  templates.Attrs{},
	}, *website.calendar).Render(w)
	if err != nil {
		return ihttp.InternalServerError("could not render calendar page", err)
	}

	return nil
}

func (website *Website) MakeRedirectorApp() *server.App {
	mux := ihttp.NewServeMux()
	website.identity.RegisterHandlers(mux)

	re := regexp.MustCompile(
		"^(.*)\\." + strings.ReplaceAll(website.config.WildcardDomain, ".", `\.`) + "$",
	)
	replace := "${1}." + website.config.Domains[0]
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) ihttp.Error {
		switch {
		case r.URL.Query().Has("go-get") && r.URL.Query().Get("go-get") == "1":
			return website.ServeHTTP(w, r)
		case slices.Contains(website.config.Domains, r.Host):
			path, _ := website.reader.CanonicalisePath(r.URL.Path)
			ihttp.PermanentRedirect(w, r, website.config.BaseURL.JoinPath(path))
		case re.MatchString(r.Host):
			url := website.config.BaseURL.JoinPath()
			url.Host = re.ReplaceAllString(r.Host, replace)
			ihttp.TemporaryRedirect(w, r, url)
		case true:
			http.NotFound(w, r)
		}

		return nil
	})

	return &server.App{
		WildcardDomain: website.config.WildcardDomain,
		Domains:        website.config.Domains,
		Handler:        mux,
	}
}