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.IssueComment)
77 r.Post("/{issue}/close", s.CloseIssue)
78 r.Post("/{issue}/reopen", s.ReopenIssue)
79 })
80 })
81
82 r.Route("/pulls", func(r chi.Router) {
83 r.Get("/", s.RepoPulls)
84 r.With(AuthMiddleware(s)).Route("/new", func(r chi.Router) {
85 r.Get("/", s.NewPull)
86 r.Post("/", s.NewPull)
87 })
88
89 r.Route("/{pull}", func(r chi.Router) {
90 r.Use(ResolvePull(s))
91 r.Get("/", s.RepoSinglePull)
92
93 r.Route("/round/{round}", func(r chi.Router) {
94 r.Get("/", s.RepoPullPatch)
95 r.Get("/actions", s.PullActions)
96 r.With(AuthMiddleware(s)).Route("/comment", func(r chi.Router) {
97 r.Get("/", s.PullComment)
98 r.Post("/", s.PullComment)
99 })
100 })
101
102 r.Route("/round/{round}.patch", func(r chi.Router) {
103 r.Get("/", s.RepoPullPatchRaw)
104 })
105
106 r.Group(func(r chi.Router) {
107 r.Use(AuthMiddleware(s))
108 r.Route("/resubmit", func(r chi.Router) {
109 r.Get("/", s.ResubmitPull)
110 r.Post("/", s.ResubmitPull)
111 })
112 r.Post("/close", s.ClosePull)
113 r.Post("/reopen", s.ReopenPull)
114 // collaborators only
115 r.Group(func(r chi.Router) {
116 r.Use(RepoPermissionMiddleware(s, "repo:push"))
117 r.Post("/merge", s.MergePull)
118 // maybe lock, etc.
119 })
120 })
121 })
122 })
123
124 // These routes get proxied to the knot
125 r.Get("/info/refs", s.InfoRefs)
126 r.Post("/git-upload-pack", s.UploadPack)
127
128 // settings routes, needs auth
129 r.Group(func(r chi.Router) {
130 r.Use(AuthMiddleware(s))
131 // repo description can only be edited by owner
132 r.With(RepoPermissionMiddleware(s, "repo:owner")).Route("/description", func(r chi.Router) {
133 r.Put("/", s.RepoDescription)
134 r.Get("/", s.RepoDescription)
135 r.Get("/edit", s.RepoDescriptionEdit)
136 })
137 r.With(RepoPermissionMiddleware(s, "repo:settings")).Route("/settings", func(r chi.Router) {
138 r.Get("/", s.RepoSettings)
139 r.With(RepoPermissionMiddleware(s, "repo:invite")).Put("/collaborator", s.AddCollaborator)
140 })
141 })
142 })
143 })
144
145 r.NotFound(func(w http.ResponseWriter, r *http.Request) {
146 s.pages.Error404(w)
147 })
148
149 return r
150}
151
152func (s *State) StandardRouter() http.Handler {
153 r := chi.NewRouter()
154
155 r.Handle("/static/*", s.pages.Static())
156
157 r.Get("/", s.Timeline)
158
159 r.With(AuthMiddleware(s)).Post("/logout", s.Logout)
160
161 r.Route("/login", func(r chi.Router) {
162 r.Get("/", s.Login)
163 r.Post("/", s.Login)
164 })
165
166 r.Route("/knots", func(r chi.Router) {
167 r.Use(AuthMiddleware(s))
168 r.Get("/", s.Knots)
169 r.Post("/key", s.RegistrationKey)
170
171 r.Route("/{domain}", func(r chi.Router) {
172 r.Post("/init", s.InitKnotServer)
173 r.Get("/", s.KnotServerInfo)
174 r.Route("/member", func(r chi.Router) {
175 r.Use(KnotOwner(s))
176 r.Get("/", s.ListMembers)
177 r.Put("/", s.AddMember)
178 r.Delete("/", s.RemoveMember)
179 })
180 })
181 })
182
183 r.Route("/repo", func(r chi.Router) {
184 r.Route("/new", func(r chi.Router) {
185 r.Use(AuthMiddleware(s))
186 r.Get("/", s.NewRepo)
187 r.Post("/", s.NewRepo)
188 })
189 // r.Post("/import", s.ImportRepo)
190 })
191
192 r.With(AuthMiddleware(s)).Route("/follow", func(r chi.Router) {
193 r.Post("/", s.Follow)
194 r.Delete("/", s.Follow)
195 })
196
197 r.With(AuthMiddleware(s)).Route("/star", func(r chi.Router) {
198 r.Post("/", s.Star)
199 r.Delete("/", s.Star)
200 })
201
202 r.Route("/settings", func(r chi.Router) {
203 r.Use(AuthMiddleware(s))
204 r.Get("/", s.Settings)
205 r.Put("/keys", s.SettingsKeys)
206 r.Delete("/keys", s.SettingsKeys)
207 r.Put("/emails", s.SettingsEmails)
208 r.Delete("/emails", s.SettingsEmails)
209 r.Get("/emails/verify", s.SettingsEmailsVerify)
210 r.Post("/emails/verify/resend", s.SettingsEmailsVerifyResend)
211 r.Post("/emails/primary", s.SettingsEmailsPrimary)
212 })
213
214 r.Get("/keys/{user}", s.Keys)
215
216 r.NotFound(func(w http.ResponseWriter, r *http.Request) {
217 s.pages.Error404(w)
218 })
219 return r
220}