optiminimize

This commit is contained in:
2026-06-28 12:15:44 +03:00
parent ddff4a3111
commit 09f080c266
388 changed files with 63617 additions and 132 deletions

49
main.go
View File

@@ -45,6 +45,53 @@ func cacheHandler(next http.Handler) http.Handler {
})
}
type mimeWriter struct {
http.ResponseWriter
override string
wrote bool
}
func (m *mimeWriter) WriteHeader(code int) {
if !m.wrote {
m.wrote = true
m.Header().Set("Content-Type", m.override)
}
m.ResponseWriter.WriteHeader(code)
}
func (m *mimeWriter) Write(b []byte) (int, error) {
if !m.wrote {
m.WriteHeader(200)
}
return m.ResponseWriter.Write(b)
}
func mimeHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ext := strings.ToLower(r.URL.Path[strings.LastIndex(r.URL.Path, ".")+1:])
var ct string
switch ext {
case "woff2":
ct = "font/woff2"
case "woff":
ct = "font/woff"
case "ttf":
ct = "font/ttf"
case "js":
ct = "text/javascript"
case "css":
ct = "text/css"
case "webp":
ct = "image/webp"
}
if ct != "" {
next.ServeHTTP(&mimeWriter{ResponseWriter: w, override: ct}, r)
return
}
next.ServeHTTP(w, r)
})
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/ABSTRACTS.png", func(w http.ResponseWriter, r *http.Request) {
@@ -64,5 +111,5 @@ func main() {
mux.HandleFunc("/", blog.ServeLanding)
log.Println("listening on :8000")
http.ListenAndServe(":8000", cacheHandler(gzipHandler(mux)))
http.ListenAndServe(":8000", mimeHandler(cacheHandler(gzipHandler(mux))))
}