legit/routes/handler.go

66 lines
1.7 KiB
Go
Raw Normal View History

2022-12-11 05:52:47 +00:00
package routes
import (
2022-12-14 15:44:34 +00:00
"log"
2022-12-12 15:23:58 +00:00
"net/http"
2022-12-14 15:44:34 +00:00
"path/filepath"
"regexp"
2022-12-12 15:23:58 +00:00
2022-12-11 05:52:47 +00:00
"github.com/alexedwards/flow"
2022-12-14 15:44:34 +00:00
"github.com/sosedoff/gitkit"
2022-12-11 05:52:47 +00:00
"icyphox.sh/legit/config"
)
2022-12-14 15:44:34 +00:00
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.RepoIndex(w, r)
2022-12-14 15:44:34 +00:00
}
}
2022-12-11 05:52:47 +00:00
func Handlers(c *config.Config) *flow.Mux {
mux := flow.New()
d := deps{c}
2022-12-12 15:23:58 +00:00
2022-12-14 15:44:34 +00:00
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}
2022-12-12 15:23:58 +00:00
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
d.Write404(w)
})
mux.HandleFunc("/", d.Index, "GET")
2022-12-14 15:44:34 +00:00
mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST")
2022-12-11 08:48:39 +00:00
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
2022-12-11 15:47:04 +00:00
mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
2022-12-12 11:47:49 +00:00
mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
2022-12-12 16:28:47 +00:00
mux.HandleFunc("/:name/refs", d.Refs, "GET")
mux.HandleFunc("/:name/...", dw.Multiplex, "GET", "POST")
2022-12-14 15:44:34 +00:00
2022-12-11 05:52:47 +00:00
return mux
}