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 10 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "path"
  12. "github.com/codegangsta/cli"
  13. "github.com/go-martini/martini"
  14. "github.com/gogits/gogs/modules/auth"
  15. "github.com/gogits/gogs/modules/auth/apiv1"
  16. "github.com/gogits/gogs/modules/avatar"
  17. "github.com/gogits/gogs/modules/base"
  18. "github.com/gogits/gogs/modules/log"
  19. "github.com/gogits/gogs/modules/middleware"
  20. "github.com/gogits/gogs/modules/middleware/binding"
  21. "github.com/gogits/gogs/modules/setting"
  22. "github.com/gogits/gogs/routers"
  23. "github.com/gogits/gogs/routers/admin"
  24. "github.com/gogits/gogs/routers/api/v1"
  25. "github.com/gogits/gogs/routers/dev"
  26. "github.com/gogits/gogs/routers/org"
  27. "github.com/gogits/gogs/routers/repo"
  28. "github.com/gogits/gogs/routers/user"
  29. )
  30. var CmdWeb = cli.Command{
  31. Name: "web",
  32. Usage: "Start Gogs web server",
  33. Description: `Gogs web server is the only thing you need to run,
  34. and it takes care of all the other things for you`,
  35. Action: runWeb,
  36. Flags: []cli.Flag{},
  37. }
  38. // checkVersion checks if binary matches the version of temolate files.
  39. func checkVersion() {
  40. data, err := ioutil.ReadFile(path.Join(setting.StaticRootPath, "templates/VERSION"))
  41. if err != nil {
  42. log.Fatal("Fail to read 'templates/VERSION': %v", err)
  43. }
  44. if string(data) != setting.AppVer {
  45. log.Fatal("Binary and template file version does not match, did you forget to recompile?")
  46. }
  47. }
  48. func newMartini() *martini.ClassicMartini {
  49. r := martini.NewRouter()
  50. m := martini.New()
  51. m.Use(middleware.Logger())
  52. m.Use(martini.Recovery())
  53. m.Use(martini.Static(path.Join(setting.StaticRootPath, "public"),
  54. martini.StaticOptions{SkipLogging: !setting.DisableRouterLog}))
  55. m.MapTo(r, (*martini.Routes)(nil))
  56. m.Action(r.Handle)
  57. return &martini.ClassicMartini{m, r}
  58. }
  59. func runWeb(*cli.Context) {
  60. routers.GlobalInit()
  61. checkVersion()
  62. m := newMartini()
  63. // Middlewares.
  64. m.Use(middleware.Renderer(middleware.RenderOptions{
  65. Directory: path.Join(setting.StaticRootPath, "templates"),
  66. Funcs: []template.FuncMap{base.TemplateFuncs},
  67. IndentJSON: true,
  68. }))
  69. m.Use(middleware.InitContext())
  70. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  71. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: setting.Service.RequireSignInView})
  72. ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{DisableCsrf: true})
  73. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  74. bindIgnErr := binding.BindIgnErr
  75. // Routers.
  76. m.Get("/", ignSignIn, routers.Home)
  77. m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install)
  78. m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  79. m.Group("", func(r martini.Router) {
  80. r.Get("/issues", user.Issues)
  81. r.Get("/pulls", user.Pulls)
  82. r.Get("/stars", user.Stars)
  83. }, reqSignIn)
  84. m.Group("/api", func(_ martini.Router) {
  85. m.Group("/v1", func(r martini.Router) {
  86. // Miscellaneous.
  87. r.Post("/markdown", bindIgnErr(apiv1.MarkdownForm{}), v1.Markdown)
  88. r.Post("/markdown/raw", v1.MarkdownRaw)
  89. // Users.
  90. r.Get("/users/search", v1.SearchUser)
  91. r.Any("**", func(ctx *middleware.Context) {
  92. ctx.JSON(404, &base.ApiJsonErr{"Not Found", v1.DOC_URL})
  93. })
  94. })
  95. })
  96. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  97. os.MkdirAll("public/img/avatar/", os.ModePerm)
  98. m.Get("/avatar/:hash", avt.ServeHTTP)
  99. m.Group("/user", func(r martini.Router) {
  100. r.Get("/login", user.SignIn)
  101. r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost)
  102. r.Get("/login/:name", user.SocialSignIn)
  103. r.Get("/sign_up", user.SignUp)
  104. r.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  105. r.Get("/reset_password", user.ResetPasswd)
  106. r.Post("/reset_password", user.ResetPasswdPost)
  107. }, reqSignOut)
  108. m.Group("/user", func(r martini.Router) {
  109. r.Get("/delete", user.Delete)
  110. r.Post("/delete", user.DeletePost)
  111. r.Get("/settings", user.Setting)
  112. r.Post("/settings", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost)
  113. }, reqSignIn)
  114. m.Group("/user", func(r martini.Router) {
  115. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  116. r.Any("/activate", user.Activate)
  117. r.Get("/email2user", user.Email2User)
  118. r.Get("/forget_password", user.ForgotPasswd)
  119. r.Post("/forget_password", user.ForgotPasswdPost)
  120. r.Get("/logout", user.SignOut)
  121. })
  122. m.Group("/user/settings", func(r martini.Router) {
  123. r.Get("/social", user.SettingSocial)
  124. r.Get("/password", user.SettingPassword)
  125. r.Post("/password", bindIgnErr(auth.UpdatePasswdForm{}), user.SettingPasswordPost)
  126. r.Any("/ssh", bindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  127. r.Get("/notification", user.SettingNotification)
  128. r.Get("/security", user.SettingSecurity)
  129. }, reqSignIn)
  130. m.Get("/user/:username", ignSignIn, user.Profile)
  131. m.Group("/repo", func(r martini.Router) {
  132. r.Get("/create", repo.Create)
  133. r.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  134. r.Get("/migrate", repo.Migrate)
  135. r.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
  136. }, reqSignIn)
  137. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  138. m.Get("/admin", adminReq, admin.Dashboard)
  139. m.Group("/admin", func(r martini.Router) {
  140. r.Get("/users", admin.Users)
  141. r.Get("/repos", admin.Repositories)
  142. r.Get("/auths", admin.Auths)
  143. r.Get("/config", admin.Config)
  144. r.Get("/monitor", admin.Monitor)
  145. }, adminReq)
  146. m.Group("/admin/users", func(r martini.Router) {
  147. r.Get("/new", admin.NewUser)
  148. r.Post("/new", bindIgnErr(auth.RegisterForm{}), admin.NewUserPost)
  149. r.Get("/:userid", admin.EditUser)
  150. r.Post("/:userid", bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  151. r.Get("/:userid/delete", admin.DeleteUser)
  152. }, adminReq)
  153. m.Group("/admin/auths", func(r martini.Router) {
  154. r.Get("/new", admin.NewAuthSource)
  155. r.Post("/new", bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)
  156. r.Get("/:authid", admin.EditAuthSource)
  157. r.Post("/:authid", bindIgnErr(auth.AuthenticationForm{}), admin.EditAuthSourcePost)
  158. r.Get("/:authid/delete", admin.DeleteAuthSource)
  159. }, adminReq)
  160. if martini.Env == martini.Dev {
  161. m.Get("/template/**", dev.TemplatePreview)
  162. }
  163. reqOwner := middleware.RequireOwner()
  164. m.Group("/o", func(r martini.Router) {
  165. r.Get("/create", org.New)
  166. r.Get("/:org", org.Organization)
  167. r.Get("/:org/dashboard", org.Dashboard)
  168. r.Get("/:org/members", org.Members)
  169. r.Get("/:org/teams", org.Teams)
  170. r.Get("/:org/setting", org.Setting)
  171. })
  172. m.Group("/:username/:reponame", func(r martini.Router) {
  173. r.Get("/settings", repo.Setting)
  174. r.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingPost)
  175. m.Group("/settings", func(r martini.Router) {
  176. r.Get("/collaboration", repo.Collaboration)
  177. r.Post("/collaboration", repo.CollaborationPost)
  178. r.Get("/hooks", repo.WebHooks)
  179. r.Get("/hooks/add", repo.WebHooksAdd)
  180. r.Post("/hooks/add", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksAddPost)
  181. r.Get("/hooks/:id", repo.WebHooksEdit)
  182. r.Post("/hooks/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  183. })
  184. }, reqSignIn, middleware.RepoAssignment(true), reqOwner)
  185. m.Group("/:username/:reponame", func(r martini.Router) {
  186. r.Get("/action/:action", repo.Action)
  187. m.Group("/issues", func(r martini.Router) {
  188. r.Get("/new", repo.CreateIssue)
  189. r.Post("/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
  190. r.Post("/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  191. r.Post("/:index/label", repo.UpdateIssueLabel)
  192. r.Post("/:index/milestone", repo.UpdateIssueMilestone)
  193. r.Post("/:index/assignee", repo.UpdateAssignee)
  194. r.Post("/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
  195. r.Post("/labels/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
  196. r.Post("/labels/delete", repo.DeleteLabel)
  197. r.Get("/milestones", repo.Milestones)
  198. r.Get("/milestones/new", repo.NewMilestone)
  199. r.Post("/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
  200. r.Get("/milestones/:index/edit", repo.UpdateMilestone)
  201. r.Post("/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost)
  202. r.Get("/milestones/:index/:action", repo.UpdateMilestone)
  203. })
  204. r.Post("/comment/:action", repo.Comment)
  205. r.Get("/releases/new", repo.NewRelease)
  206. r.Get("/releases/edit/:tagname", repo.EditRelease)
  207. }, reqSignIn, middleware.RepoAssignment(true))
  208. m.Group("/:username/:reponame", func(r martini.Router) {
  209. r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
  210. r.Post("/releases/edit/:tagname", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost)
  211. }, reqSignIn, middleware.RepoAssignment(true, true))
  212. m.Group("/:username/:reponame", func(r martini.Router) {
  213. r.Get("/issues", repo.Issues)
  214. r.Get("/issues/:index", repo.ViewIssue)
  215. r.Get("/pulls", repo.Pulls)
  216. r.Get("/branches", repo.Branches)
  217. }, ignSignIn, middleware.RepoAssignment(true))
  218. m.Group("/:username/:reponame", func(r martini.Router) {
  219. r.Get("/src/:branchname", repo.Single)
  220. r.Get("/src/:branchname/**", repo.Single)
  221. r.Get("/raw/:branchname/**", repo.SingleDownload)
  222. r.Get("/commits/:branchname", repo.Commits)
  223. r.Get("/commits/:branchname/search", repo.SearchCommits)
  224. r.Get("/commits/:branchname/**", repo.FileHistory)
  225. r.Get("/commit/:branchname", repo.Diff)
  226. r.Get("/commit/:branchname/**", repo.Diff)
  227. r.Get("/releases", repo.Releases)
  228. r.Get("/archive/:branchname/:reponame.zip", repo.ZipDownload)
  229. r.Get("/archive/:branchname/:reponame.tar.gz", repo.TarGzDownload)
  230. }, ignSignIn, middleware.RepoAssignment(true, true))
  231. m.Group("/:username", func(r martini.Router) {
  232. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  233. r.Any("/:reponame/**", repo.Http)
  234. }, ignSignInAndCsrf)
  235. // Not found handler.
  236. m.NotFound(routers.NotFound)
  237. var err error
  238. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  239. log.Info("Listen: %v://%s", setting.Protocol, listenAddr)
  240. switch setting.Protocol {
  241. case setting.HTTP:
  242. err = http.ListenAndServe(listenAddr, m)
  243. case setting.HTTPS:
  244. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  245. default:
  246. log.Fatal("Invalid protocol: %s", setting.Protocol)
  247. }
  248. if err != nil {
  249. log.Fatal("Fail to start server: %v", err)
  250. }
  251. }