this repo has no description
1package routes
2
3import (
4 "fmt"
5 "html/template"
6 "net/http"
7 "path/filepath"
8
9 "github.com/go-chi/chi/v5"
10 "github.com/icyphox/bild/legit/config"
11 "github.com/icyphox/bild/legit/db"
12)
13
14// Checks for gitprotocol-http(5) specific smells; if found, passes
15// the request on to the git http service, else render the web frontend.
16func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) {
17 path := chi.URLParam(r, "*")
18
19 if r.URL.RawQuery == "service=git-receive-pack" {
20 w.WriteHeader(http.StatusBadRequest)
21 w.Write([]byte("no pushing allowed!"))
22 return
23 }
24
25 if path == "info/refs" &&
26 r.URL.RawQuery == "service=git-upload-pack" &&
27 r.Method == "GET" {
28 h.InfoRefs(w, r)
29 } else if path == "git-upload-pack" && r.Method == "POST" {
30 h.UploadPack(w, r)
31 } else if r.Method == "GET" {
32 h.RepoIndex(w, r)
33 }
34}
35
36func Setup(c *config.Config) (http.Handler, error) {
37 r := chi.NewRouter()
38 t := template.Must(template.ParseGlob(filepath.Join(c.Dirs.Templates, "*")))
39 db, err := db.Setup(c.Server.DBPath)
40
41 if err != nil {
42 return nil, fmt.Errorf("failed to setup db: %w", err)
43 }
44
45 h := Handle{
46 c: c,
47 t: t,
48 db: db,
49 }
50
51 r.Get("/login", h.Login)
52 r.Get("/static/{file}", h.ServeStatic)
53
54 r.Route("/settings", func(r chi.Router) {
55 r.Get("/keys", h.Keys)
56 r.Put("/keys", h.Keys)
57 })
58
59 r.Route("/@{user}", func(r chi.Router) {
60 r.Get("/", h.Index)
61
62 // Repo routes
63 r.Route("/{name}", func(r chi.Router) {
64 r.Get("/", h.Multiplex)
65 r.Post("/", h.Multiplex)
66
67 r.Route("/tree/{ref}", func(r chi.Router) {
68 r.Get("/*", h.RepoTree)
69 })
70
71 r.Route("/blob/{ref}", func(r chi.Router) {
72 r.Get("/*", h.FileContent)
73 })
74
75 r.Get("/log/{ref}", h.Log)
76 r.Get("/archive/{file}", h.Archive)
77 r.Get("/commit/{ref}", h.Diff)
78 r.Get("/refs/", h.Refs)
79
80 // Catch-all routes
81 r.Get("/*", h.Multiplex)
82 r.Post("/*", h.Multiplex)
83 })
84 })
85
86 return r, nil
87}