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

web.go 12KB

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