forked from
tangled.org/core
Monorepo for Tangled — https://tangled.org
1package state
2
3import (
4 "net/http"
5 "strings"
6
7 "github.com/go-chi/chi/v5"
8 "tangled.sh/tangled.sh/core/appview/middleware"
9 "tangled.sh/tangled.sh/core/appview/settings"
10 "tangled.sh/tangled.sh/core/appview/state/userutil"
11)
12
13func (s *State) Router() http.Handler {
14 router := chi.NewRouter()
15
16 router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
17 pat := chi.URLParam(r, "*")
18 if strings.HasPrefix(pat, "did:") || strings.HasPrefix(pat, "@") {
19 s.UserRouter().ServeHTTP(w, r)
20 } else {
21 // Check if the first path element is a valid handle without '@' or a flattened DID
22 pathParts := strings.SplitN(pat, "/", 2)
23 if len(pathParts) > 0 {
24 if userutil.IsHandleNoAt(pathParts[0]) {
25 // Redirect to the same path but with '@' prefixed to the handle
26 redirectPath := "@" + pat
27 http.Redirect(w, r, "/"+redirectPath, http.StatusFound)
28 return
29 } else if userutil.IsFlattenedDid(pathParts[0]) {
30 // Redirect to the unflattened DID version
31 unflattenedDid := userutil.UnflattenDid(pathParts[0])
32 var redirectPath string
33 if len(pathParts) > 1 {
34 redirectPath = unflattenedDid + "/" + pathParts[1]
35 } else {
36 redirectPath = unflattenedDid
37 }
38 http.Redirect(w, r, "/"+redirectPath, http.StatusFound)
39 return
40 }
41 }
42 s.StandardRouter().ServeHTTP(w, r)
43 }
44 })
45
46 return router
47}
48
49func (s *State) UserRouter() http.Handler {
50 r := chi.NewRouter()
51
52 // strip @ from user
53 r.Use(StripLeadingAt)
54
55 r.With(ResolveIdent(s)).Route("/{user}", func(r chi.Router) {
56 r.Get("/", s.ProfilePage)
57 r.Route("/profile", func(r chi.Router) {
58 r.Use(middleware.AuthMiddleware(s.auth))
59 r.Get("/edit-bio", s.EditBioFragment)
60 r.Get("/edit-pins", s.EditPinsFragment)
61 r.Post("/bio", s.UpdateProfileBio)
62 r.Post("/pins", s.UpdateProfilePins)
63 })
64
65 r.With(ResolveRepo(s)).Route("/{repo}", func(r chi.Router) {
66 r.Get("/", s.RepoIndex)
67 r.Get("/commits/{ref}", s.RepoLog)
68 r.Route("/tree/{ref}", func(r chi.Router) {
69 r.Get("/", s.RepoIndex)
70 r.Get("/*", s.RepoTree)
71 })
72 r.Get("/commit/{ref}", s.RepoCommit)
73 r.Get("/branches", s.RepoBranches)
74 r.Route("/tags", func(r chi.Router) {
75 r.Get("/", s.RepoTags)
76 r.Route("/{tag}", func(r chi.Router) {
77 r.Use(middleware.AuthMiddleware(s.auth))
78 // require auth to download for now
79 r.Get("/download/{file}", s.DownloadArtifact)
80
81 // require repo:push to upload or delete artifacts
82 //
83 // additionally: only the uploader can truly delete an artifact
84 // (record+blob will live on their pds)
85 r.Group(func(r chi.Router) {
86 r.With(RepoPermissionMiddleware(s, "repo:push"))
87 r.Post("/upload", s.AttachArtifact)
88 r.Delete("/{file}", s.DeleteArtifact)
89 })
90 })
91 })
92 r.Get("/blob/{ref}/*", s.RepoBlob)
93 r.Get("/raw/{ref}/*", s.RepoBlobRaw)
94
95 r.Route("/issues", func(r chi.Router) {
96 r.With(middleware.Paginate).Get("/", s.RepoIssues)
97 r.Get("/{issue}", s.RepoSingleIssue)
98
99 r.Group(func(r chi.Router) {
100 r.Use(middleware.AuthMiddleware(s.auth))
101 r.Get("/new", s.NewIssue)
102 r.Post("/new", s.NewIssue)
103 r.Post("/{issue}/comment", s.NewIssueComment)
104 r.Route("/{issue}/comment/{comment_id}/", func(r chi.Router) {
105 r.Get("/", s.IssueComment)
106 r.Delete("/", s.DeleteIssueComment)
107 r.Get("/edit", s.EditIssueComment)
108 r.Post("/edit", s.EditIssueComment)
109 })
110 r.Post("/{issue}/close", s.CloseIssue)
111 r.Post("/{issue}/reopen", s.ReopenIssue)
112 })
113 })
114
115 r.Route("/fork", func(r chi.Router) {
116 r.Use(middleware.AuthMiddleware(s.auth))
117 r.Get("/", s.ForkRepo)
118 r.Post("/", s.ForkRepo)
119 })
120
121 r.Route("/pulls", func(r chi.Router) {
122 r.Get("/", s.RepoPulls)
123 r.With(middleware.AuthMiddleware(s.auth)).Route("/new", func(r chi.Router) {
124 r.Get("/", s.NewPull)
125 r.Get("/patch-upload", s.PatchUploadFragment)
126 r.Post("/validate-patch", s.ValidatePatch)
127 r.Get("/compare-branches", s.CompareBranchesFragment)
128 r.Get("/compare-forks", s.CompareForksFragment)
129 r.Get("/fork-branches", s.CompareForksBranchesFragment)
130 r.Post("/", s.NewPull)
131 })
132
133 r.Route("/{pull}", func(r chi.Router) {
134 r.Use(ResolvePull(s))
135 r.Get("/", s.RepoSinglePull)
136
137 r.Route("/round/{round}", func(r chi.Router) {
138 r.Get("/", s.RepoPullPatch)
139 r.Get("/interdiff", s.RepoPullInterdiff)
140 r.Get("/actions", s.PullActions)
141 r.With(middleware.AuthMiddleware(s.auth)).Route("/comment", func(r chi.Router) {
142 r.Get("/", s.PullComment)
143 r.Post("/", s.PullComment)
144 })
145 })
146
147 r.Route("/round/{round}.patch", func(r chi.Router) {
148 r.Get("/", s.RepoPullPatchRaw)
149 })
150
151 r.Group(func(r chi.Router) {
152 r.Use(middleware.AuthMiddleware(s.auth))
153 r.Route("/resubmit", func(r chi.Router) {
154 r.Get("/", s.ResubmitPull)
155 r.Post("/", s.ResubmitPull)
156 })
157 r.Post("/close", s.ClosePull)
158 r.Post("/reopen", s.ReopenPull)
159 // collaborators only
160 r.Group(func(r chi.Router) {
161 r.Use(RepoPermissionMiddleware(s, "repo:push"))
162 r.Post("/merge", s.MergePull)
163 // maybe lock, etc.
164 })
165 })
166 })
167 })
168
169 // These routes get proxied to the knot
170 r.Get("/info/refs", s.InfoRefs)
171 r.Post("/git-upload-pack", s.UploadPack)
172
173 // settings routes, needs auth
174 r.Group(func(r chi.Router) {
175 r.Use(middleware.AuthMiddleware(s.auth))
176 // repo description can only be edited by owner
177 r.With(RepoPermissionMiddleware(s, "repo:owner")).Route("/description", func(r chi.Router) {
178 r.Put("/", s.RepoDescription)
179 r.Get("/", s.RepoDescription)
180 r.Get("/edit", s.RepoDescriptionEdit)
181 })
182 r.With(RepoPermissionMiddleware(s, "repo:settings")).Route("/settings", func(r chi.Router) {
183 r.Get("/", s.RepoSettings)
184 r.With(RepoPermissionMiddleware(s, "repo:invite")).Put("/collaborator", s.AddCollaborator)
185 r.With(RepoPermissionMiddleware(s, "repo:delete")).Delete("/delete", s.DeleteRepo)
186 r.Put("/branches/default", s.SetDefaultBranch)
187 })
188 })
189 })
190 })
191
192 r.NotFound(func(w http.ResponseWriter, r *http.Request) {
193 s.pages.Error404(w)
194 })
195
196 return r
197}
198
199func (s *State) StandardRouter() http.Handler {
200 r := chi.NewRouter()
201
202 r.Handle("/static/*", s.pages.Static())
203
204 r.Get("/", s.Timeline)
205
206 r.With(middleware.AuthMiddleware(s.auth)).Post("/logout", s.Logout)
207
208 r.Route("/login", func(r chi.Router) {
209 r.Get("/", s.Login)
210 r.Post("/", s.Login)
211 })
212
213 r.Route("/knots", func(r chi.Router) {
214 r.Use(middleware.AuthMiddleware(s.auth))
215 r.Get("/", s.Knots)
216 r.Post("/key", s.RegistrationKey)
217
218 r.Route("/{domain}", func(r chi.Router) {
219 r.Post("/init", s.InitKnotServer)
220 r.Get("/", s.KnotServerInfo)
221 r.Route("/member", func(r chi.Router) {
222 r.Use(KnotOwner(s))
223 r.Get("/", s.ListMembers)
224 r.Put("/", s.AddMember)
225 r.Delete("/", s.RemoveMember)
226 })
227 })
228 })
229
230 r.Route("/repo", func(r chi.Router) {
231 r.Route("/new", func(r chi.Router) {
232 r.Use(middleware.AuthMiddleware(s.auth))
233 r.Get("/", s.NewRepo)
234 r.Post("/", s.NewRepo)
235 })
236 // r.Post("/import", s.ImportRepo)
237 })
238
239 r.With(middleware.AuthMiddleware(s.auth)).Route("/follow", func(r chi.Router) {
240 r.Post("/", s.Follow)
241 r.Delete("/", s.Follow)
242 })
243
244 r.With(middleware.AuthMiddleware(s.auth)).Route("/star", func(r chi.Router) {
245 r.Post("/", s.Star)
246 r.Delete("/", s.Star)
247 })
248
249 r.Mount("/settings", s.SettingsRouter())
250
251 r.Get("/keys/{user}", s.Keys)
252
253 r.NotFound(func(w http.ResponseWriter, r *http.Request) {
254 s.pages.Error404(w)
255 })
256 return r
257}
258
259func (s *State) SettingsRouter() http.Handler {
260 settings := &settings.Settings{
261 Db: s.db,
262 Auth: s.auth,
263 Pages: s.pages,
264 Config: s.config,
265 }
266
267 return settings.Router()
268}