legit/routes/handler.go

69 lines
1.8 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"
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
}
2022-12-14 16:10:01 +00:00
// Checks for gitprotocol-http(5) specific smells; if found, passes
2022-12-14 15:44:34 +00:00
// 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)
2022-12-14 16:10:01 +00:00
if r.URL.RawQuery == "service=git-receive-pack" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no pushing allowed!"))
return
}
if path == "info/refs" && r.URL.RawQuery == "service=git-upload-pack" && r.Method == "GET" {
2022-12-14 15:44:34 +00:00
dw.gitsvc.ServeHTTP(w, r)
2022-12-14 16:10:01 +00:00
} else if path == "git-upload-pack" && r.Method == "POST" {
2022-12-14 15:44:34 +00:00
dw.gitsvc.ServeHTTP(w, r)
} else if r.Method == "GET" {
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
}