web.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/avatar"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/middleware"
  17. "github.com/gogits/gogs/routers"
  18. "github.com/gogits/gogs/routers/admin"
  19. "github.com/gogits/gogs/routers/api/v1"
  20. "github.com/gogits/gogs/routers/dev"
  21. "github.com/gogits/gogs/routers/repo"
  22. "github.com/gogits/gogs/routers/user"
  23. )
  24. var CmdWeb = cli.Command{
  25. Name: "web",
  26. Usage: "Gogs web server",
  27. Description: `
  28. gogs web server is the only thing you need to run,
  29. and it takes care of all the other things for you`,
  30. Action: runWeb,
  31. Flags: []cli.Flag{},
  32. }
  33. func newMartini() *martini.ClassicMartini {
  34. r := martini.NewRouter()
  35. m := martini.New()
  36. m.Use(middleware.Logger())
  37. m.Use(martini.Recovery())
  38. m.Use(martini.Static("public"))
  39. m.MapTo(r, (*martini.Routes)(nil))
  40. m.Action(r.Handle)
  41. return &martini.ClassicMartini{m, r}
  42. }
  43. func runWeb(*cli.Context) {
  44. routers.GlobalInit()
  45. m := newMartini()
  46. // Middlewares.
  47. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  48. m.Use(middleware.InitContext())
  49. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  50. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  51. ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{
  52. SignInRequire: base.Service.RequireSignInView,
  53. DisableCsrf: true,
  54. })
  55. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  56. bindIgnErr := middleware.BindIgnErr
  57. // Routers.
  58. m.Get("/", ignSignIn, routers.Home)
  59. m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install)
  60. m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  61. m.Get("/issues", reqSignIn, user.Issues)
  62. m.Get("/pulls", reqSignIn, user.Pulls)
  63. m.Get("/stars", reqSignIn, user.Stars)
  64. m.Get("/help", routers.Help)
  65. m.Group("/api/v1", func(r martini.Router) {
  66. r.Post("/markdown", v1.Markdown)
  67. })
  68. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  69. m.Get("/avatar/:hash", avt.ServeHTTP)
  70. m.Group("/user", func(r martini.Router) {
  71. r.Get("/login", user.SignIn)
  72. r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost)
  73. r.Get("/login/:name", user.SocialSignIn)
  74. r.Get("/sign_up", user.SignUp)
  75. r.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  76. r.Get("/reset_password", user.ResetPasswd)
  77. r.Post("/reset_password", user.ResetPasswdPost)
  78. }, reqSignOut)
  79. m.Group("/user", func(r martini.Router) {
  80. r.Get("/logout", user.SignOut)
  81. r.Get("/delete", user.Delete)
  82. r.Post("/delete", user.DeletePost)
  83. r.Get("/setting", user.Setting)
  84. r.Post("/setting", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost)
  85. }, reqSignIn)
  86. m.Group("/user", func(r martini.Router) {
  87. r.Get("/feeds", middleware.Bind(auth.FeedsForm{}), user.Feeds)
  88. r.Get("/activate", user.Activate)
  89. r.Get("/email2user", user.Email2User)
  90. r.Get("/forget_password", user.ForgotPasswd)
  91. r.Post("/forget_password", user.ForgotPasswdPost)
  92. })
  93. m.Group("/user/setting", func(r martini.Router) {
  94. r.Get("/password", user.SettingPassword)
  95. r.Post("/password", bindIgnErr(auth.UpdatePasswdForm{}), user.SettingPasswordPost)
  96. r.Any("/ssh", bindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  97. r.Get("/notification", user.SettingNotification)
  98. r.Get("/security", user.SettingSecurity)
  99. }, reqSignIn)
  100. m.Get("/user/:username", ignSignIn, user.Profile)
  101. m.Group("/repo", func(r martini.Router) {
  102. m.Get("/create", repo.Create)
  103. m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  104. m.Get("/migrate", repo.Migrate)
  105. m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
  106. }, reqSignIn)
  107. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  108. m.Get("/admin", adminReq, admin.Dashboard)
  109. m.Group("/admin", func(r martini.Router) {
  110. r.Get("/users", admin.Users)
  111. r.Get("/repos", admin.Repositories)
  112. r.Get("/config", admin.Config)
  113. }, adminReq)
  114. m.Group("/admin/users", func(r martini.Router) {
  115. r.Get("/new", admin.NewUser)
  116. r.Post("/new", bindIgnErr(auth.RegisterForm{}), admin.NewUserPost)
  117. r.Get("/:userid", admin.EditUser)
  118. r.Post("/:userid", bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  119. r.Get("/:userid/delete", admin.DeleteUser)
  120. }, adminReq)
  121. if martini.Env == martini.Dev {
  122. m.Get("/template/**", dev.TemplatePreview)
  123. }
  124. m.Group("/:username/:reponame", func(r martini.Router) {
  125. r.Get("/settings", repo.Setting)
  126. r.Post("/settings", repo.SettingPost)
  127. r.Get("/action/:action", repo.Action)
  128. r.Get("/issues/new", repo.CreateIssue)
  129. r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
  130. r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  131. r.Post("/comment/:action", repo.Comment)
  132. }, reqSignIn, middleware.RepoAssignment(true))
  133. m.Group("/:username/:reponame", func(r martini.Router) {
  134. r.Get("/issues", repo.Issues)
  135. r.Get("/issues/:index", repo.ViewIssue)
  136. r.Get("/releases", repo.Releases)
  137. r.Any("/releases/new", repo.ReleasesNew) // TODO:
  138. r.Get("/pulls", repo.Pulls)
  139. r.Get("/branches", repo.Branches)
  140. }, ignSignIn, middleware.RepoAssignment(true))
  141. m.Group("/:username/:reponame", func(r martini.Router) {
  142. r.Get("/src/:branchname", repo.Single)
  143. r.Get("/src/:branchname/**", repo.Single)
  144. r.Get("/raw/:branchname/**", repo.SingleDownload)
  145. r.Get("/commits/:branchname", repo.Commits)
  146. r.Get("/commits/:branchname/search", repo.SearchCommits)
  147. r.Get("/commit/:branchname", repo.Diff)
  148. r.Get("/commit/:branchname/**", repo.Diff)
  149. }, ignSignIn, middleware.RepoAssignment(true, true))
  150. m.Group("/:username", func(r martini.Router) {
  151. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  152. r.Any("/:reponame/**", repo.Http)
  153. }, ignSignInAndCsrf)
  154. // Not found handler.
  155. m.NotFound(routers.NotFound)
  156. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  157. listenAddr := fmt.Sprintf("%s:%s",
  158. base.Cfg.MustValue("server", "HTTP_ADDR"),
  159. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  160. if protocol == "http" {
  161. log.Info("Listen: http://%s", listenAddr)
  162. if err := http.ListenAndServe(listenAddr, m); err != nil {
  163. qlog.Error(err.Error())
  164. }
  165. } else if protocol == "https" {
  166. log.Info("Listen: https://%s", listenAddr)
  167. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  168. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  169. qlog.Error(err.Error())
  170. }
  171. }
  172. }