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.Profile) 57 58 r.With(ResolveRepo(s)).Route("/{repo}", func(r chi.Router) { 59 r.Get("/", s.RepoIndex) 60 r.Get("/commits/{ref}", s.RepoLog) 61 r.Route("/tree/{ref}", func(r chi.Router) { 62 r.Get("/", s.RepoIndex) 63 r.Get("/*", s.RepoTree) 64 }) 65 r.Get("/commit/{ref}", s.RepoCommit) 66 r.Get("/branches", s.RepoBranches) 67 r.Route("/tags", func(r chi.Router) { 68 r.Get("/", s.RepoTags) 69 r.Route("/{tag}", func(r chi.Router) { 70 r.Use(middleware.AuthMiddleware(s.auth)) 71 // require auth to download for now 72 r.Get("/download/{file}", s.DownloadArtifact) 73 74 // require repo:push to upload or delete artifacts 75 // 76 // additionally: only the uploader can truly delete an artifact 77 // (record+blob will live on their pds) 78 r.Group(func(r chi.Router) { 79 r.With(RepoPermissionMiddleware(s, "repo:push")) 80 r.Post("/upload", s.AttachArtifact) 81 r.Delete("/{file}", s.DeleteArtifact) 82 }) 83 }) 84 }) 85 r.Get("/blob/{ref}/*", s.RepoBlob) 86 r.Get("/raw/{ref}/*", s.RepoBlobRaw) 87 88 r.Route("/issues", func(r chi.Router) { 89 r.With(middleware.Paginate).Get("/", s.RepoIssues) 90 r.Get("/{issue}", s.RepoSingleIssue) 91 92 r.Group(func(r chi.Router) { 93 r.Use(middleware.AuthMiddleware(s.auth)) 94 r.Get("/new", s.NewIssue) 95 r.Post("/new", s.NewIssue) 96 r.Post("/{issue}/comment", s.NewIssueComment) 97 r.Route("/{issue}/comment/{comment_id}/", func(r chi.Router) { 98 r.Get("/", s.IssueComment) 99 r.Delete("/", s.DeleteIssueComment) 100 r.Get("/edit", s.EditIssueComment) 101 r.Post("/edit", s.EditIssueComment) 102 }) 103 r.Post("/{issue}/close", s.CloseIssue) 104 r.Post("/{issue}/reopen", s.ReopenIssue) 105 }) 106 }) 107 108 r.Route("/fork", func(r chi.Router) { 109 r.Use(middleware.AuthMiddleware(s.auth)) 110 r.Get("/", s.ForkRepo) 111 r.Post("/", s.ForkRepo) 112 }) 113 114 r.Route("/pulls", func(r chi.Router) { 115 r.Get("/", s.RepoPulls) 116 r.With(middleware.AuthMiddleware(s.auth)).Route("/new", func(r chi.Router) { 117 r.Get("/", s.NewPull) 118 r.Get("/patch-upload", s.PatchUploadFragment) 119 r.Post("/validate-patch", s.ValidatePatch) 120 r.Get("/compare-branches", s.CompareBranchesFragment) 121 r.Get("/compare-forks", s.CompareForksFragment) 122 r.Get("/fork-branches", s.CompareForksBranchesFragment) 123 r.Post("/", s.NewPull) 124 }) 125 126 r.Route("/{pull}", func(r chi.Router) { 127 r.Use(ResolvePull(s)) 128 r.Get("/", s.RepoSinglePull) 129 130 r.Route("/round/{round}", func(r chi.Router) { 131 r.Get("/", s.RepoPullPatch) 132 r.Get("/interdiff", s.RepoPullInterdiff) 133 r.Get("/actions", s.PullActions) 134 r.With(middleware.AuthMiddleware(s.auth)).Route("/comment", func(r chi.Router) { 135 r.Get("/", s.PullComment) 136 r.Post("/", s.PullComment) 137 }) 138 }) 139 140 r.Route("/round/{round}.patch", func(r chi.Router) { 141 r.Get("/", s.RepoPullPatchRaw) 142 }) 143 144 r.Group(func(r chi.Router) { 145 r.Use(middleware.AuthMiddleware(s.auth)) 146 r.Route("/resubmit", func(r chi.Router) { 147 r.Get("/", s.ResubmitPull) 148 r.Post("/", s.ResubmitPull) 149 }) 150 r.Post("/close", s.ClosePull) 151 r.Post("/reopen", s.ReopenPull) 152 // collaborators only 153 r.Group(func(r chi.Router) { 154 r.Use(RepoPermissionMiddleware(s, "repo:push")) 155 r.Post("/merge", s.MergePull) 156 // maybe lock, etc. 157 }) 158 }) 159 }) 160 }) 161 162 // These routes get proxied to the knot 163 r.Get("/info/refs", s.InfoRefs) 164 r.Post("/git-upload-pack", s.UploadPack) 165 166 // settings routes, needs auth 167 r.Group(func(r chi.Router) { 168 r.Use(middleware.AuthMiddleware(s.auth)) 169 // repo description can only be edited by owner 170 r.With(RepoPermissionMiddleware(s, "repo:owner")).Route("/description", func(r chi.Router) { 171 r.Put("/", s.RepoDescription) 172 r.Get("/", s.RepoDescription) 173 r.Get("/edit", s.RepoDescriptionEdit) 174 }) 175 r.With(RepoPermissionMiddleware(s, "repo:settings")).Route("/settings", func(r chi.Router) { 176 r.Get("/", s.RepoSettings) 177 r.With(RepoPermissionMiddleware(s, "repo:invite")).Put("/collaborator", s.AddCollaborator) 178 r.With(RepoPermissionMiddleware(s, "repo:delete")).Delete("/delete", s.DeleteRepo) 179 r.Put("/branches/default", s.SetDefaultBranch) 180 }) 181 }) 182 }) 183 }) 184 185 r.NotFound(func(w http.ResponseWriter, r *http.Request) { 186 s.pages.Error404(w) 187 }) 188 189 return r 190} 191 192func (s *State) StandardRouter() http.Handler { 193 r := chi.NewRouter() 194 195 r.Handle("/static/*", s.pages.Static()) 196 197 r.Get("/", s.Timeline) 198 199 r.With(middleware.AuthMiddleware(s.auth)).Post("/logout", s.Logout) 200 201 r.Route("/login", func(r chi.Router) { 202 r.Get("/", s.Login) 203 r.Post("/", s.Login) 204 }) 205 206 r.Route("/knots", func(r chi.Router) { 207 r.Use(middleware.AuthMiddleware(s.auth)) 208 r.Get("/", s.Knots) 209 r.Post("/key", s.RegistrationKey) 210 211 r.Route("/{domain}", func(r chi.Router) { 212 r.Post("/init", s.InitKnotServer) 213 r.Get("/", s.KnotServerInfo) 214 r.Route("/member", func(r chi.Router) { 215 r.Use(KnotOwner(s)) 216 r.Get("/", s.ListMembers) 217 r.Put("/", s.AddMember) 218 r.Delete("/", s.RemoveMember) 219 }) 220 }) 221 }) 222 223 r.Route("/repo", func(r chi.Router) { 224 r.Route("/new", func(r chi.Router) { 225 r.Use(middleware.AuthMiddleware(s.auth)) 226 r.Get("/", s.NewRepo) 227 r.Post("/", s.NewRepo) 228 }) 229 // r.Post("/import", s.ImportRepo) 230 }) 231 232 r.With(middleware.AuthMiddleware(s.auth)).Route("/follow", func(r chi.Router) { 233 r.Post("/", s.Follow) 234 r.Delete("/", s.Follow) 235 }) 236 237 r.With(middleware.AuthMiddleware(s.auth)).Route("/star", func(r chi.Router) { 238 r.Post("/", s.Star) 239 r.Delete("/", s.Star) 240 }) 241 242 r.Route("/profile", func(r chi.Router) { 243 r.Use(middleware.AuthMiddleware(s.auth)) 244 r.Get("/edit-bio", s.EditBioFragment) 245 r.Get("/edit-pins", s.EditPinsFragment) 246 r.Post("/bio", s.UpdateProfileBio) 247 r.Post("/pins", s.UpdateProfilePins) 248 }) 249 250 r.Mount("/settings", s.SettingsRouter()) 251 252 r.Get("/keys/{user}", s.Keys) 253 254 r.NotFound(func(w http.ResponseWriter, r *http.Request) { 255 s.pages.Error404(w) 256 }) 257 return r 258} 259 260func (s *State) SettingsRouter() http.Handler { 261 settings := &settings.Settings{ 262 Db: s.db, 263 Auth: s.auth, 264 Pages: s.pages, 265 Config: s.config, 266 } 267 268 return settings.Router() 269}