From c7a8aa113a914e70e027fea93265c7232b865b5e Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Fri, 7 Jun 2024 19:33:07 +0200 Subject: feat: Add compression --- pkg/handler/git/handler.go | 7 +++++++ pkg/handler/router.go | 25 +++++++++++++++---------- pkg/handler/static/handler.go | 11 +++++++++-- 3 files changed, 31 insertions(+), 12 deletions(-) (limited to 'pkg/handler') diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index f3e74c7..28cc99e 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -6,6 +6,7 @@ import ( "net/http" "path/filepath" + "git.gabrielgio.me/cerrado/pkg/ext" "git.gabrielgio.me/cerrado/pkg/service" "git.gabrielgio.me/cerrado/templates" "github.com/alecthomas/chroma/v2" @@ -50,6 +51,7 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { } func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { + ext.SetHTML(w) name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { @@ -66,6 +68,7 @@ func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { + ext.SetHTML(w) name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { @@ -81,6 +84,7 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { + ext.SetHTML(w) name := r.PathValue("name") tags, err := g.gitService.ListTags(name) @@ -113,6 +117,7 @@ func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { + ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") rest := r.PathValue("rest") @@ -137,6 +142,7 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { + ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") rest := r.PathValue("rest") @@ -178,6 +184,7 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { + ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") diff --git a/pkg/handler/router.go b/pkg/handler/router.go index ed782f7..de5117c 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -4,6 +4,7 @@ import ( "net/http" serverconfig "git.gabrielgio.me/cerrado/pkg/config" + "git.gabrielgio.me/cerrado/pkg/ext" "git.gabrielgio.me/cerrado/pkg/handler/about" "git.gabrielgio.me/cerrado/pkg/handler/config" "git.gabrielgio.me/cerrado/pkg/handler/git" @@ -31,15 +32,19 @@ func MountHandler( mux := http.NewServeMux() - mux.HandleFunc("/static/{file}", staticHandler) - mux.HandleFunc("/{name}/about/{$}", gitHandler.About) - mux.HandleFunc("/{name}", gitHandler.Summary) - mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs) - mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree) - mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob) - mux.HandleFunc("/{name}/log/{ref}", gitHandler.Log) - mux.HandleFunc("/config", configHander) - mux.HandleFunc("/about", aboutHandler.About) - mux.HandleFunc("/", gitHandler.List) + mux.HandleFunc("/static/{file}", m(staticHandler)) + mux.HandleFunc("/{name}/about/{$}", m(gitHandler.About)) + mux.HandleFunc("/{name}", m(gitHandler.Summary)) + mux.HandleFunc("/{name}/refs/{$}", m(gitHandler.Refs)) + mux.HandleFunc("/{name}/tree/{ref}/{rest...}", m(gitHandler.Tree)) + mux.HandleFunc("/{name}/blob/{ref}/{rest...}", m(gitHandler.Blob)) + mux.HandleFunc("/{name}/log/{ref}", m(gitHandler.Log)) + mux.HandleFunc("/config", m(configHander)) + mux.HandleFunc("/about", m(aboutHandler.About)) + mux.HandleFunc("/", m(gitHandler.List)) return mux, nil } + +func m(next func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { + return ext.Compress(next) +} diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go index a8b4583..5155068 100644 --- a/pkg/handler/static/handler.go +++ b/pkg/handler/static/handler.go @@ -2,8 +2,11 @@ package static import ( "io/fs" + "mime" "net/http" + "path/filepath" + "git.gabrielgio.me/cerrado/pkg/ext" "git.gabrielgio.me/cerrado/static" ) @@ -14,8 +17,12 @@ func ServeStaticHandler() (func(w http.ResponseWriter, r *http.Request), error) } return func(w http.ResponseWriter, r *http.Request) { - f := r.PathValue("file") - + var ( + f = r.PathValue("file") + e = filepath.Ext(f) + m = mime.TypeByExtension(e) + ) + ext.SetMIME(w, m) http.ServeFileFS(w, r, staticFs, f) }, nil } -- cgit v1.2.3