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