Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

routes.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routes
  5. import (
  6. "os"
  7. "path"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/lfs"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/options"
  14. "code.gitea.io/gitea/modules/public"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/templates"
  17. "code.gitea.io/gitea/modules/validation"
  18. "code.gitea.io/gitea/routers"
  19. "code.gitea.io/gitea/routers/admin"
  20. apiv1 "code.gitea.io/gitea/routers/api/v1"
  21. "code.gitea.io/gitea/routers/dev"
  22. "code.gitea.io/gitea/routers/org"
  23. "code.gitea.io/gitea/routers/private"
  24. "code.gitea.io/gitea/routers/repo"
  25. "code.gitea.io/gitea/routers/user"
  26. "github.com/go-macaron/binding"
  27. "github.com/go-macaron/cache"
  28. "github.com/go-macaron/captcha"
  29. "github.com/go-macaron/csrf"
  30. "github.com/go-macaron/gzip"
  31. "github.com/go-macaron/i18n"
  32. "github.com/go-macaron/session"
  33. "github.com/go-macaron/toolbox"
  34. "gopkg.in/macaron.v1"
  35. )
  36. // NewMacaron initializes Macaron instance.
  37. func NewMacaron() *macaron.Macaron {
  38. m := macaron.New()
  39. if !setting.DisableRouterLog {
  40. m.Use(macaron.Logger())
  41. }
  42. m.Use(macaron.Recovery())
  43. if setting.EnableGzip {
  44. m.Use(gzip.Gziper())
  45. }
  46. if setting.Protocol == setting.FCGI {
  47. m.SetURLPrefix(setting.AppSubURL)
  48. }
  49. m.Use(public.Custom(
  50. &public.Options{
  51. SkipLogging: setting.DisableRouterLog,
  52. },
  53. ))
  54. m.Use(public.Static(
  55. &public.Options{
  56. Directory: path.Join(setting.StaticRootPath, "public"),
  57. SkipLogging: setting.DisableRouterLog,
  58. },
  59. ))
  60. m.Use(macaron.Static(
  61. setting.AvatarUploadPath,
  62. macaron.StaticOptions{
  63. Prefix: "avatars",
  64. SkipLogging: setting.DisableRouterLog,
  65. ETag: true,
  66. },
  67. ))
  68. m.Use(templates.Renderer())
  69. models.InitMailRender(templates.Mailer())
  70. localeNames, err := options.Dir("locale")
  71. if err != nil {
  72. log.Fatal(4, "Failed to list locale files: %v", err)
  73. }
  74. localFiles := make(map[string][]byte)
  75. for _, name := range localeNames {
  76. localFiles[name], err = options.Locale(name)
  77. if err != nil {
  78. log.Fatal(4, "Failed to load %s locale file. %v", name, err)
  79. }
  80. }
  81. m.Use(i18n.I18n(i18n.Options{
  82. SubURL: setting.AppSubURL,
  83. Files: localFiles,
  84. Langs: setting.Langs,
  85. Names: setting.Names,
  86. DefaultLang: "en-US",
  87. Redirect: true,
  88. }))
  89. m.Use(cache.Cacher(cache.Options{
  90. Adapter: setting.CacheService.Adapter,
  91. AdapterConfig: setting.CacheService.Conn,
  92. Interval: setting.CacheService.Interval,
  93. }))
  94. m.Use(captcha.Captchaer(captcha.Options{
  95. SubURL: setting.AppSubURL,
  96. }))
  97. m.Use(session.Sessioner(setting.SessionConfig))
  98. m.Use(csrf.Csrfer(csrf.Options{
  99. Secret: setting.SecretKey,
  100. Cookie: setting.CSRFCookieName,
  101. SetCookie: true,
  102. Header: "X-Csrf-Token",
  103. CookiePath: setting.AppSubURL,
  104. }))
  105. m.Use(toolbox.Toolboxer(m, toolbox.Options{
  106. HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{
  107. {
  108. Desc: "Database connection",
  109. Func: models.Ping,
  110. },
  111. },
  112. }))
  113. m.Use(context.Contexter())
  114. return m
  115. }
  116. // RegisterRoutes routes routes to Macaron
  117. func RegisterRoutes(m *macaron.Macaron) {
  118. reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})
  119. ignSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: setting.Service.RequireSignInView})
  120. ignSignInAndCsrf := context.Toggle(&context.ToggleOptions{DisableCSRF: true})
  121. reqSignOut := context.Toggle(&context.ToggleOptions{SignOutRequired: true})
  122. bindIgnErr := binding.BindIgnErr
  123. validation.AddBindingRules()
  124. openIDSignInEnabled := func(ctx *context.Context) {
  125. if !setting.Service.EnableOpenIDSignIn {
  126. ctx.Error(403)
  127. return
  128. }
  129. }
  130. openIDSignUpEnabled := func(ctx *context.Context) {
  131. if !setting.Service.EnableOpenIDSignUp {
  132. ctx.Error(403)
  133. return
  134. }
  135. }
  136. m.Use(user.GetNotificationCount)
  137. // FIXME: not all routes need go through same middlewares.
  138. // Especially some AJAX requests, we can reduce middleware number to improve performance.
  139. // Routers.
  140. // for health check
  141. m.Head("/", func() string {
  142. return ""
  143. })
  144. m.Get("/", ignSignIn, routers.Home)
  145. m.Group("/explore", func() {
  146. m.Get("", func(ctx *context.Context) {
  147. ctx.Redirect(setting.AppSubURL + "/explore/repos")
  148. })
  149. m.Get("/repos", routers.ExploreRepos)
  150. m.Get("/users", routers.ExploreUsers)
  151. m.Get("/organizations", routers.ExploreOrganizations)
  152. }, ignSignIn)
  153. m.Combo("/install", routers.InstallInit).Get(routers.Install).
  154. Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  155. m.Get("/^:type(issues|pulls)$", reqSignIn, user.Issues)
  156. // ***** START: User *****
  157. m.Group("/user", func() {
  158. m.Get("/login", user.SignIn)
  159. m.Post("/login", bindIgnErr(auth.SignInForm{}), user.SignInPost)
  160. m.Group("", func() {
  161. m.Combo("/login/openid").
  162. Get(user.SignInOpenID).
  163. Post(bindIgnErr(auth.SignInOpenIDForm{}), user.SignInOpenIDPost)
  164. }, openIDSignInEnabled)
  165. m.Group("/openid", func() {
  166. m.Combo("/connect").
  167. Get(user.ConnectOpenID).
  168. Post(bindIgnErr(auth.ConnectOpenIDForm{}), user.ConnectOpenIDPost)
  169. m.Group("/register", func() {
  170. m.Combo("").
  171. Get(user.RegisterOpenID, openIDSignUpEnabled).
  172. Post(bindIgnErr(auth.SignUpOpenIDForm{}), user.RegisterOpenIDPost)
  173. }, openIDSignUpEnabled)
  174. }, openIDSignInEnabled)
  175. m.Get("/sign_up", user.SignUp)
  176. m.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  177. m.Get("/reset_password", user.ResetPasswd)
  178. m.Post("/reset_password", user.ResetPasswdPost)
  179. m.Group("/oauth2", func() {
  180. m.Get("/:provider", user.SignInOAuth)
  181. m.Get("/:provider/callback", user.SignInOAuthCallback)
  182. })
  183. m.Get("/link_account", user.LinkAccount)
  184. m.Post("/link_account_signin", bindIgnErr(auth.SignInForm{}), user.LinkAccountPostSignIn)
  185. m.Post("/link_account_signup", bindIgnErr(auth.RegisterForm{}), user.LinkAccountPostRegister)
  186. m.Group("/two_factor", func() {
  187. m.Get("", user.TwoFactor)
  188. m.Post("", bindIgnErr(auth.TwoFactorAuthForm{}), user.TwoFactorPost)
  189. m.Get("/scratch", user.TwoFactorScratch)
  190. m.Post("/scratch", bindIgnErr(auth.TwoFactorScratchAuthForm{}), user.TwoFactorScratchPost)
  191. })
  192. }, reqSignOut)
  193. m.Group("/user/settings", func() {
  194. m.Get("", user.Settings)
  195. m.Post("", bindIgnErr(auth.UpdateProfileForm{}), user.SettingsPost)
  196. m.Combo("/avatar").Get(user.SettingsAvatar).
  197. Post(binding.MultipartForm(auth.AvatarForm{}), user.SettingsAvatarPost)
  198. m.Post("/avatar/delete", user.SettingsDeleteAvatar)
  199. m.Combo("/email").Get(user.SettingsEmails).
  200. Post(bindIgnErr(auth.AddEmailForm{}), user.SettingsEmailPost)
  201. m.Post("/email/delete", user.DeleteEmail)
  202. m.Get("/security", user.SettingsSecurity)
  203. m.Post("/security", bindIgnErr(auth.ChangePasswordForm{}), user.SettingsSecurityPost)
  204. m.Group("/openid", func() {
  205. m.Combo("").Get(user.SettingsOpenID).
  206. Post(bindIgnErr(auth.AddOpenIDForm{}), user.SettingsOpenIDPost)
  207. m.Post("/delete", user.DeleteOpenID)
  208. m.Post("/toggle_visibility", user.ToggleOpenIDVisibility)
  209. }, openIDSignInEnabled)
  210. m.Combo("/keys").Get(user.SettingsKeys).
  211. Post(bindIgnErr(auth.AddKeyForm{}), user.SettingsKeysPost)
  212. m.Post("/keys/delete", user.DeleteKey)
  213. m.Combo("/applications").Get(user.SettingsApplications).
  214. Post(bindIgnErr(auth.NewAccessTokenForm{}), user.SettingsApplicationsPost)
  215. m.Post("/applications/delete", user.SettingsDeleteApplication)
  216. m.Route("/delete", "GET,POST", user.SettingsDelete)
  217. m.Combo("/account_link").Get(user.SettingsAccountLinks).Post(user.SettingsDeleteAccountLink)
  218. m.Get("/organization", user.SettingsOrganization)
  219. m.Get("/repos", user.SettingsRepos)
  220. m.Group("/security/two_factor", func() {
  221. m.Post("/regenerate_scratch", user.SettingsTwoFactorRegenerateScratch)
  222. m.Post("/disable", user.SettingsTwoFactorDisable)
  223. m.Get("/enroll", user.SettingsTwoFactorEnroll)
  224. m.Post("/enroll", bindIgnErr(auth.TwoFactorAuthForm{}), user.SettingsTwoFactorEnrollPost)
  225. })
  226. }, reqSignIn, func(ctx *context.Context) {
  227. ctx.Data["PageIsUserSettings"] = true
  228. })
  229. m.Group("/user", func() {
  230. // r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  231. m.Any("/activate", user.Activate)
  232. m.Any("/activate_email", user.ActivateEmail)
  233. m.Get("/email2user", user.Email2User)
  234. m.Get("/forgot_password", user.ForgotPasswd)
  235. m.Post("/forgot_password", user.ForgotPasswdPost)
  236. m.Get("/logout", user.SignOut)
  237. })
  238. // ***** END: User *****
  239. adminReq := context.Toggle(&context.ToggleOptions{SignInRequired: true, AdminRequired: true})
  240. // ***** START: Admin *****
  241. m.Group("/admin", func() {
  242. m.Get("", adminReq, admin.Dashboard)
  243. m.Get("/config", admin.Config)
  244. m.Post("/config/test_mail", admin.SendTestMail)
  245. m.Get("/monitor", admin.Monitor)
  246. m.Group("/users", func() {
  247. m.Get("", admin.Users)
  248. m.Combo("/new").Get(admin.NewUser).Post(bindIgnErr(auth.AdminCreateUserForm{}), admin.NewUserPost)
  249. m.Combo("/:userid").Get(admin.EditUser).Post(bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  250. m.Post("/:userid/delete", admin.DeleteUser)
  251. })
  252. m.Group("/orgs", func() {
  253. m.Get("", admin.Organizations)
  254. })
  255. m.Group("/repos", func() {
  256. m.Get("", admin.Repos)
  257. m.Post("/delete", admin.DeleteRepo)
  258. })
  259. m.Group("/auths", func() {
  260. m.Get("", admin.Authentications)
  261. m.Combo("/new").Get(admin.NewAuthSource).Post(bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)
  262. m.Combo("/:authid").Get(admin.EditAuthSource).
  263. Post(bindIgnErr(auth.AuthenticationForm{}), admin.EditAuthSourcePost)
  264. m.Post("/:authid/delete", admin.DeleteAuthSource)
  265. })
  266. m.Group("/notices", func() {
  267. m.Get("", admin.Notices)
  268. m.Post("/delete", admin.DeleteNotices)
  269. m.Get("/empty", admin.EmptyNotices)
  270. })
  271. }, adminReq)
  272. // ***** END: Admin *****
  273. m.Group("", func() {
  274. m.Group("/:username", func() {
  275. m.Get("", user.Profile)
  276. m.Get("/followers", user.Followers)
  277. m.Get("/following", user.Following)
  278. })
  279. m.Get("/attachments/:uuid", func(ctx *context.Context) {
  280. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  281. if err != nil {
  282. if models.IsErrAttachmentNotExist(err) {
  283. ctx.Error(404)
  284. } else {
  285. ctx.Handle(500, "GetAttachmentByUUID", err)
  286. }
  287. return
  288. }
  289. fr, err := os.Open(attach.LocalPath())
  290. if err != nil {
  291. ctx.Handle(500, "Open", err)
  292. return
  293. }
  294. defer fr.Close()
  295. if err := attach.IncreaseDownloadCount(); err != nil {
  296. ctx.Handle(500, "Update", err)
  297. return
  298. }
  299. if err = repo.ServeData(ctx, attach.Name, fr); err != nil {
  300. ctx.Handle(500, "ServeData", err)
  301. return
  302. }
  303. })
  304. m.Post("/attachments", repo.UploadAttachment)
  305. }, ignSignIn)
  306. m.Group("/:username", func() {
  307. m.Get("/action/:action", user.Action)
  308. }, reqSignIn)
  309. if macaron.Env == macaron.DEV {
  310. m.Get("/template/*", dev.TemplatePreview)
  311. }
  312. reqRepoAdmin := context.RequireRepoAdmin()
  313. reqRepoWriter := context.RequireRepoWriter()
  314. // ***** START: Organization *****
  315. m.Group("/org", func() {
  316. m.Group("", func() {
  317. m.Get("/create", org.Create)
  318. m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
  319. }, func(ctx *context.Context) {
  320. if !ctx.User.CanCreateOrganization() {
  321. ctx.NotFound()
  322. }
  323. })
  324. m.Group("/:org", func() {
  325. m.Get("/dashboard", user.Dashboard)
  326. m.Get("/^:type(issues|pulls)$", user.Issues)
  327. m.Get("/members", org.Members)
  328. m.Get("/members/action/:action", org.MembersAction)
  329. m.Get("/teams", org.Teams)
  330. }, context.OrgAssignment(true))
  331. m.Group("/:org", func() {
  332. m.Get("/teams/:team", org.TeamMembers)
  333. m.Get("/teams/:team/repositories", org.TeamRepositories)
  334. m.Route("/teams/:team/action/:action", "GET,POST", org.TeamsAction)
  335. m.Route("/teams/:team/action/repo/:action", "GET,POST", org.TeamsRepoAction)
  336. }, context.OrgAssignment(true, false, true))
  337. m.Group("/:org", func() {
  338. m.Get("/teams/new", org.NewTeam)
  339. m.Post("/teams/new", bindIgnErr(auth.CreateTeamForm{}), org.NewTeamPost)
  340. m.Get("/teams/:team/edit", org.EditTeam)
  341. m.Post("/teams/:team/edit", bindIgnErr(auth.CreateTeamForm{}), org.EditTeamPost)
  342. m.Post("/teams/:team/delete", org.DeleteTeam)
  343. m.Group("/settings", func() {
  344. m.Combo("").Get(org.Settings).
  345. Post(bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost)
  346. m.Post("/avatar", binding.MultipartForm(auth.AvatarForm{}), org.SettingsAvatar)
  347. m.Post("/avatar/delete", org.SettingsDeleteAvatar)
  348. m.Group("/hooks", func() {
  349. m.Get("", org.Webhooks)
  350. m.Post("/delete", org.DeleteWebhook)
  351. m.Get("/:type/new", repo.WebhooksNew)
  352. m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
  353. m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
  354. m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
  355. m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost)
  356. m.Get("/:id", repo.WebHooksEdit)
  357. m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  358. m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost)
  359. m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
  360. m.Post("/discord/:id", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksEditPost)
  361. })
  362. m.Route("/delete", "GET,POST", org.SettingsDelete)
  363. })
  364. }, context.OrgAssignment(true, true))
  365. }, reqSignIn)
  366. // ***** END: Organization *****
  367. // ***** START: Repository *****
  368. m.Group("/repo", func() {
  369. m.Get("/create", repo.Create)
  370. m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  371. m.Get("/migrate", repo.Migrate)
  372. m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
  373. m.Group("/fork", func() {
  374. m.Combo("/:repoid").Get(repo.Fork).
  375. Post(bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
  376. }, context.RepoIDAssignment(), context.UnitTypes(), context.LoadRepoUnits(), context.CheckUnit(models.UnitTypeCode))
  377. }, reqSignIn)
  378. m.Group("/:username/:reponame", func() {
  379. m.Group("/settings", func() {
  380. m.Combo("").Get(repo.Settings).
  381. Post(bindIgnErr(auth.RepoSettingForm{}), repo.SettingsPost)
  382. m.Group("/collaboration", func() {
  383. m.Combo("").Get(repo.Collaboration).Post(repo.CollaborationPost)
  384. m.Post("/access_mode", repo.ChangeCollaborationAccessMode)
  385. m.Post("/delete", repo.DeleteCollaboration)
  386. })
  387. m.Group("/branches", func() {
  388. m.Combo("").Get(repo.ProtectedBranch).Post(repo.ProtectedBranchPost)
  389. m.Combo("/*").Get(repo.SettingsProtectedBranch).
  390. Post(bindIgnErr(auth.ProtectBranchForm{}), repo.SettingsProtectedBranchPost)
  391. }, repo.MustBeNotBare)
  392. m.Group("/hooks", func() {
  393. m.Get("", repo.Webhooks)
  394. m.Post("/delete", repo.DeleteWebhook)
  395. m.Get("/:type/new", repo.WebhooksNew)
  396. m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
  397. m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
  398. m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
  399. m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost)
  400. m.Get("/:id", repo.WebHooksEdit)
  401. m.Post("/:id/test", repo.TestWebhook)
  402. m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  403. m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
  404. m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
  405. m.Post("/discord/:id", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksEditPost)
  406. m.Group("/git", func() {
  407. m.Get("", repo.GitHooks)
  408. m.Combo("/:name").Get(repo.GitHooksEdit).
  409. Post(repo.GitHooksEditPost)
  410. }, context.GitHookService())
  411. })
  412. m.Group("/keys", func() {
  413. m.Combo("").Get(repo.DeployKeys).
  414. Post(bindIgnErr(auth.AddKeyForm{}), repo.DeployKeysPost)
  415. m.Post("/delete", repo.DeleteDeployKey)
  416. })
  417. }, func(ctx *context.Context) {
  418. ctx.Data["PageIsSettings"] = true
  419. })
  420. }, reqSignIn, context.RepoAssignment(), reqRepoAdmin, context.UnitTypes(), context.LoadRepoUnits(), context.RepoRef())
  421. m.Get("/:username/:reponame/action/:action", reqSignIn, context.RepoAssignment(), repo.Action)
  422. m.Group("/:username/:reponame", func() {
  423. m.Group("/issues", func() {
  424. m.Combo("/new").Get(context.RepoRef(), repo.NewIssue).
  425. Post(bindIgnErr(auth.CreateIssueForm{}), repo.NewIssuePost)
  426. }, context.CheckUnit(models.UnitTypeIssues))
  427. // FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
  428. // So they can apply their own enable/disable logic on routers.
  429. m.Group("/issues", func() {
  430. m.Group("/:index", func() {
  431. m.Post("/title", repo.UpdateIssueTitle)
  432. m.Post("/content", repo.UpdateIssueContent)
  433. m.Post("/watch", repo.IssueWatch)
  434. m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment)
  435. m.Group("/times", func() {
  436. m.Post("/add", bindIgnErr(auth.AddTimeManuallyForm{}), repo.AddTimeManually)
  437. m.Group("/stopwatch", func() {
  438. m.Post("/toggle", repo.IssueStopwatch)
  439. m.Post("/cancel", repo.CancelStopwatch)
  440. })
  441. })
  442. })
  443. m.Post("/labels", reqRepoWriter, repo.UpdateIssueLabel)
  444. m.Post("/milestone", reqRepoWriter, repo.UpdateIssueMilestone)
  445. m.Post("/assignee", reqRepoWriter, repo.UpdateIssueAssignee)
  446. m.Post("/status", reqRepoWriter, repo.UpdateIssueStatus)
  447. })
  448. m.Group("/comments/:id", func() {
  449. m.Post("", repo.UpdateCommentContent)
  450. m.Post("/delete", repo.DeleteComment)
  451. }, context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests))
  452. m.Group("/labels", func() {
  453. m.Post("/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
  454. m.Post("/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
  455. m.Post("/delete", repo.DeleteLabel)
  456. m.Post("/initialize", bindIgnErr(auth.InitializeLabelsForm{}), repo.InitializeLabels)
  457. }, reqRepoWriter, context.RepoRef(), context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests))
  458. m.Group("/milestones", func() {
  459. m.Combo("/new").Get(repo.NewMilestone).
  460. Post(bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
  461. m.Get("/:id/edit", repo.EditMilestone)
  462. m.Post("/:id/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.EditMilestonePost)
  463. m.Get("/:id/:action", repo.ChangeMilestonStatus)
  464. m.Post("/delete", repo.DeleteMilestone)
  465. }, reqRepoWriter, context.RepoRef(), context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests))
  466. m.Combo("/compare/*", repo.MustAllowPulls, repo.SetEditorconfigIfExists).
  467. Get(repo.CompareAndPullRequest).
  468. Post(bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)
  469. m.Group("", func() {
  470. m.Combo("/_edit/*").Get(repo.EditFile).
  471. Post(bindIgnErr(auth.EditRepoFileForm{}), repo.EditFilePost)
  472. m.Combo("/_new/*").Get(repo.NewFile).
  473. Post(bindIgnErr(auth.EditRepoFileForm{}), repo.NewFilePost)
  474. m.Post("/_preview/*", bindIgnErr(auth.EditPreviewDiffForm{}), repo.DiffPreviewPost)
  475. m.Combo("/_delete/*").Get(repo.DeleteFile).
  476. Post(bindIgnErr(auth.DeleteRepoFileForm{}), repo.DeleteFilePost)
  477. m.Group("", func() {
  478. m.Combo("/_upload/*").Get(repo.UploadFile).
  479. Post(bindIgnErr(auth.UploadRepoFileForm{}), repo.UploadFilePost)
  480. m.Post("/upload-file", repo.UploadFileToServer)
  481. m.Post("/upload-remove", bindIgnErr(auth.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)
  482. }, func(ctx *context.Context) {
  483. if !setting.Repository.Upload.Enabled {
  484. ctx.Handle(404, "", nil)
  485. return
  486. }
  487. })
  488. }, repo.MustBeNotBare, reqRepoWriter, context.RepoRef(), func(ctx *context.Context) {
  489. if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
  490. ctx.Handle(404, "", nil)
  491. return
  492. }
  493. })
  494. m.Group("/branches", func() {
  495. m.Post("/_new/*", context.RepoRef(), bindIgnErr(auth.NewBranchForm{}), repo.CreateBranch)
  496. m.Post("/delete", repo.DeleteBranchPost)
  497. m.Post("/restore", repo.RestoreBranchPost)
  498. }, reqRepoWriter, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))
  499. }, reqSignIn, context.RepoAssignment(), context.UnitTypes(), context.LoadRepoUnits())
  500. // Releases
  501. m.Group("/:username/:reponame", func() {
  502. m.Group("/releases", func() {
  503. m.Get("/", repo.MustBeNotBare, repo.Releases)
  504. }, repo.MustBeNotBare, context.RepoRef())
  505. m.Group("/releases", func() {
  506. m.Get("/new", repo.NewRelease)
  507. m.Post("/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
  508. m.Post("/delete", repo.DeleteRelease)
  509. }, reqSignIn, repo.MustBeNotBare, reqRepoWriter, context.RepoRef())
  510. m.Group("/releases", func() {
  511. m.Get("/edit/*", repo.EditRelease)
  512. m.Post("/edit/*", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost)
  513. }, reqSignIn, repo.MustBeNotBare, reqRepoWriter, func(ctx *context.Context) {
  514. var err error
  515. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  516. if err != nil {
  517. ctx.Handle(500, "GetBranchCommit", err)
  518. return
  519. }
  520. ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
  521. if err != nil {
  522. ctx.Handle(500, "GetCommitsCount", err)
  523. return
  524. }
  525. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  526. })
  527. }, context.RepoAssignment(), context.UnitTypes(), context.LoadRepoUnits(), context.CheckUnit(models.UnitTypeReleases))
  528. m.Group("/:username/:reponame", func() {
  529. m.Group("", func() {
  530. m.Get("/^:type(issues|pulls)$", repo.RetrieveLabels, repo.Issues)
  531. m.Get("/^:type(issues|pulls)$/:index", repo.ViewIssue)
  532. m.Get("/labels/", context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests), repo.RetrieveLabels, repo.Labels)
  533. m.Get("/milestones", context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests), repo.Milestones)
  534. }, context.RepoRef())
  535. m.Group("/wiki", func() {
  536. m.Get("/?:page", repo.Wiki)
  537. m.Get("/_pages", repo.WikiPages)
  538. m.Group("", func() {
  539. m.Combo("/_new").Get(repo.NewWiki).
  540. Post(bindIgnErr(auth.NewWikiForm{}), repo.NewWikiPost)
  541. m.Combo("/:page/_edit").Get(repo.EditWiki).
  542. Post(bindIgnErr(auth.NewWikiForm{}), repo.EditWikiPost)
  543. m.Post("/:page/delete", repo.DeleteWikiPagePost)
  544. }, reqSignIn, reqRepoWriter)
  545. }, repo.MustEnableWiki, context.RepoRef())
  546. m.Group("/wiki", func() {
  547. m.Get("/raw/*", repo.WikiRaw)
  548. m.Get("/*", repo.WikiRaw)
  549. }, repo.MustEnableWiki)
  550. m.Group("/activity", func() {
  551. m.Get("", repo.Activity)
  552. m.Get("/:period", repo.Activity)
  553. }, context.RepoRef(), repo.MustBeNotBare, context.CheckAnyUnit(models.UnitTypePullRequests, models.UnitTypeIssues, models.UnitTypeReleases))
  554. m.Get("/archive/*", repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode), repo.Download)
  555. m.Group("/branches", func() {
  556. m.Get("", repo.Branches)
  557. }, repo.MustBeNotBare, context.RepoRef(), context.CheckUnit(models.UnitTypeCode))
  558. m.Group("/pulls/:index", func() {
  559. m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
  560. m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles)
  561. m.Post("/merge", reqRepoWriter, repo.MergePullRequest)
  562. m.Post("/cleanup", context.RepoRef(), repo.CleanUpPullRequest)
  563. }, repo.MustAllowPulls)
  564. m.Group("", func() {
  565. m.Get("/raw/*", repo.SingleDownload)
  566. m.Get("/commits/*", repo.RefCommits)
  567. m.Get("/graph", repo.Graph)
  568. m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
  569. }, repo.MustBeNotBare, context.RepoRef(), context.CheckUnit(models.UnitTypeCode))
  570. m.Group("", func() {
  571. m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home)
  572. m.Get("/forks", repo.Forks)
  573. }, context.RepoRef(), context.CheckUnit(models.UnitTypeCode))
  574. m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)",
  575. repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode), repo.RawDiff)
  576. m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists,
  577. repo.SetDiffViewStyle, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode), repo.CompareDiff)
  578. }, ignSignIn, context.RepoAssignment(), context.UnitTypes(), context.LoadRepoUnits())
  579. m.Group("/:username/:reponame", func() {
  580. m.Get("/stars", repo.Stars)
  581. m.Get("/watchers", repo.Watchers)
  582. m.Get("/search", context.CheckUnit(models.UnitTypeCode), repo.Search)
  583. }, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes(), context.LoadRepoUnits())
  584. m.Group("/:username", func() {
  585. m.Group("/:reponame", func() {
  586. m.Get("", repo.SetEditorconfigIfExists, repo.Home)
  587. m.Get("\\.git$", repo.SetEditorconfigIfExists, repo.Home)
  588. }, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes(), context.LoadRepoUnits())
  589. m.Group("/:reponame", func() {
  590. m.Group("/info/lfs", func() {
  591. m.Post("/objects/batch", lfs.BatchHandler)
  592. m.Get("/objects/:oid/:filename", lfs.ObjectOidHandler)
  593. m.Any("/objects/:oid", lfs.ObjectOidHandler)
  594. m.Post("/objects", lfs.PostHandler)
  595. m.Any("/*", func(ctx *context.Context) {
  596. ctx.Handle(404, "", nil)
  597. })
  598. }, ignSignInAndCsrf)
  599. m.Any("/*", ignSignInAndCsrf, repo.HTTP)
  600. m.Head("/tasks/trigger", repo.TriggerTask)
  601. })
  602. })
  603. // ***** END: Repository *****
  604. m.Group("/notifications", func() {
  605. m.Get("", user.Notifications)
  606. m.Post("/status", user.NotificationStatusPost)
  607. }, reqSignIn)
  608. m.Group("/api", func() {
  609. apiv1.RegisterRoutes(m)
  610. }, ignSignIn)
  611. m.Group("/api/internal", func() {
  612. // package name internal is ideal but Golang is not allowed, so we use private as package name.
  613. private.RegisterRoutes(m)
  614. })
  615. // robots.txt
  616. m.Get("/robots.txt", func(ctx *context.Context) {
  617. if setting.HasRobotsTxt {
  618. ctx.ServeFileContent(path.Join(setting.CustomPath, "robots.txt"))
  619. } else {
  620. ctx.Handle(404, "", nil)
  621. }
  622. })
  623. // Not found handler.
  624. m.NotFound(routers.NotFound)
  625. }