mirror of https://git.jolheiser.com/ugit.git
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
parent
13a3d903d8
commit
6e497c3b7a
|
@ -26,6 +26,11 @@ func (r Repo) Name() string {
|
||||||
return strings.TrimSuffix(filepath.Base(r.path), ".git")
|
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
|
// NewRepo constructs a Repo given a dir and name
|
||||||
func NewRepo(dir, name string) (*Repo, error) {
|
func NewRepo(dir, name string) (*Repo, error) {
|
||||||
if !strings.HasSuffix(name, ".git") {
|
if !strings.HasSuffix(name, ".git") {
|
||||||
|
|
|
@ -3,12 +3,9 @@ package http
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"go.jolheiser.com/ugit/internal/git"
|
"go.jolheiser.com/ugit/internal/git"
|
||||||
"go.jolheiser.com/ugit/internal/http/httperr"
|
"go.jolheiser.com/ugit/internal/http/httperr"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (rh repoHandler) infoRefs(w http.ResponseWriter, r *http.Request) error {
|
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")
|
w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
|
||||||
rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git")
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
||||||
repo, err := git.NewProtocol(rp)
|
protocol, err := git.NewProtocol(repo.Path())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httperr.Error(err)
|
return httperr.Error(err)
|
||||||
}
|
}
|
||||||
if err := repo.HTTPInfoRefs(Session{
|
if err := protocol.HTTPInfoRefs(Session{
|
||||||
w: w,
|
w: w,
|
||||||
r: r,
|
r: r,
|
||||||
}); err != nil {
|
}); 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 {
|
func (rh repoHandler) uploadPack(w http.ResponseWriter, r *http.Request) error {
|
||||||
w.Header().Set("content-type", "application/x-git-upload-pack-result")
|
w.Header().Set("content-type", "application/x-git-upload-pack-result")
|
||||||
rp := filepath.Join(rh.s.RepoDir, chi.URLParam(r, "repo")+".git")
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
||||||
repo, err := git.NewProtocol(rp)
|
protocol, err := git.NewProtocol(repo.Path())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httperr.Error(err)
|
return httperr.Error(err)
|
||||||
}
|
}
|
||||||
if err := repo.HTTPUploadPack(Session{
|
if err := protocol.HTTPUploadPack(Session{
|
||||||
w: w,
|
w: w,
|
||||||
r: r,
|
r: r,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"go.jolheiser.com/ugit/assets"
|
"go.jolheiser.com/ugit/assets"
|
||||||
"go.jolheiser.com/ugit/internal/git"
|
"go.jolheiser.com/ugit/internal/git"
|
||||||
|
@ -61,22 +62,18 @@ func New(settings Settings) Server {
|
||||||
mux.Use(middleware.Recoverer)
|
mux.Use(middleware.Recoverer)
|
||||||
|
|
||||||
rh := repoHandler{s: settings}
|
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) {
|
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.Use(rh.repoMiddleware)
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
repo := r.Context().Value(repoCtxKey).(*git.Repo)
|
||||||
if r.URL.Query().Has("go-get") {
|
if r.URL.Query().Has("go-get") {
|
||||||
repo := chi.URLParam(r, "repo")
|
w.Write([]byte(settings.goGet(repo.Name())))
|
||||||
w.Write([]byte(settings.goGet(repo)))
|
return
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(chi.URLParam(r, "repo"), ".git") {
|
||||||
|
http.Redirect(w, r, "/"+repo.Name(), http.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rh.repoTree("", "").ServeHTTP(w, r)
|
rh.repoTree("", "").ServeHTTP(w, r)
|
||||||
|
@ -88,6 +85,10 @@ func New(settings Settings) Server {
|
||||||
r.Get("/log/{ref}", httperr.Handler(rh.repoLog))
|
r.Get("/log/{ref}", httperr.Handler(rh.repoLog))
|
||||||
r.Get("/commit/{commit}", httperr.Handler(rh.repoCommit))
|
r.Get("/commit/{commit}", httperr.Handler(rh.repoCommit))
|
||||||
r.Get("/commit/{commit}.patch", httperr.Handler(rh.repoPatch))
|
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))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue