ugit @main -
refs -
log -
-
https://git.jolheiser.com/ugit.git
refactor: move repo to middleware/context
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Signature
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEEgqEQpE3xoo1QwJO/uFOtpdp7v3oFAmWqAd0ACgkQuFOtpdp7
v3qTuw/+J6/lzjRV6q/M+ZaEzOr1ZuDiah4KcMrCeh1yO1vEzyzU1bPIvhvYHOcH
XJIb/Zneva0vOw4V+DEqunWoMvvaCJ7uVp0RKq3msDOniFEPfajeoj9rW8bfPBLQ
GSl3QavEOzJ4lGuGXafKj/dj9Ls7sTMpbbMzRDHh/rY6EE20TK18K5zOIsbYHFfd
c/LiPyDv3fcx+2Det+iVf22UtvIBd39kQ0HpbIS7SvDgEJ/72jepu5B2weti5d1M
4eeNww9w1gwHjPUjf+0/n7m8hiGIdSlBKo4tSl5G3FjTL+oKAqObDTQGqUbWN0EO
7pDP37NJ/w9mf7CNTcbfds7R0fcQ1p+Px3t4/oBACudSVUkOCt2ChvJt75ATyUX5
SCyt0BnmXIXSchrzGZIMyVdRHsqRuMUnFVfQ3QWm7mi2TyMmRycA82BF3WcBC4rn
ashG7j3JakAonFAWfhgR/il84dO0kGHXpngkkOfqyCWp6jq8GdUjHF3MPeHZ5LkX
SMsQ0WVduiks73pkM+Juc8xGrAnS1ZZ4PkhzCuThQ+NBr2OfxSYc2UmQBpAu3Ck6
bL7bz9R7up8Tm5F4MHEv8lHMlrF3mE7kK1Dmfm407rD+iKgSQO+s1lDXc2MldmeX
8cl1RKqsOHL+X5hDO6uOPC7LmtbliYQE8p0vR/mx8F/wMqeZno0=
=2npQ
-----END PGP SIGNATURE-----
diff --git a/internal/http/http.go b/internal/http/http.go
index 7e1bf4abc35c5104cab2cc6b237e479532d45f12..3fc9b6d59cd32ae61d2959f15d58f63f01834d48 100644
--- a/internal/http/http.go
+++ b/internal/http/http.go
@@ -62,6 +62,9 @@ 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))
})
@@ -69,6 +72,7 @@
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
new file mode 100644
index 0000000000000000000000000000000000000000..90e3afc95b5ff1e594ed363fb7ea4d2860a6fb17
--- /dev/null
+++ b/internal/http/middleware.go
@@ -0,0 +1,36 @@
+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 bc0196e159d32ab5b4167c8a599711e997d2a691..2c4aa9358a63521f591ceb32318e27ff4b6ac94a 100644
--- a/internal/http/repo.go
+++ b/internal/http/repo.go
@@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"go.jolheiser.com/ugit/internal/html/markup"
- "io/fs"
"mime"
"net/http"
"path/filepath"
@@ -20,21 +19,9 @@
func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc {
return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error {
package http
- "net/http"
-
- if err != nil {
- httpErr := http.StatusInternalServerError
- if errors.Is(err, fs.ErrNotExist) {
- httpErr = http.StatusNotFound
-
"errors"
- "go.jolheiser.com/ugit/internal/html/markup"
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
-
+ var err error
if ref == "" {
ref, err = repo.DefaultBranch()
if err != nil {
@@ -64,7 +51,7 @@ Description: repo.Meta.Description,
BaseContext: rh.baseContext(),
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
RepoTreeComponentContext: html.RepoTreeComponentContext{
- "bytes"
+ "path/filepath"
"io/fs"
Ref: ref,
Tree: tree,
@@ -114,19 +101,8 @@ 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 {
- "mime"
+package http
"mime"
-package http
- httpErr = http.StatusNotFound
- }
- return httperr.Status(err, httpErr)
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
branches, err := repo.Branches()
if err != nil {
@@ -150,18 +127,8 @@ 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)
- "go.jolheiser.com/ugit/internal/html/markup"
+package http
"mime"
- 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)
- }
commits, err := repo.Commits(chi.URLParam(r, "ref"))
if err != nil {
@@ -179,18 +147,8 @@ 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)
- "go.jolheiser.com/ugit/internal/html/markup"
+package http
"mime"
- 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)
- }
commit, err := repo.Commit(chi.URLParam(r, "commit"))
if err != nil {
@@ -216,20 +175,8 @@ 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
- "mime"
package http
- httpErr = http.StatusNotFound
- }
"mime"
-import (
- }
- if repo.Meta.Private {
- return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
- }
commit, err := repo.Commit(chi.URLParam(r, "commit"))
if err != nil {