diff --git a/assets/helpers.js.gz b/assets/helpers.js.gz new file mode 100644 index 0000000..3056ad0 Binary files /dev/null and b/assets/helpers.js.gz differ diff --git a/assets/htmx.min.js.gz b/assets/htmx.min.js.gz new file mode 100644 index 0000000..5b6af32 Binary files /dev/null and b/assets/htmx.min.js.gz differ diff --git a/assets/htmx.sse.js.gz b/assets/htmx.sse.js.gz new file mode 100644 index 0000000..395c9b6 Binary files /dev/null and b/assets/htmx.sse.js.gz differ diff --git a/assets/output.css.gz b/assets/output.css.gz new file mode 100644 index 0000000..6687c04 Binary files /dev/null and b/assets/output.css.gz differ diff --git a/assets/style.css.gz b/assets/style.css.gz new file mode 100644 index 0000000..8db2f85 Binary files /dev/null and b/assets/style.css.gz differ diff --git a/assets/tailwind.css.gz b/assets/tailwind.css.gz new file mode 100644 index 0000000..ef197db Binary files /dev/null and b/assets/tailwind.css.gz differ diff --git a/main.go b/main.go index 24670a8..2369d9e 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "net/http" "os" "os/signal" + "path/filepath" + "strings" "syscall" "time" ) @@ -20,6 +22,38 @@ func init() { cfg = config.LoadConfigOrDefault("") } +// GzipFileServer serves pre-compressed .gz files if available +func GzipFileServer(root http.FileSystem) http.Handler { + fs := http.FileServer(root) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Check if client accepts gzip + if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + // Check for .gz version of the file + gzPath := r.URL.Path + ".gz" + if file, err := root.Open(gzPath); err == nil { + file.Close() + // Set headers for gzip + w.Header().Set("Content-Encoding", "gzip") + w.Header().Set("Content-Type", getContentType(r.URL.Path)) + r.URL.Path = gzPath + } + } + fs.ServeHTTP(w, r) + }) +} + +// Helper to set correct Content-Type +func getContentType(path string) string { + switch filepath.Ext(path) { + case ".css": + return "text/css" + case ".js": + return "application/javascript" + default: + return "" // http.FileServer will detect it + } +} + func ListenToRequests(port string) *http.Server { mux := http.NewServeMux() server := &http.Server{ @@ -28,8 +62,9 @@ func ListenToRequests(port string) *http.Server { // ReadTimeout: time.Second * 5, // does this timeout conflict with sse connection? WriteTimeout: 0, // sse streaming } - fs := http.FileServer(http.Dir("assets/")) - mux.Handle("GET /assets/", http.StripPrefix("/assets/", fs)) + // fs := http.FileServer(http.Dir("assets/")) + fs := http.Dir("assets/") + mux.Handle("GET /assets/", http.StripPrefix("/assets/", GzipFileServer(fs))) // mux.HandleFunc("GET /ping", handlers.HandlePing) mux.HandleFunc("GET /", handlers.HandleHome)