home.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2014 The Gogs 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 routers
  5. import (
  6. "fmt"
  7. "github.com/Unknwon/paginater"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/middleware"
  11. "github.com/gogits/gogs/modules/setting"
  12. "github.com/gogits/gogs/routers/user"
  13. )
  14. const (
  15. HOME base.TplName = "home"
  16. EXPLORE_REPOS base.TplName = "explore/repos"
  17. )
  18. func Home(ctx *middleware.Context) {
  19. if ctx.IsSigned {
  20. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  21. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  22. ctx.HTML(200, user.ACTIVATE)
  23. } else {
  24. user.Dashboard(ctx)
  25. }
  26. return
  27. }
  28. // Check auto-login.
  29. uname := ctx.GetCookie(setting.CookieUserName)
  30. if len(uname) != 0 {
  31. ctx.Redirect(setting.AppSubUrl + "/user/login")
  32. return
  33. }
  34. if setting.OauthService != nil {
  35. ctx.Data["OauthEnabled"] = true
  36. ctx.Data["OauthService"] = setting.OauthService
  37. }
  38. ctx.Data["PageIsHome"] = true
  39. ctx.HTML(200, HOME)
  40. }
  41. func Explore(ctx *middleware.Context) {
  42. ctx.Data["Title"] = ctx.Tr("explore")
  43. ctx.Data["PageIsExplore"] = true
  44. ctx.Data["PageIsExploreRepositories"] = true
  45. page := ctx.QueryInt("page")
  46. if page <= 1 {
  47. page = 1
  48. }
  49. ctx.Data["Page"] = paginater.New(int(models.CountPublicRepositories()), setting.ExplorePagingNum, page, 5)
  50. repos, err := models.GetRecentUpdatedRepositories(page)
  51. if err != nil {
  52. ctx.Handle(500, "GetRecentUpdatedRepositories", err)
  53. return
  54. }
  55. for _, repo := range repos {
  56. if err = repo.GetOwner(); err != nil {
  57. ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
  58. return
  59. }
  60. }
  61. ctx.Data["Repos"] = repos
  62. ctx.HTML(200, EXPLORE_REPOS)
  63. }
  64. func NotFound(ctx *middleware.Context) {
  65. ctx.Data["Title"] = "Page Not Found"
  66. ctx.Handle(404, "home.NotFound", nil)
  67. }