Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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("/org", func(r martini.Router) {
  165. r.Get("/create", org.New)
  166. r.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.NewPost)
  167. r.Get("/:org", org.Organization)
  168. r.Get("/:org/dashboard", org.Dashboard)
  169. r.Get("/:org/members", org.Members)
  170. r.Get("/:org/teams/:team/edit", org.EditTeam)
  171. r.Get("/:org/teams/new", org.NewTeam)
  172. r.Get("/:org/teams", org.Teams)
  173. r.Get("/:org/settings", org.Settings)
  174. r.Post("/:org/settings", bindIgnErr(auth.OrgSettingForm{}), org.SettingsPost)
  175. r.Post("/:org/settings/delete", org.DeletePost)
  176. }, reqSignIn)
  177. m.Group("/:username/:reponame", func(r martini.Router) {
  178. r.Get("/settings", repo.Setting)
  179. r.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingPost)
  180. m.Group("/settings", func(r martini.Router) {
  181. r.Get("/collaboration", repo.Collaboration)
  182. r.Post("/collaboration", repo.CollaborationPost)
  183. r.Get("/hooks", repo.WebHooks)
  184. r.Get("/hooks/add", repo.WebHooksAdd)
  185. r.Post("/hooks/add", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksAddPost)
  186. r.Get("/hooks/:id", repo.WebHooksEdit)
  187. r.Post("/hooks/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  188. })
  189. }, reqSignIn, middleware.RepoAssignment(true), reqOwner)
  190. m.Group("/:username/:reponame", func(r martini.Router) {
  191. r.Get("/action/:action", repo.Action)
  192. m.Group("/issues", func(r martini.Router) {
  193. r.Get("/new", repo.CreateIssue)
  194. r.Post("/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
  195. r.Post("/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  196. r.Post("/:index/label", repo.UpdateIssueLabel)
  197. r.Post("/:index/milestone", repo.UpdateIssueMilestone)
  198. r.Post("/:index/assignee", repo.UpdateAssignee)
  199. r.Post("/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
  200. r.Post("/labels/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
  201. r.Post("/labels/delete", repo.DeleteLabel)
  202. r.Get("/milestones", repo.Milestones)
  203. r.Get("/milestones/new", repo.NewMilestone)
  204. r.Post("/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
  205. r.Get("/milestones/:index/edit", repo.UpdateMilestone)
  206. r.Post("/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost)
  207. r.Get("/milestones/:index/:action", repo.UpdateMilestone)
  208. })
  209. r.Post("/comment/:action", repo.Comment)
  210. r.Get("/releases/new", repo.NewRelease)
  211. r.Get("/releases/edit/:tagname", repo.EditRelease)
  212. }, reqSignIn, middleware.RepoAssignment(true))
  213. m.Group("/:username/:reponame", func(r martini.Router) {
  214. r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
  215. r.Post("/releases/edit/:tagname", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost)
  216. }, reqSignIn, middleware.RepoAssignment(true, true))
  217. m.Group("/:username/:reponame", func(r martini.Router) {
  218. r.Get("/issues", repo.Issues)
  219. r.Get("/issues/:index", repo.ViewIssue)
  220. r.Get("/pulls", repo.Pulls)
  221. r.Get("/branches", repo.Branches)
  222. }, ignSignIn, middleware.RepoAssignment(true))
  223. m.Group("/:username/:reponame", func(r martini.Router) {
  224. r.Get("/src/:branchname", repo.Single)
  225. r.Get("/src/:branchname/**", repo.Single)
  226. r.Get("/raw/:branchname/**", repo.SingleDownload)
  227. r.Get("/commits/:branchname", repo.Commits)
  228. r.Get("/commits/:branchname/search", repo.SearchCommits)
  229. r.Get("/commits/:branchname/**", repo.FileHistory)
  230. r.Get("/commit/:branchname", repo.Diff)
  231. r.Get("/commit/:branchname/**", repo.Diff)
  232. r.Get("/releases", repo.Releases)
  233. r.Get("/archive/:branchname/:reponame.zip", repo.ZipDownload)
  234. r.Get("/archive/:branchname/:reponame.tar.gz", repo.TarGzDownload)
  235. }, ignSignIn, middleware.RepoAssignment(true, true))
  236. m.Group("/:username", func(r martini.Router) {
  237. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  238. r.Any("/:reponame/**", repo.Http)
  239. }, ignSignInAndCsrf)
  240. // Not found handler.
  241. m.NotFound(routers.NotFound)
  242. var err error
  243. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  244. log.Info("Listen: %v://%s", setting.Protocol, listenAddr)
  245. switch setting.Protocol {
  246. case setting.HTTP:
  247. err = http.ListenAndServe(listenAddr, m)
  248. case setting.HTTPS:
  249. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  250. default:
  251. log.Fatal("Invalid protocol: %s", setting.Protocol)
  252. }
  253. if err != nil {
  254. log.Fatal("Fail to start server: %v", err)
  255. }
  256. }