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.

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