web.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 cmd
  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/auth/apiv1"
  15. "github.com/gogits/gogs/modules/avatar"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/middleware"
  19. "github.com/gogits/gogs/modules/middleware/binding"
  20. "github.com/gogits/gogs/routers"
  21. "github.com/gogits/gogs/routers/admin"
  22. "github.com/gogits/gogs/routers/api/v1"
  23. "github.com/gogits/gogs/routers/dev"
  24. "github.com/gogits/gogs/routers/repo"
  25. "github.com/gogits/gogs/routers/user"
  26. )
  27. var CmdWeb = cli.Command{
  28. Name: "web",
  29. Usage: "Start Gogs web server",
  30. Description: `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", martini.StaticOptions{SkipLogging: !base.DisableRouterLog}))
  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{
  50. Funcs: []template.FuncMap{base.TemplateFuncs},
  51. IndentJSON: true,
  52. }))
  53. m.Use(middleware.InitContext())
  54. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  55. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  56. ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{DisableCsrf: true})
  57. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  58. bindIgnErr := binding.BindIgnErr
  59. // Routers.
  60. m.Get("/", ignSignIn, routers.Home)
  61. m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install)
  62. m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  63. m.Get("/issues", reqSignIn, user.Issues)
  64. m.Get("/pulls", reqSignIn, user.Pulls)
  65. m.Get("/stars", reqSignIn, user.Stars)
  66. m.Group("/api", func(r martini.Router) {
  67. m.Group("/v1", func(r martini.Router) {
  68. // Miscellaneous.
  69. r.Post("/markdown", bindIgnErr(apiv1.MarkdownForm{}), v1.Markdown)
  70. r.Post("/markdown/raw", v1.MarkdownRaw)
  71. // Users.
  72. r.Get("/users/search", v1.SearchUser)
  73. r.Any("**", func(ctx *middleware.Context) {
  74. ctx.JSON(404, &base.ApiJsonErr{"Not Found", v1.DOC_URL})
  75. })
  76. })
  77. })
  78. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  79. os.MkdirAll("public/img/avatar/", os.ModePerm)
  80. m.Get("/avatar/:hash", avt.ServeHTTP)
  81. m.Group("/user", func(r martini.Router) {
  82. r.Get("/login", user.SignIn)
  83. r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost)
  84. r.Get("/login/:name", user.SocialSignIn)
  85. r.Get("/sign_up", user.SignUp)
  86. r.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  87. r.Get("/reset_password", user.ResetPasswd)
  88. r.Post("/reset_password", user.ResetPasswdPost)
  89. }, reqSignOut)
  90. m.Group("/user", func(r martini.Router) {
  91. r.Get("/delete", user.Delete)
  92. r.Post("/delete", user.DeletePost)
  93. r.Get("/settings", user.Setting)
  94. r.Post("/settings", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost)
  95. }, reqSignIn)
  96. m.Group("/user", func(r martini.Router) {
  97. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  98. r.Any("/activate", user.Activate)
  99. r.Get("/email2user", user.Email2User)
  100. r.Get("/forget_password", user.ForgotPasswd)
  101. r.Post("/forget_password", user.ForgotPasswdPost)
  102. r.Get("/logout", user.SignOut)
  103. })
  104. m.Group("/user/settings", func(r martini.Router) {
  105. r.Get("/social", user.SettingSocial)
  106. r.Get("/password", user.SettingPassword)
  107. r.Post("/password", bindIgnErr(auth.UpdatePasswdForm{}), user.SettingPasswordPost)
  108. r.Any("/ssh", bindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  109. r.Get("/notification", user.SettingNotification)
  110. r.Get("/security", user.SettingSecurity)
  111. }, reqSignIn)
  112. m.Get("/user/:username", ignSignIn, user.Profile)
  113. m.Group("/repo", func(r martini.Router) {
  114. r.Get("/create", repo.Create)
  115. r.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  116. r.Get("/migrate", repo.Migrate)
  117. r.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
  118. }, reqSignIn)
  119. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  120. m.Get("/admin", adminReq, admin.Dashboard)
  121. m.Group("/admin", func(r martini.Router) {
  122. r.Get("/users", admin.Users)
  123. r.Get("/repos", admin.Repositories)
  124. r.Get("/config", admin.Config)
  125. r.Get("/auths", admin.Auths)
  126. }, adminReq)
  127. m.Group("/admin/users", func(r martini.Router) {
  128. r.Get("/new", admin.NewUser)
  129. r.Post("/new", bindIgnErr(auth.RegisterForm{}), admin.NewUserPost)
  130. r.Get("/:userid", admin.EditUser)
  131. r.Post("/:userid", bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  132. r.Get("/:userid/delete", admin.DeleteUser)
  133. }, adminReq)
  134. m.Group("/admin/auths", func(r martini.Router) {
  135. r.Get("/new", admin.NewAuthSource)
  136. r.Post("/new", bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)
  137. r.Get("/:authid", admin.EditAuthSource)
  138. r.Post("/:authid", bindIgnErr(auth.AuthenticationForm{}), admin.EditAuthSourcePost)
  139. r.Get("/:authid/delete", admin.DeleteAuthSource)
  140. }, adminReq)
  141. if martini.Env == martini.Dev {
  142. m.Get("/template/**", dev.TemplatePreview)
  143. }
  144. reqOwner := middleware.RequireOwner()
  145. m.Group("/:username/:reponame", func(r martini.Router) {
  146. r.Get("/settings", repo.Setting)
  147. r.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingPost)
  148. m.Group("/settings", func(r martini.Router) {
  149. r.Get("/collaboration", repo.Collaboration)
  150. r.Post("/collaboration", repo.CollaborationPost)
  151. r.Get("/hooks", repo.WebHooks)
  152. r.Get("/hooks/add", repo.WebHooksAdd)
  153. r.Post("/hooks/add", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksAddPost)
  154. r.Get("/hooks/:id", repo.WebHooksEdit)
  155. r.Post("/hooks/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  156. })
  157. }, reqSignIn, middleware.RepoAssignment(true), reqOwner)
  158. m.Group("/:username/:reponame", func(r martini.Router) {
  159. r.Get("/action/:action", repo.Action)
  160. m.Group("/issues", func(r martini.Router) {
  161. r.Get("/new", repo.CreateIssue)
  162. r.Post("/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
  163. r.Post("/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  164. r.Post("/:index/label", repo.UpdateIssueLabel)
  165. r.Post("/:index/milestone", repo.UpdateIssueMilestone)
  166. r.Post("/:index/assignee", repo.UpdateAssignee)
  167. r.Post("/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
  168. r.Post("/labels/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
  169. r.Post("/labels/delete", repo.DeleteLabel)
  170. r.Get("/milestones", repo.Milestones)
  171. r.Get("/milestones/new", repo.NewMilestone)
  172. r.Post("/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
  173. r.Get("/milestones/:index/edit", repo.UpdateMilestone)
  174. r.Post("/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost)
  175. r.Get("/milestones/:index/:action", repo.UpdateMilestone)
  176. })
  177. r.Post("/comment/:action", repo.Comment)
  178. r.Get("/releases/new", repo.ReleasesNew)
  179. }, reqSignIn, middleware.RepoAssignment(true))
  180. m.Group("/:username/:reponame", func(r martini.Router) {
  181. r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.ReleasesNewPost)
  182. }, reqSignIn, middleware.RepoAssignment(true, true))
  183. m.Group("/:username/:reponame", func(r martini.Router) {
  184. r.Get("/issues", repo.Issues)
  185. r.Get("/issues/:index", repo.ViewIssue)
  186. r.Get("/pulls", repo.Pulls)
  187. r.Get("/branches", repo.Branches)
  188. }, ignSignIn, middleware.RepoAssignment(true))
  189. m.Group("/:username/:reponame", func(r martini.Router) {
  190. r.Get("/src/:branchname", repo.Single)
  191. r.Get("/src/:branchname/**", repo.Single)
  192. r.Get("/raw/:branchname/**", repo.SingleDownload)
  193. r.Get("/commits/:branchname", repo.Commits)
  194. r.Get("/commits/:branchname/search", repo.SearchCommits)
  195. r.Get("/commits/:branchname/**", repo.FileHistory)
  196. r.Get("/commit/:branchname", repo.Diff)
  197. r.Get("/commit/:branchname/**", repo.Diff)
  198. r.Get("/releases", repo.Releases)
  199. r.Get("/archive/:branchname/:reponame.zip", repo.ZipDownload)
  200. r.Get("/archive/:branchname/:reponame.tar.gz", repo.TarGzDownload)
  201. }, ignSignIn, middleware.RepoAssignment(true, true))
  202. m.Group("/:username", func(r martini.Router) {
  203. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  204. r.Any("/:reponame/**", repo.Http)
  205. }, ignSignInAndCsrf)
  206. // Not found handler.
  207. m.NotFound(routers.NotFound)
  208. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  209. listenAddr := fmt.Sprintf("%s:%s",
  210. base.Cfg.MustValue("server", "HTTP_ADDR", "0.0.0.0"),
  211. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  212. if protocol == "http" {
  213. log.Info("Listen: http://%s", listenAddr)
  214. if err := http.ListenAndServe(listenAddr, m); err != nil {
  215. qlog.Error(err.Error())
  216. }
  217. } else if protocol == "https" {
  218. log.Info("Listen: https://%s", listenAddr)
  219. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  220. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  221. qlog.Error(err.Error())
  222. }
  223. }
  224. qlog.Fatalf("Invalid protocol: %s", protocol)
  225. }