From d0e0c1eb99303e1000140d4b98c610077278dc42 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 1 Jun 2024 20:20:00 +0200 Subject: ref: Remove mux --- pkg/handler/git/handler.go | 15 +++++++-------- pkg/handler/router.go | 11 +++++------ pkg/handler/static/handler.go | 9 ++++++--- 3 files changed, 18 insertions(+), 17 deletions(-) (limited to 'pkg') diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index b77bcfc..e2f4042 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -8,7 +8,6 @@ import ( "git.gabrielgio.me/cerrado/templates" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" - "github.com/gorilla/mux" ) type ( @@ -43,7 +42,7 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { } func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { - name := mux.Vars(r)["name"] + name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { slog.Error("Error loading head", "error", err) @@ -59,7 +58,7 @@ func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { - name := mux.Vars(r)["name"] + name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { slog.Error("Error loading head", "error", err) @@ -74,7 +73,7 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { - name := mux.Vars(r)["name"] + name := r.PathValue("name") tags, err := g.gitService.ListTags(name) if err != nil { @@ -106,8 +105,8 @@ func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { - name := mux.Vars(r)["name"] - ref := mux.Vars(r)["ref"] + name := r.PathValue("name") + ref := r.PathValue("ref") gitList := &templates.GitItemPage{ Name: name, Ref: ref, @@ -117,8 +116,8 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { } func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { - name := mux.Vars(r)["name"] - ref := mux.Vars(r)["ref"] + name := r.PathValue("name") + ref := r.PathValue("ref") commits, err := g.gitService.ListCommits(name, ref) if err != nil { diff --git a/pkg/handler/router.go b/pkg/handler/router.go index 79f70f1..bdf883e 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -9,7 +9,6 @@ import ( "git.gabrielgio.me/cerrado/pkg/handler/git" "git.gabrielgio.me/cerrado/pkg/handler/static" "git.gabrielgio.me/cerrado/pkg/service" - "github.com/gorilla/mux" ) // Mount handler gets the requires service and repository to build the handlers @@ -25,17 +24,17 @@ func MountHandler( configHander = config.ConfigFile(configRepo) ) - staticHandler, err := static.NewStaticHander("/static/") + staticHandler, err := static.ServeStaticHandler() if err != nil { return nil, err } - mux := mux.NewRouter() + mux := http.NewServeMux() - mux.PathPrefix("/static").Handler(staticHandler) - mux.HandleFunc("/{name}/about", gitHandler.About) + 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}/refs/{$}", gitHandler.Refs) mux.HandleFunc("/{name}/tree/{ref}", gitHandler.Tree) mux.HandleFunc("/{name}/log/{ref}", gitHandler.Log) mux.HandleFunc("/config", configHander) diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go index 6a826cc..a8b4583 100644 --- a/pkg/handler/static/handler.go +++ b/pkg/handler/static/handler.go @@ -7,12 +7,15 @@ import ( "git.gabrielgio.me/cerrado/static" ) -func NewStaticHander(prefix string) (http.Handler, error) { +func ServeStaticHandler() (func(w http.ResponseWriter, r *http.Request), error) { staticFs, err := fs.Sub(static.Static, ".") if err != nil { return nil, err } - handler := http.StripPrefix(prefix, http.FileServer(http.FS(staticFs))) - return handler, nil + return func(w http.ResponseWriter, r *http.Request) { + f := r.PathValue("file") + + http.ServeFileFS(w, r, staticFs, f) + }, nil } -- cgit v1.2.3