You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 main
  5. import (
  6. "fmt"
  7. "html/template"
  8. "net/http"
  9. "strings"
  10. "github.com/codegangsta/cli"
  11. "github.com/codegangsta/martini"
  12. "github.com/gogits/binding"
  13. "github.com/gogits/gogs/models"
  14. "github.com/gogits/gogs/modules/auth"
  15. "github.com/gogits/gogs/modules/avatar"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/mailer"
  19. "github.com/gogits/gogs/modules/middleware"
  20. "github.com/gogits/gogs/routers"
  21. "github.com/gogits/gogs/routers/admin"
  22. "github.com/gogits/gogs/routers/dev"
  23. "github.com/gogits/gogs/routers/repo"
  24. "github.com/gogits/gogs/routers/user"
  25. )
  26. var CmdWeb = cli.Command{
  27. Name: "web",
  28. Usage: "just run",
  29. Description: `
  30. gogs web`,
  31. Action: runWeb,
  32. Flags: []cli.Flag{},
  33. }
  34. // globalInit is for global configuration reload-able.
  35. func globalInit() {
  36. base.NewConfigContext()
  37. mailer.NewMailerContext()
  38. models.LoadModelsConfig()
  39. models.LoadRepoConfig()
  40. models.NewRepoContext()
  41. models.NewEngine()
  42. }
  43. // Check run mode(Default of martini is Dev).
  44. func checkRunMode() {
  45. switch base.Cfg.MustValue("", "RUN_MODE") {
  46. case "prod":
  47. martini.Env = martini.Prod
  48. case "test":
  49. martini.Env = martini.Test
  50. }
  51. log.Info("Run Mode: %s", strings.Title(martini.Env))
  52. }
  53. func newMartini() *martini.ClassicMartini {
  54. r := martini.NewRouter()
  55. m := martini.New()
  56. m.Use(middleware.Logger())
  57. m.Use(martini.Recovery())
  58. m.Use(martini.Static("public"))
  59. m.MapTo(r, (*martini.Routes)(nil))
  60. m.Action(r.Handle)
  61. return &martini.ClassicMartini{m, r}
  62. }
  63. func runWeb(*cli.Context) {
  64. globalInit()
  65. base.NewServices()
  66. checkRunMode()
  67. log.Info("%s %s", base.AppName, base.AppVer)
  68. m := newMartini()
  69. // Middlewares.
  70. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  71. m.Use(middleware.InitContext())
  72. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  73. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  74. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  75. // Routers.
  76. m.Get("/", ignSignIn, routers.Home)
  77. m.Get("/issues", reqSignIn, user.Issues)
  78. m.Get("/pulls", reqSignIn, user.Pulls)
  79. m.Get("/stars", reqSignIn, user.Stars)
  80. m.Get("/help", routers.Help)
  81. avatarCache := avatar.HttpHandler("public/img/avatar/", "public/img/avatar_default.jpg")
  82. m.Get("/avatar/:hash", avatarCache.ServeHTTP)
  83. m.Group("/user", func(r martini.Router) {
  84. r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  85. r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  86. }, reqSignOut)
  87. m.Group("/user", func(r martini.Router) {
  88. r.Any("/logout", user.SignOut)
  89. r.Any("/delete", user.Delete)
  90. r.Any("/setting", binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  91. }, reqSignIn)
  92. m.Group("/user", func(r martini.Router) {
  93. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  94. r.Get("/activate", user.Activate)
  95. })
  96. m.Group("/user/setting", func(r martini.Router) {
  97. r.Any("/password", binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  98. r.Any("/ssh", binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  99. r.Any("/notification", user.SettingNotification)
  100. r.Any("/security", user.SettingSecurity)
  101. }, reqSignIn)
  102. m.Get("/user/:username", ignSignIn, user.Profile)
  103. m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  104. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  105. m.Get("/admin", adminReq, admin.Dashboard)
  106. m.Group("/admin", func(r martini.Router) {
  107. r.Get("/users", admin.Users)
  108. r.Get("/repos", admin.Repositories)
  109. r.Get("/config", admin.Config)
  110. }, adminReq)
  111. m.Group("/admin/users", func(r martini.Router) {
  112. r.Any("/new", binding.BindIgnErr(auth.RegisterForm{}), admin.NewUser)
  113. r.Any("/:userid", binding.BindIgnErr(auth.AdminEditUserForm{}), admin.EditUser)
  114. r.Any("/:userid/delete", admin.DeleteUser)
  115. }, adminReq)
  116. m.Group("/:username/:reponame", func(r martini.Router) {
  117. r.Post("/settings", repo.SettingPost)
  118. r.Get("/settings", repo.Setting)
  119. r.Get("/action/:action", repo.Action)
  120. }, reqSignIn, middleware.RepoAssignment(true))
  121. m.Group("/:username/:reponame", func(r martini.Router) {
  122. r.Get("/commits/:branchname", repo.Commits)
  123. r.Get("/issues", repo.Issues)
  124. r.Any("/issues/new", binding.BindIgnErr(auth.CreateIssueForm{}), repo.CreateIssue)
  125. r.Get("/issues/:issueid", repo.ViewIssue)
  126. r.Get("/pulls", repo.Pulls)
  127. r.Get("/branches", repo.Branches)
  128. r.Get("/src/:branchname", repo.Single)
  129. r.Get("/src/:branchname/**", repo.Single)
  130. r.Get("/commits/:branchname", repo.Commits)
  131. r.Get("/commits/:branchname", repo.Commits)
  132. }, ignSignIn, middleware.RepoAssignment(true))
  133. // TODO: implement single commit page
  134. // m.Get("/:username/:reponame/commit/:commitid/**", ignSignIn, middleware.RepoAssignment(true), repo.Single)
  135. // m.Get("/:username/:reponame/commit/:commitid", ignSignIn, middleware.RepoAssignment(true), repo.Single)
  136. m.Group("/:username", func(r martini.Router) {
  137. r.Get("/:reponame", middleware.RepoAssignment(true), repo.Single)
  138. r.Any("/:reponame/**", repo.Http)
  139. }, ignSignIn)
  140. if martini.Env == martini.Dev {
  141. m.Get("/template/**", dev.TemplatePreview)
  142. }
  143. // Not found handler.
  144. m.NotFound(routers.NotFound)
  145. listenAddr := fmt.Sprintf("%s:%s",
  146. base.Cfg.MustValue("server", "HTTP_ADDR"),
  147. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  148. log.Info("Listen: %s", listenAddr)
  149. if err := http.ListenAndServe(listenAddr, m); err != nil {
  150. log.Critical(err.Error())
  151. }
  152. }