mirror of https://git.jolheiser.com/ugit.git
refactor: move repo to middleware/context
Signed-off-by: jolheiser <john.olheiser@gmail.com>ffdhall
parent
22cdff623a
commit
beec45c876
|
@ -62,6 +62,9 @@ func New(settings Settings) Server {
|
||||||
|
|
||||||
rh := repoHandler{s: settings}
|
rh := repoHandler{s: settings}
|
||||||
mux.Route("/{repo}.git", func(r chi.Router) {
|
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.Get("/info/refs", httperr.Handler(rh.infoRefs))
|
||||||
r.Post("/git-upload-pack", httperr.Handler(rh.uploadPack))
|
r.Post("/git-upload-pack", httperr.Handler(rh.uploadPack))
|
||||||
})
|
})
|
||||||
|
@ -69,6 +72,7 @@ func New(settings Settings) Server {
|
||||||
mux.Route("/", func(r chi.Router) {
|
mux.Route("/", func(r chi.Router) {
|
||||||
r.Get("/", httperr.Handler(rh.index))
|
r.Get("/", httperr.Handler(rh.index))
|
||||||
r.Route("/{repo}", func(r chi.Router) {
|
r.Route("/{repo}", func(r chi.Router) {
|
||||||
|
r.Use(rh.repoMiddleware)
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Query().Has("go-get") {
|
if r.URL.Query().Has("go-get") {
|
||||||
repo := chi.URLParam(r, "repo")
|
repo := chi.URLParam(r, "repo")
|
||||||
|
|
|
@ -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
|
||||||
|
})
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"go.jolheiser.com/ugit/internal/html/markup"
|
"go.jolheiser.com/ugit/internal/html/markup"
|
||||||
"io/fs"
|
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -19,19 +18,9 @@ import (
|
||||||
|
|
||||||
func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc {
|
func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc {
|
||||||
return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error {
|
return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error {
|
||||||
repoName := chi.URLParam(r, "repo")
|
repo := r.Context().Value(repoCtxKey).(*git.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var err error
|
||||||
if ref == "" {
|
if ref == "" {
|
||||||
ref, err = repo.DefaultBranch()
|
ref, err = repo.DefaultBranch()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,7 +50,7 @@ func (rh repoHandler) repoTree(ref, path string) http.HandlerFunc {
|
||||||
BaseContext: rh.baseContext(),
|
BaseContext: rh.baseContext(),
|
||||||
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
RepoHeaderComponentContext: rh.repoHeaderContext(repo, r),
|
||||||
RepoTreeComponentContext: html.RepoTreeComponentContext{
|
RepoTreeComponentContext: html.RepoTreeComponentContext{
|
||||||
Repo: repoName,
|
Repo: repo.Name(),
|
||||||
Ref: ref,
|
Ref: ref,
|
||||||
Tree: tree,
|
Tree: tree,
|
||||||
Back: back,
|
Back: back,
|
||||||
|
@ -110,18 +99,7 @@ func (rh repoHandler) repoFile(w http.ResponseWriter, r *http.Request, repo *git
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error {
|
func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error {
|
||||||
repoName := chi.URLParam(r, "repo")
|
repo := r.Context().Value(repoCtxKey).(*git.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
branches, err := repo.Branches()
|
branches, err := repo.Branches()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -146,18 +124,7 @@ func (rh repoHandler) repoRefs(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error {
|
func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error {
|
||||||
repoName := chi.URLParam(r, "repo")
|
repo := r.Context().Value(repoCtxKey).(*git.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
commits, err := repo.Commits(chi.URLParam(r, "ref"))
|
commits, err := repo.Commits(chi.URLParam(r, "ref"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -176,18 +143,7 @@ func (rh repoHandler) repoLog(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
|
func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
|
||||||
repoName := chi.URLParam(r, "repo")
|
repo := r.Context().Value(repoCtxKey).(*git.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
commit, err := repo.Commit(chi.URLParam(r, "commit"))
|
commit, err := repo.Commit(chi.URLParam(r, "commit"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -214,18 +170,7 @@ func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rh repoHandler) repoPatch(w http.ResponseWriter, r *http.Request) error {
|
func (rh repoHandler) repoPatch(w http.ResponseWriter, r *http.Request) error {
|
||||||
repoName := chi.URLParam(r, "repo")
|
repo := r.Context().Value(repoCtxKey).(*git.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
commit, err := repo.Commit(chi.URLParam(r, "commit"))
|
commit, err := repo.Commit(chi.URLParam(r, "commit"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue