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.

191 lines
6.4 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
  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. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  64. // Routers.
  65. m.Get("/", ignSignIn, routers.Home)
  66. m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install)
  67. m.Get("/issues", reqSignIn, user.Issues)
  68. m.Get("/pulls", reqSignIn, user.Pulls)
  69. m.Get("/stars", reqSignIn, user.Stars)
  70. m.Get("/help", routers.Help)
  71. m.Group("/api/v1", func(r martini.Router) {
  72. r.Post("/markdown", v1.Markdown)
  73. })
  74. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  75. m.Get("/avatar/:hash", avt.ServeHTTP)
  76. m.Group("/user", func(r martini.Router) {
  77. r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  78. r.Any("/login/github", user.SocialSignIn)
  79. r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  80. r.Any("/forget_password", user.ForgotPasswd)
  81. r.Any("/reset_password", user.ResetPasswd)
  82. }, reqSignOut)
  83. m.Group("/user", func(r martini.Router) {
  84. r.Any("/logout", user.SignOut)
  85. r.Any("/delete", user.Delete)
  86. r.Any("/setting", binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  87. }, reqSignIn)
  88. m.Group("/user", func(r martini.Router) {
  89. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  90. r.Get("/activate", user.Activate)
  91. })
  92. m.Group("/user/setting", func(r martini.Router) {
  93. r.Any("/password", binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  94. r.Any("/ssh", binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  95. r.Any("/notification", user.SettingNotification)
  96. r.Any("/security", user.SettingSecurity)
  97. }, reqSignIn)
  98. m.Get("/user/:username", ignSignIn, user.Profile)
  99. m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  100. m.Any("/repo/mirror", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Mirror)
  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. if martini.Env == martini.Dev {
  114. m.Get("/template/**", dev.TemplatePreview)
  115. }
  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. r.Any("/issues/new", binding.BindIgnErr(auth.CreateIssueForm{}), repo.CreateIssue)
  121. r.Post("/issues/:index", binding.BindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  122. r.Post("/comment/:action", repo.Comment)
  123. }, reqSignIn, middleware.RepoAssignment(true))
  124. m.Group("/:username/:reponame", func(r martini.Router) {
  125. r.Get("/issues", repo.Issues)
  126. r.Get("/issues/:index", repo.ViewIssue)
  127. r.Get("/releases", repo.Releases)
  128. r.Any("/releases/new", repo.ReleasesNew)
  129. r.Get("/pulls", repo.Pulls)
  130. r.Get("/branches", repo.Branches)
  131. }, ignSignIn, middleware.RepoAssignment(true))
  132. m.Group("/:username/:reponame", func(r martini.Router) {
  133. r.Get("/src/:branchname", repo.Single)
  134. r.Get("/src/:branchname/**", repo.Single)
  135. r.Get("/raw/:branchname/**", repo.SingleDownload)
  136. r.Get("/commits/:branchname", repo.Commits)
  137. r.Get("/commit/:branchname", repo.Diff)
  138. r.Get("/commit/:branchname/**", repo.Diff)
  139. }, ignSignIn, middleware.RepoAssignment(true, true))
  140. m.Group("/:username", func(r martini.Router) {
  141. r.Any("/:reponame/**", repo.Http)
  142. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  143. }, ignSignIn)
  144. // Not found handler.
  145. m.NotFound(routers.NotFound)
  146. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  147. listenAddr := fmt.Sprintf("%s:%s",
  148. base.Cfg.MustValue("server", "HTTP_ADDR"),
  149. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  150. if protocol == "http" {
  151. log.Info("Listen: http://%s", listenAddr)
  152. if err := http.ListenAndServe(listenAddr, m); err != nil {
  153. qlog.Error(err.Error())
  154. }
  155. } else if protocol == "https" {
  156. log.Info("Listen: https://%s", listenAddr)
  157. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  158. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  159. qlog.Error(err.Error())
  160. }
  161. }
  162. }