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