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