Skip to content

Commit

Permalink
cmd/anubis: prepare for more efficient serving
Browse files Browse the repository at this point in the history
Signed-off-by: Xe Iaso <[email protected]>
  • Loading branch information
Xe committed Jan 25, 2025
1 parent 5881119 commit 5f95aee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion cmd/anubis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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")
}
Binary file added cmd/anubis/static/js/main.mjs.br
Binary file not shown.
Binary file added cmd/anubis/static/js/main.mjs.gz
Binary file not shown.
Binary file added cmd/anubis/static/js/main.mjs.zst
Binary file not shown.

0 comments on commit 5f95aee

Please sign in to comment.