routes/handler.go (view raw)
1package routes
2
3import (
4 "net/http"
5
6 "git.icyphox.sh/legit/config"
7 "github.com/alexedwards/flow"
8)
9
10// Checks for gitprotocol-http(5) specific smells; if found, passes
11// the request on to the git http service, else render the web frontend.
12func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) {
13 path := flow.Param(r.Context(), "...")
14
15 if r.URL.RawQuery == "service=git-receive-pack" {
16 w.WriteHeader(http.StatusBadRequest)
17 w.Write([]byte("no pushing allowed!"))
18 return
19 }
20
21 if path == "info/refs" &&
22 r.URL.RawQuery == "service=git-upload-pack" &&
23 r.Method == "GET" {
24 d.InfoRefs(w, r)
25 } else if path == "git-upload-pack" && r.Method == "POST" {
26 d.UploadPack(w, r)
27 } else if r.Method == "GET" {
28 d.RepoIndex(w, r)
29 }
30}
31
32func Handlers(c *config.Config) *flow.Mux {
33 mux := flow.New()
34 d := deps{c}
35
36 mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
37 d.Write404(w)
38 })
39
40 mux.HandleFunc("/", d.Index, "GET")
41 mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
42 mux.HandleFunc("/:name", d.Multiplex, "GET", "POST")
43 mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
44 mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
45 mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
46 mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
47 mux.HandleFunc("/:name/refs", d.Refs, "GET")
48 mux.HandleFunc("/:name/...", d.Multiplex, "GET", "POST")
49
50 return mux
51}