simplify routing
1 file changed, 24 insertions(+), 19 deletions(-)
changed files
M routes/handler.go → routes/handler.go
@@ -6,13 +6,18 @@ "log" "net/http" "path" "path/filepath" - "strings" "github.com/dimfeld/httptreemux/v5" "github.com/fsnotify/fsnotify" "alin.ovh/elgit/config" + "alin.ovh/elgit/templates" ) + +var ErrNotAllowed = templates.Error{ + Code: http.StatusMethodNotAllowed, + Message: "that's not possible.", +} // Checks for gitprotocol-http(5) specific smells; if found, passes // the request on to the git http service, else render the web frontend.@@ -44,19 +49,25 @@ return } - switch { - case rest == "info/refs" && - r.URL.RawQuery == "service=git-upload-pack" && - r.Method == "GET": - d.InfoRefs(w, r, params) - case rest == "git-upload-pack" && r.Method == "POST": - d.UploadPack(w, r, params) - case r.Method == "GET": - if fixed, found := strings.CutSuffix(r.URL.Path, "/"); found { - http.Redirect(w, r, fixed, http.StatusPermanentRedirect) - } else { + switch r.Method { + case http.MethodGet: + switch { + case rest == "info/refs" && + r.URL.RawQuery == "service=git-upload-pack": + d.InfoRefs(w, r, params) + default: d.RepoIndex(w, r, params) } + + case http.MethodPost: + switch rest { + case "git-upload-pack": + d.UploadPack(w, r, params) + default: + d.WriteError(w, ErrNotAllowed) + } + default: + d.WriteError(w, ErrNotAllowed) } }@@ -110,8 +121,6 @@ mux.NotFoundHandler = func(w http.ResponseWriter, _ *http.Request) { d.Write404(w) } - mux.RedirectTrailingSlash = false - mux.GET("/", d.Index) mux.NewGroup("/static/"). UsingContext().@@ -126,18 +135,14 @@ func (d deps) mountRepoPaths(group *httptreemux.Group) { group.GET("/tree/:ref/*rest", d.RepoTree) group.GET("/blob/:ref/*rest", d.FileContent) - group.GET("/tree/:ref", d.RepoTree) group.GET("/tree/:ref/", d.RepoTree) - group.GET("/blob/:ref/", d.FileContent) - group.GET("/log/:ref", d.Log) + group.GET("/log/:ref/", d.Log) group.GET("/archive/:file", d.Archive) group.GET("/commit/:ref", d.Diff) group.GET("/commit/:ref/*file", d.FileDiff) group.GET("/refs/", d.Refs) group.GET("", d.Multiplex) group.POST("", d.Multiplex) - group.GET("/", d.Multiplex) - group.POST("/", d.Multiplex) group.GET("/*rest", d.Multiplex) group.POST("/*rest", d.Multiplex) }