gitview/internal/routes/handler.go
internal/routes/handler.goRaw
1package routes
2
3import (
4 "net/http"
5
6 "gitview/internal/config"
7)
8
9// Checks for gitprotocol-http(5) specific smells; if found, passes
10// the request on to the git http service, else render the web frontend.
11func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) {
12 path := r.PathValue("rest")
13
14 if r.URL.Query().Get("service") == "git-receive-pack" || path == "git-receive-pack" {
15 w.WriteHeader(http.StatusBadRequest)
16 _, _ = w.Write([]byte("no pushing allowed!"))
17 return
18 }
19
20 switch {
21 case path == "info/refs" &&
22 r.URL.Query().Get("service") == "git-upload-pack" &&
23 r.Method == http.MethodGet:
24 d.InfoRefs(w, r)
25 case path == "git-upload-pack" && r.Method == http.MethodPost:
26 d.UploadPack(w, r)
27 case r.Method == http.MethodGet && path == "":
28 d.RepoIndex(w, r)
29 default:
30 d.Write404(w)
31 }
32}
33
34func Handlers(c *config.Config) *http.ServeMux {
35 mux := http.NewServeMux()
36 d := deps{c}
37
38 mux.HandleFunc("GET /{$}", d.Index)
39 mux.HandleFunc("GET /{name}", d.Multiplex)
40 mux.HandleFunc("POST /{name}", d.Multiplex)
41 mux.HandleFunc("GET /{name}/tree/{ref}/{rest...}", d.RepoTree)
42 mux.HandleFunc("GET /{name}/blob/{ref}/{rest...}", d.FileContent)
43 mux.HandleFunc("GET /{name}/log/{ref}", d.Log)
44 mux.HandleFunc("GET /{name}/archive/{file}", d.Archive)
45 mux.HandleFunc("GET /{name}/commit/{ref}", d.Diff)
46 mux.HandleFunc("GET /{name}/refs/{$}", d.Refs)
47 mux.HandleFunc("GET /{name}/{rest...}", d.Multiplex)
48 mux.HandleFunc("POST /{name}/{rest...}", d.Multiplex)
49
50 // Route static paths separately so nested assets cannot conflict with repository routes.
51 handler := http.NewServeMux()
52 handler.HandleFunc("GET /static/{file...}", d.ServeStatic)
53 handler.Handle("/", mux)
54 return handler
55}