web.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. bindIgnErr := binding.BindIgnErr
  69. // Routers.
  70. m.Get("/", ignSignIn, routers.Home)
  71. m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install)
  72. m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  73. m.Get("/issues", reqSignIn, user.Issues)
  74. m.Get("/pulls", reqSignIn, user.Pulls)
  75. m.Get("/stars", reqSignIn, user.Stars)
  76. m.Get("/help", routers.Help)
  77. m.Group("/api/v1", func(r martini.Router) {
  78. r.Post("/markdown", v1.Markdown)
  79. })
  80. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  81. m.Get("/avatar/:hash", avt.ServeHTTP)
  82. m.Group("/user", func(r martini.Router) {
  83. r.Get("/login", user.SignIn)
  84. r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost)
  85. r.Get("/login/github", user.SocialSignIn)
  86. r.Get("/sign_up", user.SignUp)
  87. r.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  88. r.Get("/reset_password", user.ResetPasswd)
  89. r.Post("/reset_password", user.ResetPasswdPost)
  90. }, reqSignOut)
  91. m.Group("/user", func(r martini.Router) {
  92. r.Get("/logout", user.SignOut)
  93. r.Get("/delete", user.Delete)
  94. r.Post("/delete", user.DeletePost)
  95. r.Get("/setting", user.Setting)
  96. r.Post("/setting", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost)
  97. }, reqSignIn)
  98. m.Group("/user", func(r martini.Router) {
  99. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  100. r.Get("/activate", user.Activate)
  101. r.Get("/forget_password", user.ForgotPasswd)
  102. r.Post("/forget_password", user.ForgotPasswdPost)
  103. })
  104. m.Group("/user/setting", func(r martini.Router) {
  105. r.Get("/password", user.SettingPassword)
  106. r.Post("/password", bindIgnErr(auth.UpdatePasswdForm{}), user.SettingPasswordPost)
  107. r.Any("/ssh", bindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  108. r.Get("/notification", user.SettingNotification)
  109. r.Get("/security", user.SettingSecurity)
  110. }, reqSignIn)
  111. m.Get("/user/:username", ignSignIn, user.Profile)
  112. m.Group("/repo", func(r martini.Router) {
  113. m.Get("/create", repo.Create)
  114. m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  115. m.Get("/mirror", repo.Mirror)
  116. m.Post("/mirror", bindIgnErr(auth.CreateRepoForm{}), repo.MirrorPost)
  117. }, reqSignIn)
  118. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  119. m.Get("/admin", adminReq, admin.Dashboard)
  120. m.Group("/admin", func(r martini.Router) {
  121. r.Get("/users", admin.Users)
  122. r.Get("/repos", admin.Repositories)
  123. r.Get("/config", admin.Config)
  124. }, adminReq)
  125. m.Group("/admin/users", func(r martini.Router) {
  126. r.Get("/new", admin.NewUser)
  127. r.Post("/new", bindIgnErr(auth.RegisterForm{}), admin.NewUserPost)
  128. r.Get("/:userid", admin.EditUser)
  129. r.Post("/:userid", bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  130. r.Get("/:userid/delete", admin.DeleteUser)
  131. }, adminReq)
  132. if martini.Env == martini.Dev {
  133. m.Get("/template/**", dev.TemplatePreview)
  134. }
  135. m.Group("/:username/:reponame", func(r martini.Router) {
  136. r.Post("/settings", repo.SettingPost)
  137. r.Get("/settings", repo.Setting)
  138. r.Get("/action/:action", repo.Action)
  139. r.Get("/issues/new", repo.CreateIssue)
  140. r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
  141. r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  142. r.Post("/comment/:action", repo.Comment)
  143. r.Post("/import", repo.Import)
  144. }, reqSignIn, middleware.RepoAssignment(true))
  145. m.Group("/:username/:reponame", func(r martini.Router) {
  146. r.Get("/issues", repo.Issues)
  147. r.Get("/issues/:index", repo.ViewIssue)
  148. r.Get("/releases", repo.Releases)
  149. r.Any("/releases/new", repo.ReleasesNew) // TODO:
  150. r.Get("/pulls", repo.Pulls)
  151. r.Get("/branches", repo.Branches)
  152. }, ignSignIn, middleware.RepoAssignment(true))
  153. m.Group("/:username/:reponame", func(r martini.Router) {
  154. r.Get("/src/:branchname", repo.Single)
  155. r.Get("/src/:branchname/**", repo.Single)
  156. r.Get("/raw/:branchname/**", repo.SingleDownload)
  157. r.Get("/commits/:branchname", repo.Commits)
  158. r.Get("/commits/:branchname/search", repo.SearchCommits)
  159. r.Get("/commit/:branchname", repo.Diff)
  160. r.Get("/commit/:branchname/**", repo.Diff)
  161. }, ignSignIn, middleware.RepoAssignment(true, true))
  162. m.Group("/:username", func(r martini.Router) {
  163. r.Any("/:reponame/**", repo.Http)
  164. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  165. }, ignSignInAndCsrf)
  166. // Not found handler.
  167. m.NotFound(routers.NotFound)
  168. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  169. listenAddr := fmt.Sprintf("%s:%s",
  170. base.Cfg.MustValue("server", "HTTP_ADDR"),
  171. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  172. if protocol == "http" {
  173. log.Info("Listen: http://%s", listenAddr)
  174. if err := http.ListenAndServe(listenAddr, m); err != nil {
  175. qlog.Error(err.Error())
  176. }
  177. } else if protocol == "https" {
  178. log.Info("Listen: https://%s", listenAddr)
  179. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  180. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  181. qlog.Error(err.Error())
  182. }
  183. }
  184. }