routes: multiplex git and web ui

This commit is contained in:
Anirudh Oppiliappan 2022-12-14 21:14:34 +05:30
parent 1872ca726a
commit 1b27f32cc2
No known key found for this signature in database
GPG key ID: 8A93F96F78C5D4C4
3 changed files with 47 additions and 1 deletions

2
go.mod
View file

@ -18,12 +18,14 @@ require (
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/sosedoff/gitkit v0.3.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/mod v0.7.0 // indirect

5
go.sum
View file

@ -35,6 +35,8 @@ github.com/go-git/go-git-fixtures/v4 v4.3.1 h1:y5z6dd3qi8Hl+stezc8p3JxDkoTRqMAlK
github.com/go-git/go-git-fixtures/v4 v4.3.1/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo=
github.com/go-git/go-git/v5 v5.5.1 h1:5vtv2TB5PM/gPM+EvsHJ16hJh4uAkdGcKilcwY7FYwo=
github.com/go-git/go-git/v5 v5.5.1/go.mod h1:uz5PQ3d0gz7mSgzZhSJToM6ALPaKCdSnl58/Xb5hzr8=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
@ -66,6 +68,8 @@ github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skeema/knownhosts v1.1.0 h1:Wvr9V0MxhjRbl3f9nMnKnFfiWTJmtECJ9Njkea3ysW0=
github.com/skeema/knownhosts v1.1.0/go.mod h1:sKFq3RD6/TKZkSWn8boUbDC7Qkgcv+8XXijpFO6roag=
github.com/sosedoff/gitkit v0.3.0 h1:TfINVRNUM+GcFa+LGhZ3RcWN86Im1M6i8qs0IsgMy90=
github.com/sosedoff/gitkit v0.3.0/go.mod h1:V3EpGZ0nvCBhXerPsbDeqtyReNb48cwP9KtkUYTKT5I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@ -75,6 +79,7 @@ github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

View file

@ -1,26 +1,65 @@
package routes
import (
"log"
"net/http"
"path/filepath"
"regexp"
"github.com/alexedwards/flow"
"github.com/sosedoff/gitkit"
"icyphox.sh/legit/config"
)
type depsWrapper struct {
actualDeps deps
gitsvc *gitkit.Server
}
// Checks for gitprotocol-http(5) specific query params; if found, passes
// the request on to the git http service, else render the web frontend.
func (dw *depsWrapper) Multiplex(w http.ResponseWriter, r *http.Request) {
path := flow.Param(r.Context(), "...")
name := flow.Param(r.Context(), "name")
name = filepath.Clean(name)
gitCommand := regexp.MustCompile(`git-(upload|receive)-pack`)
if path == "info/refs" && gitCommand.MatchString(r.URL.RawQuery) && r.Method == "GET" {
dw.gitsvc.ServeHTTP(w, r)
} else if gitCommand.MatchString(path) && r.Method == "POST" {
dw.gitsvc.ServeHTTP(w, r)
} else if r.Method == "GET" {
log.Println("index:", r.URL.String())
dw.actualDeps.Index(w, r)
}
}
func Handlers(c *config.Config) *flow.Mux {
mux := flow.New()
d := deps{c}
gitsvc := gitkit.New(gitkit.Config{
Dir: c.Repo.ScanPath,
AutoCreate: false,
})
if err := gitsvc.Setup(); err != nil {
log.Fatalf("git server: %s", err)
}
dw := depsWrapper{actualDeps: d, gitsvc: gitsvc}
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
d.Write404(w)
})
mux.HandleFunc("/", d.Index, "GET")
mux.HandleFunc("/:name", d.RepoIndex, "GET")
mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST")
mux.HandleFunc("/:name/...", dw.Multiplex, "GET", "POST")
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
mux.HandleFunc("/:name/refs", d.Refs, "GET")
return mux
}