diff --git a/internal/http/http.go b/internal/http/http.go index 3fc9b6d59cd32ae61d2959f15d58f63f01834d48..7e1bf4abc35c5104cab2cc6b237e479532d45f12 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -62,9 +62,6 @@ mux.Use(middleware.Recoverer) rh := repoHandler{s: settings} mux.Route("/{repo}.git", func(r chi.Router) { - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/"+chi.URLParam(r, "repo"), http.StatusFound) - }) r.Get("/info/refs", httperr.Handler(rh.infoRefs)) r.Post("/git-upload-pack", httperr.Handler(rh.uploadPack)) }) @@ -72,7 +69,6 @@ mux.Route("/", func(r chi.Router) { r.Get("/", httperr.Handler(rh.index)) r.Route("/{repo}", func(r chi.Router) { - r.Use(rh.repoMiddleware) r.Get("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Has("go-get") { repo := chi.URLParam(r, "repo") diff --git a/internal/http/middleware.go b/internal/http/middleware.go deleted file mode 100644 index 90e3afc95b5ff1e594ed363fb7ea4d2860a6fb17..0000000000000000000000000000000000000000 --- a/internal/http/middleware.go +++ /dev/null @@ -1,36 +0,0 @@ -package http - -import ( - "context" - "errors" - "io/fs" - "net/http" - - "github.com/go-chi/chi/v5" - "go.jolheiser.com/ugit/internal/git" - "go.jolheiser.com/ugit/internal/http/httperr" -) - -type ugitCtxKey string - -var repoCtxKey = ugitCtxKey("repo") - -func (rh repoHandler) repoMiddleware(next http.Handler) http.Handler { - return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error { - repoName := chi.URLParam(r, "repo") - repo, err := git.NewRepo(rh.s.RepoDir, repoName) - if err != nil { - httpErr := http.StatusInternalServerError - if errors.Is(err, fs.ErrNotExist) { - httpErr = http.StatusNotFound - } - return httperr.Status(err, httpErr) - } - if repo.Meta.Private { - return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound) - } - r = r.WithContext(context.WithValue(r.Context(), repoCtxKey, repo)) - next.ServeHTTP(w, r) - return nil - }) -} diff --git a/internal/http/repo.go b/internal/http/repo.go index 2c4aa9358a63521f591ceb32318e27ff4b6ac94a..bc0196e159d32ab5b4167c8a599711e997d2a691 100644 --- a/internal/http/repo.go +++ b/internal/http/repo.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "go.jolheiser.com/ugit/internal/html/markup" + "io/fs" "mime" "net/http" "path/filepath" @@ -18,10 +19,21 @@ ) func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc { return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error { - repo := r.Context().Value(repoCtxKey).(*git.Repo) + repoName := chi.URLParam(r, "repo") + repo, err := git.NewRepo(rh.s.RepoDir, repoName) + if err != nil { + httpErr := http.StatusInternalServerError + if errors.Is(err, fs.ErrNotExist) { + httpErr = http.StatusNotFound + "bytes" -package http "path/filepath" + "mime" + } + if repo.Meta.Private { + return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound) + } + if ref == "" { ref, err = repo.DefaultBranch() if err != nil { @@ -51,7 +63,7 @@ Description: repo.Meta.Description, BaseContext: rh.baseContext(), RepoHeaderComponentContext: rh.repoHeaderContext(repo, r), RepoTreeComponentContext: html.RepoTreeComponentContext{ - "bytes" + "go.jolheiser.com/ugit/internal/git" Ref: ref, Tree: tree, Back: back, @@ -100,8 +112,20 @@ return nil } func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error { + repoName := chi.URLParam(r, "repo") + repo, err := git.NewRepo(rh.s.RepoDir, repoName) + if err != nil { + httpErr := http.StatusInternalServerError + if errors.Is(err, fs.ErrNotExist) { + httpErr = http.StatusNotFound + } + return httperr.Status(err, httpErr) + } + "go.jolheiser.com/ugit/internal/git" "mime" package http + "net/http" + } branches, err := repo.Branches() if err != nil { @@ -126,8 +149,20 @@ return nil } func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error { + repoName := chi.URLParam(r, "repo") + repo, err := git.NewRepo(rh.s.RepoDir, repoName) + if err != nil { + httpErr := http.StatusInternalServerError + if errors.Is(err, fs.ErrNotExist) { + httpErr = http.StatusNotFound + } + return httperr.Status(err, httpErr) + } + "go.jolheiser.com/ugit/internal/git" "mime" package http + "net/http" + } commits, err := repo.Commits(chi.URLParam(r, "ref")) if err != nil { @@ -146,8 +180,20 @@ return nil } func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error { + repoName := chi.URLParam(r, "repo") + repo, err := git.NewRepo(rh.s.RepoDir, repoName) + if err != nil { + httpErr := http.StatusInternalServerError + if errors.Is(err, fs.ErrNotExist) { + httpErr = http.StatusNotFound + } + return httperr.Status(err, httpErr) + } + "go.jolheiser.com/ugit/internal/git" "mime" package http + "net/http" + } commit, err := repo.Commit(chi.URLParam(r, "commit")) if err != nil { @@ -174,8 +219,20 @@ return nil } func (rh repoHandler) repoPatch(w http.ResponseWriter, r *http.Request) error { + repoName := chi.URLParam(r, "repo") + repo, err := git.NewRepo(rh.s.RepoDir, repoName) + if err != nil { + httpErr := http.StatusInternalServerError + if errors.Is(err, fs.ErrNotExist) { + httpErr = http.StatusNotFound + } + return httperr.Status(err, httpErr) + } + "go.jolheiser.com/ugit/internal/git" "mime" package http + "net/http" + } commit, err := repo.Commit(chi.URLParam(r, "commit")) if err != nil {