routes/handler.go (view raw)
1package routes
2
3import (
4 "log"
5 "net/http"
6 "path/filepath"
7
8 "git.icyphox.sh/legit/config"
9 "github.com/alexedwards/flow"
10 "github.com/sosedoff/gitkit"
11)
12
13type depsWrapper struct {
14 actualDeps deps
15 gitsvc *gitkit.Server
16}
17
18// Checks for gitprotocol-http(5) specific smells; if found, passes
19// the request on to the git http service, else render the web frontend.
20func (dw *depsWrapper) Multiplex(w http.ResponseWriter, r *http.Request) {
21 path := flow.Param(r.Context(), "...")
22 name := flow.Param(r.Context(), "name")
23 name = filepath.Clean(name)
24
25 if r.URL.RawQuery == "service=git-receive-pack" {
26 w.WriteHeader(http.StatusBadRequest)
27 w.Write([]byte("no pushing allowed!"))
28 return
29 }
30
31 if path == "info/refs" && r.URL.RawQuery == "service=git-upload-pack" && r.Method == "GET" {
32 dw.gitsvc.ServeHTTP(w, r)
33 } else if path == "git-upload-pack" && r.Method == "POST" {
34 dw.gitsvc.ServeHTTP(w, r)
35 } else if r.Method == "GET" {
36 dw.actualDeps.RepoIndex(w, r)
37 }
38}
39
40func Handlers(c *config.Config) *flow.Mux {
41 mux := flow.New()
42 d := deps{c}
43
44 gitsvc := gitkit.New(gitkit.Config{
45 Dir: c.Repo.ScanPath,
46 AutoCreate: false,
47 })
48 if err := gitsvc.Setup(); err != nil {
49 log.Fatalf("git server: %s", err)
50 }
51
52 dw := depsWrapper{actualDeps: d, gitsvc: gitsvc}
53
54 mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
55 d.Write404(w)
56 })
57
58 mux.HandleFunc("/", d.Index, "GET")
59 mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
60 mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST")
61 mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
62 mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
63 mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
64 mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
65 mux.HandleFunc("/:name/refs", d.Refs, "GET")
66 mux.HandleFunc("/:name/...", dw.Multiplex, "GET", "POST")
67
68 return mux
69}