aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2025-01-25 17:21:17 -0500
committerXe Iaso <me@xeiaso.net>2025-01-25 17:21:17 -0500
commit5f95aee14b747ddb2306e53283b43d023c7d68f0 (patch)
tree3783d65061548b976da6a1a0a178ec0d931e97c6
parent58811194f0bced9b578937cdeb36d00243ac7df1 (diff)
downloadx-5f95aee14b747ddb2306e53283b43d023c7d68f0.tar.xz
x-5f95aee14b747ddb2306e53283b43d023c7d68f0.zip
cmd/anubis: prepare for more efficient serving
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--cmd/anubis/main.go33
-rw-r--r--cmd/anubis/static/js/main.mjs.brbin0 -> 944 bytes
-rw-r--r--cmd/anubis/static/js/main.mjs.gzbin0 -> 1146 bytes
-rw-r--r--cmd/anubis/static/js/main.mjs.zstbin0 -> 1148 bytes
4 files changed, 32 insertions, 1 deletions
diff --git a/cmd/anubis/main.go b/cmd/anubis/main.go
index 6217b48..6e886e4 100644
--- a/cmd/anubis/main.go
+++ b/cmd/anubis/main.go
@@ -74,6 +74,9 @@ const (
//go:generate go run github.com/a-h/templ/cmd/templ@latest generate
//go:generate esbuild js/main.mjs --minify --bundle --outfile=static/js/main.mjs
+//go:generate gzip -f -k static/js/main.mjs
+//go:generate zstd -f -k --ultra -22 static/js/main.mjs
+//go:generate brotli -fZk static/js/main.mjs
func main() {
internal.HandleStartup()
@@ -84,9 +87,11 @@ func main() {
}
mux := http.NewServeMux()
+ xess.Mount(mux)
mux.Handle(staticPath, internal.UnchangingCache(http.StripPrefix(staticPath, http.FileServerFS(static))))
- xess.Mount(mux)
+
+ // mux.HandleFunc("GET /.within.website/x/cmd/anubis/static/js/main.mjs", serveMainJSWithBestEncoding)
mux.HandleFunc("POST /.within.website/x/cmd/anubis/api/make-challenge", s.makeChallenge)
mux.HandleFunc("GET /.within.website/x/cmd/anubis/api/pass-challenge", s.passChallenge)
@@ -460,6 +465,11 @@ func (s *Server) testError(w http.ResponseWriter, r *http.Request) {
templ.Handler(base("Oh noes!", errorPage(err)), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r)
}
+func ohNoes(w http.ResponseWriter, r *http.Request, err error) {
+ slog.Error("super fatal error", "err", err)
+ templ.Handler(base("Oh noes!", errorPage("An internal server error happened")), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r)
+}
+
func clearCookie(w http.ResponseWriter) {
http.SetCookie(w, &http.Cookie{
Name: cookieName,
@@ -473,3 +483,24 @@ func clearCookie(w http.ResponseWriter) {
func randomJitter() bool {
return mrand.Intn(100) > 10
}
+
+func serveMainJSWithBestEncoding(w http.ResponseWriter, r *http.Request) {
+ priorityList := []string{"zstd", "br", "gzip"}
+ enc2ext := map[string]string{
+ "zstd": "zst",
+ "br": "br",
+ "gzip": "gz",
+ }
+
+ for _, enc := range priorityList {
+ if strings.Contains(r.Header.Get("Accept-Encoding"), enc) {
+ w.Header().Set("Content-Type", "text/javascript")
+ w.Header().Set("Content-Encoding", enc)
+ http.ServeFileFS(w, r, static, "static/js/main.mjs."+enc2ext[enc])
+ return
+ }
+ }
+
+ w.Header().Set("Content-Type", "text/javascript")
+ http.ServeFileFS(w, r, static, "static/js/main.mjs")
+}
diff --git a/cmd/anubis/static/js/main.mjs.br b/cmd/anubis/static/js/main.mjs.br
new file mode 100644
index 0000000..33be3d4
--- /dev/null
+++ b/cmd/anubis/static/js/main.mjs.br
Binary files differ
diff --git a/cmd/anubis/static/js/main.mjs.gz b/cmd/anubis/static/js/main.mjs.gz
new file mode 100644
index 0000000..03df0af
--- /dev/null
+++ b/cmd/anubis/static/js/main.mjs.gz
Binary files differ
diff --git a/cmd/anubis/static/js/main.mjs.zst b/cmd/anubis/static/js/main.mjs.zst
new file mode 100644
index 0000000..3d271b3
--- /dev/null
+++ b/cmd/anubis/static/js/main.mjs.zst
Binary files differ