You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

web.go 7.2 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "os"
  10. "github.com/codegangsta/cli"
  11. "github.com/go-martini/martini"
  12. qlog "github.com/qiniu/log"
  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/routers"
  19. "github.com/gogits/gogs/routers/admin"
  20. "github.com/gogits/gogs/routers/api/v1"
  21. "github.com/gogits/gogs/routers/dev"
  22. "github.com/gogits/gogs/routers/repo"
  23. "github.com/gogits/gogs/routers/user"
  24. )
  25. var CmdWeb = cli.Command{
  26. Name: "web",
  27. Usage: "Gogs web server",
  28. Description: `
  29. gogs web server is the only thing you need to run,
  30. and it takes care of all the other things for you`,
  31. Action: runWeb,
  32. Flags: []cli.Flag{},
  33. }
  34. func newMartini() *martini.ClassicMartini {
  35. r := martini.NewRouter()
  36. m := martini.New()
  37. m.Use(middleware.Logger())
  38. m.Use(martini.Recovery())
  39. m.Use(martini.Static("public"))
  40. m.MapTo(r, (*martini.Routes)(nil))
  41. m.Action(r.Handle)
  42. return &martini.ClassicMartini{m, r}
  43. }
  44. func runWeb(*cli.Context) {
  45. routers.GlobalInit()
  46. m := newMartini()
  47. // Middlewares.
  48. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  49. m.Use(middleware.InitContext())
  50. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  51. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  52. ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{DisableCsrf: true})
  53. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  54. bindIgnErr := middleware.BindIgnErr
  55. // Routers.
  56. m.Get("/", ignSignIn, routers.Home)
  57. m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install)
  58. m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  59. m.Get("/issues", reqSignIn, user.Issues)
  60. m.Get("/pulls", reqSignIn, user.Pulls)
  61. m.Get("/stars", reqSignIn, user.Stars)
  62. m.Get("/help", routers.Help)
  63. m.Group("/api/v1", func(r martini.Router) {
  64. r.Post("/markdown", v1.Markdown)
  65. })
  66. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  67. os.MkdirAll("public/img/avatar/", os.ModePerm)
  68. m.Get("/avatar/:hash", avt.ServeHTTP)
  69. m.Group("/user", func(r martini.Router) {
  70. r.Get("/login", user.SignIn)
  71. r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost)
  72. r.Get("/login/:name", user.SocialSignIn)
  73. r.Get("/sign_up", user.SignUp)
  74. r.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  75. r.Get("/reset_password", user.ResetPasswd)
  76. r.Post("/reset_password", user.ResetPasswdPost)
  77. }, reqSignOut)
  78. m.Group("/user", func(r martini.Router) {
  79. r.Get("/delete", user.Delete)
  80. r.Post("/delete", user.DeletePost)
  81. r.Get("/setting", user.Setting)
  82. r.Post("/setting", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost)
  83. }, reqSignIn)
  84. m.Group("/user", func(r martini.Router) {
  85. r.Get("/feeds", middleware.Bind(auth.FeedsForm{}), user.Feeds)
  86. r.Any("/activate", user.Activate)
  87. r.Get("/email2user", user.Email2User)
  88. r.Get("/forget_password", user.ForgotPasswd)
  89. r.Post("/forget_password", user.ForgotPasswdPost)
  90. r.Get("/logout", user.SignOut)
  91. })
  92. m.Group("/user/setting", func(r martini.Router) {
  93. r.Get("/social", user.SettingSocial)
  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. r.Get("/releases/new", repo.ReleasesNew)
  133. }, reqSignIn, middleware.RepoAssignment(true))
  134. m.Group("/:username/:reponame", func(r martini.Router) {
  135. r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.ReleasesNewPost)
  136. }, reqSignIn, middleware.RepoAssignment(true, true))
  137. m.Group("/:username/:reponame", func(r martini.Router) {
  138. r.Get("/issues", repo.Issues)
  139. r.Get("/issues/:index", repo.ViewIssue)
  140. r.Get("/pulls", repo.Pulls)
  141. r.Get("/branches", repo.Branches)
  142. }, ignSignIn, middleware.RepoAssignment(true))
  143. m.Group("/:username/:reponame", func(r martini.Router) {
  144. r.Get("/src/:branchname", repo.Single)
  145. r.Get("/src/:branchname/**", repo.Single)
  146. r.Get("/raw/:branchname/**", repo.SingleDownload)
  147. r.Get("/commits/:branchname", repo.Commits)
  148. r.Get("/commits/:branchname/search", repo.SearchCommits)
  149. r.Get("/commit/:branchname", repo.Diff)
  150. r.Get("/commit/:branchname/**", repo.Diff)
  151. r.Get("/releases", repo.Releases)
  152. r.Get("/archive/:branchname/:reponame.zip", repo.ZipDownload)
  153. }, ignSignIn, middleware.RepoAssignment(true, true))
  154. m.Group("/:username", func(r martini.Router) {
  155. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  156. r.Any("/:reponame/**", repo.Http)
  157. }, ignSignInAndCsrf)
  158. // Not found handler.
  159. m.NotFound(routers.NotFound)
  160. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  161. listenAddr := fmt.Sprintf("%s:%s",
  162. base.Cfg.MustValue("server", "HTTP_ADDR"),
  163. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  164. if protocol == "http" {
  165. log.Info("Listen: http://%s", listenAddr)
  166. if err := http.ListenAndServe(listenAddr, m); err != nil {
  167. qlog.Error(err.Error())
  168. }
  169. } else if protocol == "https" {
  170. log.Info("Listen: https://%s", listenAddr)
  171. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  172. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  173. qlog.Error(err.Error())
  174. }
  175. }
  176. }