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.

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