fix: move protocol to main handler

This also handles the middleware correctly and allows for dots in repo names

Signed-off-by: jolheiser <john.olheiser@gmail.com>
ffdhall
jolheiser 2024-01-20 15:49:38 -06:00
parent 13a3d903d8
commit 6e497c3b7a
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
3 changed files with 22 additions and 19 deletions

View File

@ -26,6 +26,11 @@ func (r Repo) Name() string {
return strings.TrimSuffix(filepath.Base(r.path), ".git")
}
// Path returns the path to the Repo
func (r Repo) Path() string {
return r.path
}
// NewRepo constructs a Repo given a dir and name
func NewRepo(dir, name string) (*Repo, error) {
if !strings.HasSuffix(name, ".git") {

View File

@ -3,12 +3,9 @@ package http
import (
"errors"
"net/http"
"path/filepath"
"go.jolheiser.com/ugit/internal/git"
"go.jolheiser.com/ugit/internal/http/httperr"
"github.com/go-chi/chi/v5"
)
func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error {
@ -17,12 +14,12 @@ func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error {
}
w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git")
repo, err := git.NewProtocol(rp)
repo := r.Context().Value(repoCtxKey).(*git.Repo)
protocol, err := git.NewProtocol(repo.Path())
if err != nil {
return httperr.Error(err)
}
if err := repo.HTTPInfoRefs(Session{
if err := protocol.HTTPInfoRefs(Session{
w: w,
r: r,
}); err != nil {
@ -34,12 +31,12 @@ func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error {
func (rh repoHandler) uploadPack(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("content-type", "application/x-git-upload-pack-result")
rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git")
repo, err := git.NewProtocol(rp)
repo := r.Context().Value(repoCtxKey).(*git.Repo)
protocol, err := git.NewProtocol(repo.Path())
if err != nil {
return httperr.Error(err)
}
if err := repo.HTTPUploadPack(Session{
if err := protocol.HTTPUploadPack(Session{
w: w,
r: r,
}); err != nil {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"go.jolheiser.com/ugit/assets"
"go.jolheiser.com/ugit/internal/git"
@ -61,22 +62,18 @@ func New(settings Settings) Server {
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))
})
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) {
repo := r.Context().Value(repoCtxKey).(*git.Repo)
if r.URL.Query().Has("go-get") {
repo := chi.URLParam(r, "repo")
w.Write([]byte(settings.goGet(repo)))
w.Write([]byte(settings.goGet(repo.Name())))
return
}
if strings.HasSuffix(chi.URLParam(r, "repo"), ".git") {
http.Redirect(w, r, "/"+repo.Name(), http.StatusFound)
return
}
rh.repoTree("", "").ServeHTTP(w, r)
@ -88,6 +85,10 @@ func New(settings Settings) Server {
r.Get("/log/{ref}", httperr.Handler(rh.repoLog))
r.Get("/commit/{commit}", httperr.Handler(rh.repoCommit))
r.Get("/commit/{commit}.patch", httperr.Handler(rh.repoPatch))
// Protocol
r.Get("/info/refs", httperr.Handler(rh.infoRefs))
r.Post("/git-upload-pack", httperr.Handler(rh.uploadPack))
})
})