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/auth"
13 "github.com/icyphox/bild/config"
14 database "github.com/icyphox/bild/db"
15 "github.com/icyphox/bild/routes/middleware"
16 "github.com/icyphox/bild/routes/tmpl"
17)
18
19// Checks for gitprotocol-http(5) specific smells; if found, passes
20// the request on to the git http service, else render the web frontend.
21func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) {
22 path := chi.URLParam(r, "*")
23
24 if r.URL.RawQuery == "service=git-receive-pack" {
25 w.WriteHeader(http.StatusBadRequest)
26 w.Write([]byte("no pushing allowed!"))
27 return
28 }
29
30 if path == "info/refs" &&
31 r.URL.RawQuery == "service=git-upload-pack" &&
32 r.Method == "GET" {
33 h.InfoRefs(w, r)
34 } else if path == "git-upload-pack" && r.Method == "POST" {
35 h.UploadPack(w, r)
36 } else if r.Method == "GET" {
37 h.RepoIndex(w, r)
38 }
39}
40
41func Setup(c *config.Config, db *database.DB) (http.Handler, error) {
42 r := chi.NewRouter()
43 s := sessions.NewCookieStore([]byte("TODO_CHANGE_ME"))
44 t, err := tmpl.Load(c.Dirs.Templates)
45 if err != nil {
46 return nil, fmt.Errorf("failed to load templates: %w", err)
47 }
48
49 auth := auth.NewAuth(s)
50
51 h := Handle{
52 c: c,
53 t: t,
54 s: s,
55 db: db,
56 auth: auth,
57 }
58
59 r.Get("/", h.Timeline)
60
61 r.Group(func(r chi.Router) {
62 r.Get("/login", h.Login)
63 r.Post("/login", h.Login)
64 })
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.Use(middleware.AddDID)
83 r.Get("/", h.Index)
84
85 // Repo routes
86 r.Route("/{name}", func(r chi.Router) {
87 r.Get("/", h.Multiplex)
88 r.Post("/", h.Multiplex)
89
90 r.Route("/tree/{ref}", func(r chi.Router) {
91 r.Get("/*", h.RepoTree)
92 })
93
94 r.Route("/blob/{ref}", func(r chi.Router) {
95 r.Get("/*", h.FileContent)
96 })
97
98 r.Get("/log/{ref}", h.Log)
99 r.Get("/archive/{file}", h.Archive)
100 r.Get("/commit/{ref}", h.Diff)
101 r.Get("/refs/", h.Refs)
102
103 r.Group(func(r chi.Router) {
104 // settings page is only accessible to owners
105 r.Use(h.AccessLevel(database.Owner))
106 r.Route("/settings", func(r chi.Router) {
107 r.Put("/collaborators", h.Collaborators)
108 })
109 })
110
111 // Catch-all routes
112 r.Get("/*", h.Multiplex)
113 r.Post("/*", h.Multiplex)
114 })
115 })
116
117 return r, nil
118}