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/auth"
15 "github.com/icyphox/bild/legit/routes/tmpl"
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 (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) {
21 path := chi.URLParam(r, "*")
22
23 if r.URL.RawQuery == "service=git-receive-pack" {
24 w.WriteHeader(http.StatusBadRequest)
25 w.Write([]byte("no pushing allowed!"))
26 return
27 }
28
29 if path == "info/refs" &&
30 r.URL.RawQuery == "service=git-upload-pack" &&
31 r.Method == "GET" {
32 h.InfoRefs(w, r)
33 } else if path == "git-upload-pack" && r.Method == "POST" {
34 h.UploadPack(w, r)
35 } else if r.Method == "GET" {
36 h.RepoIndex(w, r)
37 }
38}
39
40func Setup(c *config.Config) (http.Handler, error) {
41 r := chi.NewRouter()
42 s := sessions.NewCookieStore([]byte("TODO_CHANGE_ME"))
43 t, err := tmpl.Load(c.Dirs.Templates)
44 if err != nil {
45 return nil, fmt.Errorf("failed to load templates: %w", err)
46 }
47
48 auth := auth.NewAuth(s)
49
50 db, err := db.Setup(c.Server.DBPath)
51 if err != nil {
52 return nil, fmt.Errorf("failed to setup db: %w", err)
53 }
54
55 h := Handle{
56 c: c,
57 t: t,
58 s: s,
59 db: db,
60 auth: auth,
61 }
62
63 r.Get("/login", h.Login)
64 r.Post("/login", h.Login)
65 r.Get("/static/{file}", h.ServeStatic)
66
67 r.Route("/repo", func(r chi.Router) {
68 r.Use(h.AuthMiddleware)
69 r.Get("/new", h.NewRepo)
70 r.Put("/new", h.NewRepo)
71 })
72
73 r.Group(func(r chi.Router) {
74 r.Use(h.AuthMiddleware)
75 r.Route("/settings", func(r chi.Router) {
76 r.Get("/keys", h.Keys)
77 r.Put("/keys", h.Keys)
78 })
79 })
80
81 r.Route("/@{user}", func(r chi.Router) {
82 r.Get("/", h.Index)
83
84 // Repo routes
85 r.Route("/{name}", func(r chi.Router) {
86 r.Get("/", h.Multiplex)
87 r.Post("/", h.Multiplex)
88
89 r.Route("/tree/{ref}", func(r chi.Router) {
90 r.Get("/*", h.RepoTree)
91 })
92
93 r.Route("/blob/{ref}", func(r chi.Router) {
94 r.Get("/*", h.FileContent)
95 })
96
97 r.Get("/log/{ref}", h.Log)
98 r.Get("/archive/{file}", h.Archive)
99 r.Get("/commit/{ref}", h.Diff)
100 r.Get("/refs/", h.Refs)
101
102 // Catch-all routes
103 r.Get("/*", h.Multiplex)
104 r.Post("/*", h.Multiplex)
105 })
106 })
107
108 return r, nil
109}