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

routes.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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.CacheAdapter,
  91. AdapterConfig: setting.CacheConn,
  92. Interval: setting.CacheInterval,
  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. m.Use(user.GetNotificationCount)
  125. // FIXME: not all routes need go through same middlewares.
  126. // Especially some AJAX requests, we can reduce middleware number to improve performance.
  127. // Routers.
  128. // for health check
  129. m.Head("/", func() string {
  130. return ""
  131. })
  132. m.Get("/", ignSignIn, routers.Home)
  133. m.Group("/explore", func() {
  134. m.Get("", func(ctx *context.Context) {
  135. ctx.Redirect(setting.AppSubURL + "/explore/repos")
  136. })
  137. m.Get("/repos", routers.ExploreRepos)
  138. m.Get("/users", routers.ExploreUsers)
  139. m.Get("/organizations", routers.ExploreOrganizations)
  140. }, ignSignIn)
  141. m.Combo("/install", routers.InstallInit).Get(routers.Install).
  142. Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  143. m.Get("/^:type(issues|pulls)$", reqSignIn, user.Issues)
  144. // ***** START: User *****
  145. m.Group("/user", func() {
  146. m.Get("/login", user.SignIn)
  147. m.Post("/login", bindIgnErr(auth.SignInForm{}), user.SignInPost)
  148. if setting.Service.EnableOpenIDSignIn {
  149. m.Combo("/login/openid").
  150. Get(user.SignInOpenID).
  151. Post(bindIgnErr(auth.SignInOpenIDForm{}), user.SignInOpenIDPost)
  152. m.Group("/openid", func() {
  153. m.Combo("/connect").
  154. Get(user.ConnectOpenID).
  155. Post(bindIgnErr(auth.ConnectOpenIDForm{}), user.ConnectOpenIDPost)
  156. m.Combo("/routes").
  157. Get(user.RegisterOpenID).
  158. Post(bindIgnErr(auth.SignUpOpenIDForm{}), user.RegisterOpenIDPost)
  159. })
  160. }
  161. m.Get("/sign_up", user.SignUp)
  162. m.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  163. m.Get("/reset_password", user.ResetPasswd)
  164. m.Post("/reset_password", user.ResetPasswdPost)
  165. m.Group("/oauth2", func() {
  166. m.Get("/:provider", user.SignInOAuth)
  167. m.Get("/:provider/callback", user.SignInOAuthCallback)
  168. })
  169. m.Get("/link_account", user.LinkAccount)
  170. m.Post("/link_account_signin", bindIgnErr(auth.SignInForm{}), user.LinkAccountPostSignIn)
  171. m.Post("/link_account_signup", bindIgnErr(auth.RegisterForm{}), user.LinkAccountPostRegister)
  172. m.Group("/two_factor", func() {
  173. m.Get("", user.TwoFactor)
  174. m.Post("", bindIgnErr(auth.TwoFactorAuthForm{}), user.TwoFactorPost)
  175. m.Get("/scratch", user.TwoFactorScratch)
  176. m.Post("/scratch", bindIgnErr(auth.TwoFactorScratchAuthForm{}), user.TwoFactorScratchPost)
  177. })
  178. }, reqSignOut)
  179. m.Group("/user/settings", func() {
  180. m.Get("", user.Settings)
  181. m.Post("", bindIgnErr(auth.UpdateProfileForm{}), user.SettingsPost)
  182. m.Combo("/avatar").Get(user.SettingsAvatar).
  183. Post(binding.MultipartForm(auth.AvatarForm{}), user.SettingsAvatarPost)
  184. m.Post("/avatar/delete", user.SettingsDeleteAvatar)
  185. m.Combo("/email").Get(user.SettingsEmails).
  186. Post(bindIgnErr(auth.AddEmailForm{}), user.SettingsEmailPost)
  187. m.Post("/email/delete", user.DeleteEmail)
  188. m.Get("/password", user.SettingsPassword)
  189. m.Post("/password", bindIgnErr(auth.ChangePasswordForm{}), user.SettingsPasswordPost)
  190. if setting.Service.EnableOpenIDSignIn {
  191. m.Group("/openid", func() {
  192. m.Combo("").Get(user.SettingsOpenID).
  193. Post(bindIgnErr(auth.AddOpenIDForm{}), user.SettingsOpenIDPost)
  194. m.Post("/delete", user.DeleteOpenID)
  195. m.Post("/toggle_visibility", user.ToggleOpenIDVisibility)
  196. })
  197. }
  198. m.Combo("/ssh").Get(user.SettingsSSHKeys).
  199. Post(bindIgnErr(auth.AddSSHKeyForm{}), user.SettingsSSHKeysPost)
  200. m.Post("/ssh/delete", user.DeleteSSHKey)
  201. m.Combo("/applications").Get(user.SettingsApplications).
  202. Post(bindIgnErr(auth.NewAccessTokenForm{}), user.SettingsApplicationsPost)
  203. m.Post("/applications/delete", user.SettingsDeleteApplication)
  204. m.Route("/delete", "GET,POST", user.SettingsDelete)
  205. m.Combo("/account_link").Get(user.SettingsAccountLinks).Post(user.SettingsDeleteAccountLink)
  206. m.Group("/two_factor", func() {
  207. m.Get("", user.SettingsTwoFactor)
  208. m.Post("/regenerate_scratch", user.SettingsTwoFactorRegenerateScratch)
  209. m.Post("/disable", user.SettingsTwoFactorDisable)
  210. m.Get("/enroll", user.SettingsTwoFactorEnroll)
  211. m.Post("/enroll", bindIgnErr(auth.TwoFactorAuthForm{}), user.SettingsTwoFactorEnrollPost)
  212. })
  213. }, reqSignIn, func(ctx *context.Context) {
  214. ctx.Data["PageIsUserSettings"] = true
  215. })
  216. m.Group("/user", func() {
  217. // r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  218. m.Any("/activate", user.Activate)
  219. m.Any("/activate_email", user.ActivateEmail)
  220. m.Get("/email2user", user.Email2User)
  221. m.Get("/forgot_password", user.ForgotPasswd)
  222. m.Post("/forgot_password", user.ForgotPasswdPost)
  223. m.Get("/logout", user.SignOut)
  224. })
  225. // ***** END: User *****
  226. adminReq := context.Toggle(&context.ToggleOptions{SignInRequired: true, AdminRequired: true})
  227. // ***** START: Admin *****
  228. m.Group("/admin", func() {
  229. m.Get("", adminReq, admin.Dashboard)
  230. m.Get("/config", admin.Config)
  231. m.Post("/config/test_mail", admin.SendTestMail)
  232. m.Get("/monitor", admin.Monitor)
  233. m.Group("/users", func() {
  234. m.Get("", admin.Users)
  235. m.Combo("/new").Get(admin.NewUser).Post(bindIgnErr(auth.AdminCreateUserForm{}), admin.NewUserPost)
  236. m.Combo("/:userid").Get(admin.EditUser).Post(bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  237. m.Post("/:userid/delete", admin.DeleteUser)
  238. })
  239. m.Group("/orgs", func() {
  240. m.Get("", admin.Organizations)
  241. })
  242. m.Group("/repos", func() {
  243. m.Get("", admin.Repos)
  244. m.Post("/delete", admin.DeleteRepo)
  245. })
  246. m.Group("/auths", func() {
  247. m.Get("", admin.Authentications)
  248. m.Combo("/new").Get(admin.NewAuthSource).Post(bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)
  249. m.Combo("/:authid").Get(admin.EditAuthSource).
  250. Post(bindIgnErr(auth.AuthenticationForm{}), admin.EditAuthSourcePost)
  251. m.Post("/:authid/delete", admin.DeleteAuthSource)
  252. })
  253. m.Group("/notices", func() {
  254. m.Get("", admin.Notices)
  255. m.Post("/delete", admin.DeleteNotices)
  256. m.Get("/empty", admin.EmptyNotices)
  257. })
  258. }, adminReq)
  259. // ***** END: Admin *****
  260. m.Group("", func() {
  261. m.Group("/:username", func() {
  262. m.Get("", user.Profile)
  263. m.Get("/followers", user.Followers)
  264. m.Get("/following", user.Following)
  265. })
  266. m.Get("/attachments/:uuid", func(ctx *context.Context) {
  267. attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
  268. if err != nil {
  269. if models.IsErrAttachmentNotExist(err) {
  270. ctx.Error(404)
  271. } else {
  272. ctx.Handle(500, "GetAttachmentByUUID", err)
  273. }
  274. return
  275. }
  276. fr, err := os.Open(attach.LocalPath())
  277. if err != nil {
  278. ctx.Handle(500, "Open", err)
  279. return
  280. }
  281. defer fr.Close()
  282. if err := attach.IncreaseDownloadCount(); err != nil {
  283. ctx.Handle(500, "Update", err)
  284. return
  285. }
  286. if err = repo.ServeData(ctx, attach.Name, fr); err != nil {
  287. ctx.Handle(500, "ServeData", err)
  288. return
  289. }
  290. })
  291. m.Post("/attachments", repo.UploadAttachment)
  292. }, ignSignIn)
  293. m.Group("/:username", func() {
  294. m.Get("/action/:action", user.Action)
  295. }, reqSignIn)
  296. if macaron.Env == macaron.DEV {
  297. m.Get("/template/*", dev.TemplatePreview)
  298. }
  299. reqRepoAdmin := context.RequireRepoAdmin()
  300. reqRepoWriter := context.RequireRepoWriter()
  301. // ***** START: Organization *****
  302. m.Group("/org", func() {
  303. m.Group("", func() {
  304. m.Get("/create", org.Create)
  305. m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
  306. }, func(ctx *context.Context) {
  307. if !ctx.User.CanCreateOrganization() {
  308. ctx.NotFound()
  309. }
  310. })
  311. m.Group("/:org", func() {
  312. m.Get("/dashboard", user.Dashboard)
  313. m.Get("/^:type(issues|pulls)$", user.Issues)
  314. m.Get("/members", org.Members)
  315. m.Get("/members/action/:action", org.MembersAction)
  316. m.Get("/teams", org.Teams)
  317. }, context.OrgAssignment(true))
  318. m.Group("/:org", func() {
  319. m.Get("/teams/:team", org.TeamMembers)
  320. m.Get("/teams/:team/repositories", org.TeamRepositories)
  321. m.Route("/teams/:team/action/:action", "GET,POST", org.TeamsAction)
  322. m.Route("/teams/:team/action/repo/:action", "GET,POST", org.TeamsRepoAction)
  323. }, context.OrgAssignment(true, false, true))
  324. m.Group("/:org", func() {
  325. m.Get("/teams/new", org.NewTeam)
  326. m.Post("/teams/new", bindIgnErr(auth.CreateTeamForm{}), org.NewTeamPost)
  327. m.Get("/teams/:team/edit", org.EditTeam)
  328. m.Post("/teams/:team/edit", bindIgnErr(auth.CreateTeamForm{}), org.EditTeamPost)
  329. m.Post("/teams/:team/delete", org.DeleteTeam)
  330. m.Group("/settings", func() {
  331. m.Combo("").Get(org.Settings).
  332. Post(bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost)
  333. m.Post("/avatar", binding.MultipartForm(auth.AvatarForm{}), org.SettingsAvatar)
  334. m.Post("/avatar/delete", org.SettingsDeleteAvatar)
  335. m.Group("/hooks", func() {
  336. m.Get("", org.Webhooks)
  337. m.Post("/delete", org.DeleteWebhook)
  338. m.Get("/:type/new", repo.WebhooksNew)
  339. m.Post("/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
  340. m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
  341. m.Get("/:id", repo.WebHooksEdit)
  342. m.Post("/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  343. m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
  344. })
  345. m.Route("/delete", "GET,POST", org.SettingsDelete)
  346. })
  347. m.Route("/invitations/new", "GET,POST", org.Invitation)
  348. }, context.OrgAssignment(true, true))
  349. }, reqSignIn)
  350. // ***** END: Organization *****
  351. // ***** START: Repository *****
  352. m.Group("/repo", func() {
  353. m.Get("/create", repo.Create)
  354. m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  355. m.Get("/migrate", repo.Migrate)
  356. m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
  357. m.Combo("/fork/:repoid").Get(repo.Fork).
  358. Post(bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
  359. }, reqSignIn)
  360. m.Group("/:username/:reponame", func() {
  361. m.Group("/settings", func() {
  362. m.Combo("").Get(repo.Settings).
  363. Post(bindIgnErr(auth.RepoSettingForm{}), repo.SettingsPost)
  364. m.Group("/collaboration", func() {
  365. m.Combo("").Get(repo.Collaboration).Post(repo.CollaborationPost)
  366. m.Post("/access_mode", repo.ChangeCollaborationAccessMode)
  367. m.Post("/delete", repo.DeleteCollaboration)
  368. })
  369. m.Group("/branches", func() {
  370. m.Combo("").Get(repo.ProtectedBranch).Post(repo.ProtectedBranchPost)
  371. m.Post("/can_push", repo.ChangeProtectedBranch)
  372. m.Post("/delete", repo.DeleteProtectedBranch)
  373. }, repo.MustBeNotBare)
  374. m.Group("/hooks", func() {
  375. m.Get("", repo.Webhooks)
  376. m.Post("/delete", repo.DeleteWebhook)
  377. m.Get("/:type/new", repo.WebhooksNew)
  378. m.Post("/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
  379. m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
  380. m.Get("/:id", repo.WebHooksEdit)
  381. m.Post("/:id/test", repo.TestWebhook)
  382. m.Post("/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  383. m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
  384. m.Group("/git", func() {
  385. m.Get("", repo.GitHooks)
  386. m.Combo("/:name").Get(repo.GitHooksEdit).
  387. Post(repo.GitHooksEditPost)
  388. }, context.GitHookService())
  389. })
  390. m.Group("/keys", func() {
  391. m.Combo("").Get(repo.DeployKeys).
  392. Post(bindIgnErr(auth.AddSSHKeyForm{}), repo.DeployKeysPost)
  393. m.Post("/delete", repo.DeleteDeployKey)
  394. })
  395. }, func(ctx *context.Context) {
  396. ctx.Data["PageIsSettings"] = true
  397. }, context.UnitTypes())
  398. }, reqSignIn, context.RepoAssignment(), reqRepoAdmin, context.RepoRef())
  399. m.Get("/:username/:reponame/action/:action", reqSignIn, context.RepoAssignment(), repo.Action)
  400. m.Group("/:username/:reponame", func() {
  401. // FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
  402. // So they can apply their own enable/disable logic on routers.
  403. m.Group("/issues", func() {
  404. m.Combo("/new", repo.MustEnableIssues).Get(context.RepoRef(), repo.NewIssue).
  405. Post(bindIgnErr(auth.CreateIssueForm{}), repo.NewIssuePost)
  406. m.Group("/:index", func() {
  407. m.Post("/title", repo.UpdateIssueTitle)
  408. m.Post("/content", repo.UpdateIssueContent)
  409. m.Post("/watch", repo.IssueWatch)
  410. m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment)
  411. })
  412. m.Post("/labels", repo.UpdateIssueLabel, reqRepoWriter)
  413. m.Post("/milestone", repo.UpdateIssueMilestone, reqRepoWriter)
  414. m.Post("/assignee", repo.UpdateIssueAssignee, reqRepoWriter)
  415. m.Post("/status", repo.UpdateIssueStatus, reqRepoWriter)
  416. })
  417. m.Group("/comments/:id", func() {
  418. m.Post("", repo.UpdateCommentContent)
  419. m.Post("/delete", repo.DeleteComment)
  420. })
  421. m.Group("/labels", func() {
  422. m.Post("/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
  423. m.Post("/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
  424. m.Post("/delete", repo.DeleteLabel)
  425. m.Post("/initialize", bindIgnErr(auth.InitializeLabelsForm{}), repo.InitializeLabels)
  426. }, reqRepoWriter, context.RepoRef())
  427. m.Group("/milestones", func() {
  428. m.Combo("/new").Get(repo.NewMilestone).
  429. Post(bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
  430. m.Get("/:id/edit", repo.EditMilestone)
  431. m.Post("/:id/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.EditMilestonePost)
  432. m.Get("/:id/:action", repo.ChangeMilestonStatus)
  433. m.Post("/delete", repo.DeleteMilestone)
  434. }, reqRepoWriter, context.RepoRef())
  435. m.Group("/releases", func() {
  436. m.Get("/new", repo.NewRelease)
  437. m.Post("/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
  438. m.Post("/delete", repo.DeleteRelease)
  439. }, repo.MustBeNotBare, reqRepoWriter, context.RepoRef())
  440. m.Group("/releases", func() {
  441. m.Get("/edit/*", repo.EditRelease)
  442. m.Post("/edit/*", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost)
  443. }, repo.MustBeNotBare, reqRepoWriter, func(ctx *context.Context) {
  444. var err error
  445. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  446. if err != nil {
  447. ctx.Handle(500, "GetBranchCommit", err)
  448. return
  449. }
  450. ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
  451. if err != nil {
  452. ctx.Handle(500, "CommitsCount", err)
  453. return
  454. }
  455. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  456. })
  457. m.Combo("/compare/*", repo.MustAllowPulls, repo.SetEditorconfigIfExists).
  458. Get(repo.CompareAndPullRequest).
  459. Post(bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)
  460. m.Group("", func() {
  461. m.Combo("/_edit/*").Get(repo.EditFile).
  462. Post(bindIgnErr(auth.EditRepoFileForm{}), repo.EditFilePost)
  463. m.Combo("/_new/*").Get(repo.NewFile).
  464. Post(bindIgnErr(auth.EditRepoFileForm{}), repo.NewFilePost)
  465. m.Post("/_preview/*", bindIgnErr(auth.EditPreviewDiffForm{}), repo.DiffPreviewPost)
  466. m.Combo("/_delete/*").Get(repo.DeleteFile).
  467. Post(bindIgnErr(auth.DeleteRepoFileForm{}), repo.DeleteFilePost)
  468. m.Group("", func() {
  469. m.Combo("/_upload/*").Get(repo.UploadFile).
  470. Post(bindIgnErr(auth.UploadRepoFileForm{}), repo.UploadFilePost)
  471. m.Post("/upload-file", repo.UploadFileToServer)
  472. m.Post("/upload-remove", bindIgnErr(auth.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)
  473. }, func(ctx *context.Context) {
  474. if !setting.Repository.Upload.Enabled {
  475. ctx.Handle(404, "", nil)
  476. return
  477. }
  478. })
  479. }, repo.MustBeNotBare, reqRepoWriter, context.RepoRef(), func(ctx *context.Context) {
  480. if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
  481. ctx.Handle(404, "", nil)
  482. return
  483. }
  484. })
  485. }, reqSignIn, context.RepoAssignment(), context.UnitTypes())
  486. m.Group("/:username/:reponame", func() {
  487. m.Group("", func() {
  488. m.Get("/releases", repo.MustBeNotBare, repo.Releases)
  489. m.Get("/^:type(issues|pulls)$", repo.RetrieveLabels, repo.Issues)
  490. m.Get("/^:type(issues|pulls)$/:index", repo.ViewIssue)
  491. m.Get("/labels/", repo.RetrieveLabels, repo.Labels)
  492. m.Get("/milestones", repo.Milestones)
  493. }, context.RepoRef())
  494. // m.Get("/branches", repo.Branches)
  495. m.Post("/branches/:name/delete", reqSignIn, reqRepoWriter, repo.MustBeNotBare, repo.DeleteBranchPost)
  496. m.Group("/wiki", func() {
  497. m.Get("/?:page", repo.Wiki)
  498. m.Get("/_pages", repo.WikiPages)
  499. m.Group("", func() {
  500. m.Combo("/_new").Get(repo.NewWiki).
  501. Post(bindIgnErr(auth.NewWikiForm{}), repo.NewWikiPost)
  502. m.Combo("/:page/_edit").Get(repo.EditWiki).
  503. Post(bindIgnErr(auth.NewWikiForm{}), repo.EditWikiPost)
  504. m.Post("/:page/delete", repo.DeleteWikiPagePost)
  505. }, reqSignIn, reqRepoWriter)
  506. }, repo.MustEnableWiki, context.RepoRef())
  507. m.Group("/wiki", func() {
  508. m.Get("/raw/*", repo.WikiRaw)
  509. m.Get("/*", repo.WikiRaw)
  510. }, repo.MustEnableWiki)
  511. m.Get("/archive/*", repo.MustBeNotBare, repo.Download)
  512. m.Group("/pulls/:index", func() {
  513. m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
  514. m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles)
  515. m.Post("/merge", reqRepoWriter, repo.MergePullRequest)
  516. }, repo.MustAllowPulls)
  517. m.Group("", func() {
  518. m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home)
  519. m.Get("/raw/*", repo.SingleDownload)
  520. m.Get("/commits/*", repo.RefCommits)
  521. m.Get("/graph", repo.Graph)
  522. m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
  523. m.Get("/forks", repo.Forks)
  524. }, context.RepoRef())
  525. m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.MustBeNotBare, repo.RawDiff)
  526. m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.MustBeNotBare, repo.CompareDiff)
  527. }, ignSignIn, context.RepoAssignment(), context.UnitTypes())
  528. m.Group("/:username/:reponame", func() {
  529. m.Get("/stars", repo.Stars)
  530. m.Get("/watchers", repo.Watchers)
  531. }, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes())
  532. m.Group("/:username", func() {
  533. m.Group("/:reponame", func() {
  534. m.Get("", repo.SetEditorconfigIfExists, repo.Home)
  535. m.Get("\\.git$", repo.SetEditorconfigIfExists, repo.Home)
  536. }, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes())
  537. m.Group("/:reponame", func() {
  538. m.Group("/info/lfs", func() {
  539. m.Post("/objects/batch", lfs.BatchHandler)
  540. m.Get("/objects/:oid/:filename", lfs.ObjectOidHandler)
  541. m.Any("/objects/:oid", lfs.ObjectOidHandler)
  542. m.Post("/objects", lfs.PostHandler)
  543. m.Any("/*", func(ctx *context.Context) {
  544. ctx.Handle(404, "", nil)
  545. })
  546. }, ignSignInAndCsrf)
  547. m.Any("/*", ignSignInAndCsrf, repo.HTTP)
  548. m.Head("/tasks/trigger", repo.TriggerTask)
  549. })
  550. })
  551. // ***** END: Repository *****
  552. m.Group("/notifications", func() {
  553. m.Get("", user.Notifications)
  554. m.Post("/status", user.NotificationStatusPost)
  555. }, reqSignIn)
  556. m.Group("/api", func() {
  557. apiv1.RegisterRoutes(m)
  558. }, ignSignIn)
  559. m.Group("/api/internal", func() {
  560. // package name internal is ideal but Golang is not allowed, so we use private as package name.
  561. private.RegisterRoutes(m)
  562. })
  563. // robots.txt
  564. m.Get("/robots.txt", func(ctx *context.Context) {
  565. if setting.HasRobotsTxt {
  566. ctx.ServeFileContent(path.Join(setting.CustomPath, "robots.txt"))
  567. } else {
  568. ctx.Error(404)
  569. }
  570. })
  571. // Not found handler.
  572. m.NotFound(routers.NotFound)
  573. }