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.

196 lines
6.6 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
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. "github.com/codegangsta/cli"
  10. "github.com/go-martini/martini"
  11. qlog "github.com/qiniu/log"
  12. "github.com/gogits/binding"
  13. "github.com/gogits/gogs/modules/auth"
  14. "github.com/gogits/gogs/modules/avatar"
  15. "github.com/gogits/gogs/modules/base"
  16. "github.com/gogits/gogs/modules/log"
  17. "github.com/gogits/gogs/modules/middleware"
  18. "github.com/gogits/gogs/modules/oauth2"
  19. "github.com/gogits/gogs/routers"
  20. "github.com/gogits/gogs/routers/admin"
  21. "github.com/gogits/gogs/routers/api/v1"
  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. func newMartini() *martini.ClassicMartini {
  36. r := martini.NewRouter()
  37. m := martini.New()
  38. m.Use(middleware.Logger())
  39. m.Use(martini.Recovery())
  40. m.Use(martini.Static("public"))
  41. m.MapTo(r, (*martini.Routes)(nil))
  42. m.Action(r.Handle)
  43. return &martini.ClassicMartini{m, r}
  44. }
  45. func runWeb(*cli.Context) {
  46. routers.GlobalInit()
  47. m := newMartini()
  48. // Middlewares.
  49. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  50. m.Use(middleware.InitContext())
  51. if base.OauthService != nil {
  52. if base.OauthService.GitHub.Enabled {
  53. m.Use(oauth2.Github(&oauth2.Options{
  54. ClientId: base.OauthService.GitHub.ClientId,
  55. ClientSecret: base.OauthService.GitHub.ClientSecret,
  56. RedirectURL: base.AppUrl + oauth2.PathCallback[1:],
  57. Scopes: []string{base.OauthService.GitHub.Scopes},
  58. }))
  59. }
  60. }
  61. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  62. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  63. ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{
  64. SignInRequire: base.Service.RequireSignInView,
  65. DisableCsrf: true,
  66. })
  67. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  68. // Routers.
  69. m.Get("/", ignSignIn, routers.Home)
  70. m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install)
  71. m.Get("/issues", reqSignIn, user.Issues)
  72. m.Get("/pulls", reqSignIn, user.Pulls)
  73. m.Get("/stars", reqSignIn, user.Stars)
  74. m.Get("/help", routers.Help)
  75. m.Group("/api/v1", func(r martini.Router) {
  76. r.Post("/markdown", v1.Markdown)
  77. })
  78. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  79. m.Get("/avatar/:hash", avt.ServeHTTP)
  80. m.Group("/user", func(r martini.Router) {
  81. r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  82. r.Any("/login/github", user.SocialSignIn)
  83. r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  84. r.Any("/forget_password", user.ForgotPasswd)
  85. r.Any("/reset_password", user.ResetPasswd)
  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. m.Any("/repo/mirror", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Mirror)
  105. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  106. m.Get("/admin", adminReq, admin.Dashboard)
  107. m.Group("/admin", func(r martini.Router) {
  108. r.Get("/users", admin.Users)
  109. r.Get("/repos", admin.Repositories)
  110. r.Get("/config", admin.Config)
  111. }, adminReq)
  112. m.Group("/admin/users", func(r martini.Router) {
  113. r.Any("/new", binding.BindIgnErr(auth.RegisterForm{}), admin.NewUser)
  114. r.Any("/:userid", binding.BindIgnErr(auth.AdminEditUserForm{}), admin.EditUser)
  115. r.Any("/:userid/delete", admin.DeleteUser)
  116. }, adminReq)
  117. if martini.Env == martini.Dev {
  118. m.Get("/template/**", dev.TemplatePreview)
  119. }
  120. m.Group("/:username/:reponame", func(r martini.Router) {
  121. r.Post("/settings", repo.SettingPost)
  122. r.Get("/settings", repo.Setting)
  123. r.Get("/action/:action", repo.Action)
  124. r.Any("/issues/new", binding.BindIgnErr(auth.CreateIssueForm{}), repo.CreateIssue)
  125. r.Post("/issues/:index", binding.BindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  126. r.Post("/comment/:action", repo.Comment)
  127. }, reqSignIn, middleware.RepoAssignment(true))
  128. m.Group("/:username/:reponame", func(r martini.Router) {
  129. r.Get("/issues", repo.Issues)
  130. r.Get("/issues/:index", repo.ViewIssue)
  131. r.Get("/releases", repo.Releases)
  132. r.Any("/releases/new", repo.ReleasesNew)
  133. r.Get("/pulls", repo.Pulls)
  134. r.Get("/branches", repo.Branches)
  135. }, ignSignIn, middleware.RepoAssignment(true))
  136. m.Group("/:username/:reponame", func(r martini.Router) {
  137. r.Get("/src/:branchname", repo.Single)
  138. r.Get("/src/:branchname/**", repo.Single)
  139. r.Get("/raw/:branchname/**", repo.SingleDownload)
  140. r.Get("/commits/:branchname", repo.Commits)
  141. r.Get("/commit/:branchname", repo.Diff)
  142. r.Get("/commit/:branchname/**", repo.Diff)
  143. }, ignSignIn, middleware.RepoAssignment(true, true))
  144. m.Group("/:username", func(r martini.Router) {
  145. r.Any("/:reponame/**", repo.Http)
  146. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  147. }, ignSignInAndCsrf)
  148. // Not found handler.
  149. m.NotFound(routers.NotFound)
  150. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  151. listenAddr := fmt.Sprintf("%s:%s",
  152. base.Cfg.MustValue("server", "HTTP_ADDR"),
  153. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  154. if protocol == "http" {
  155. log.Info("Listen: http://%s", listenAddr)
  156. if err := http.ListenAndServe(listenAddr, m); err != nil {
  157. qlog.Error(err.Error())
  158. }
  159. } else if protocol == "https" {
  160. log.Info("Listen: https://%s", listenAddr)
  161. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  162. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  163. qlog.Error(err.Error())
  164. }
  165. }
  166. }