diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-01-25 17:21:17 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2025-01-25 17:21:17 -0500 |
| commit | 5f95aee14b747ddb2306e53283b43d023c7d68f0 (patch) | |
| tree | 3783d65061548b976da6a1a0a178ec0d931e97c6 | |
| parent | 58811194f0bced9b578937cdeb36d00243ac7df1 (diff) | |
| download | x-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.go | 33 | ||||
| -rw-r--r-- | cmd/anubis/static/js/main.mjs.br | bin | 0 -> 944 bytes | |||
| -rw-r--r-- | cmd/anubis/static/js/main.mjs.gz | bin | 0 -> 1146 bytes | |||
| -rw-r--r-- | cmd/anubis/static/js/main.mjs.zst | bin | 0 -> 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 Binary files differnew file mode 100644 index 0000000..33be3d4 --- /dev/null +++ b/cmd/anubis/static/js/main.mjs.br diff --git a/cmd/anubis/static/js/main.mjs.gz b/cmd/anubis/static/js/main.mjs.gz Binary files differnew file mode 100644 index 0000000..03df0af --- /dev/null +++ b/cmd/anubis/static/js/main.mjs.gz diff --git a/cmd/anubis/static/js/main.mjs.zst b/cmd/anubis/static/js/main.mjs.zst Binary files differnew file mode 100644 index 0000000..3d271b3 --- /dev/null +++ b/cmd/anubis/static/js/main.mjs.zst |
