this repo has no description
1package routes
2
3import (
4 "html/template"
5 "net/http"
6
7 "github.com/go-chi/chi/v5"
8 "github.com/icyphox/bild/legit/config"
9)
10
11// Checks for gitprotocol-http(5) specific smells; if found, passes
12// the request on to the git http service, else render the web frontend.
13func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) {
14 path := chi.URLParam(r, "*")
15
16 if r.URL.RawQuery == "service=git-receive-pack" {
17 w.WriteHeader(http.StatusBadRequest)
18 w.Write([]byte("no pushing allowed!"))
19 return
20 }
21
22 if path == "info/refs" &&
23 r.URL.RawQuery == "service=git-upload-pack" &&
24 r.Method == "GET" {
25 d.InfoRefs(w, r)
26 } else if path == "git-upload-pack" && r.Method == "POST" {
27 d.UploadPack(w, r)
28 } else if r.Method == "GET" {
29 d.RepoIndex(w, r)
30 }
31}
32
33func Handlers(c *config.Config, t *template.Template) http.Handler {
34 r := chi.NewRouter()
35 d := deps{c, t}
36
37 r.Get("/login", d.Login)
38 r.Get("/static/{file}", d.ServeStatic)
39
40 r.Route("/@{user}", func(r chi.Router) {
41 r.Get("/", d.Index)
42 r.Route("/{name}", func(r chi.Router) {
43 r.Get("/", d.Multiplex)
44 r.Post("/", d.Multiplex)
45
46 r.Route("/tree/{ref}", func(r chi.Router) {
47 r.Get("/*", d.RepoTree)
48 })
49
50 r.Route("/blob/{ref}", func(r chi.Router) {
51 r.Get("/*", d.FileContent)
52 })
53
54 r.Get("/log/{ref}", d.Log)
55 r.Get("/archive/{file}", d.Archive)
56 r.Get("/commit/{ref}", d.Diff)
57 r.Get("/refs/", d.Refs)
58
59 // Catch-all routes
60 r.Get("/*", d.Multiplex)
61 r.Post("/*", d.Multiplex)
62 })
63 })
64
65 return r
66}