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.

166 lines
5.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "github.com/codegangsta/cli"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/binding"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/avatar"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/middleware"
  17. "github.com/gogits/gogs/routers"
  18. "github.com/gogits/gogs/routers/admin"
  19. "github.com/gogits/gogs/routers/api/v1"
  20. "github.com/gogits/gogs/routers/dev"
  21. "github.com/gogits/gogs/routers/repo"
  22. "github.com/gogits/gogs/routers/user"
  23. )
  24. var CmdWeb = cli.Command{
  25. Name: "web",
  26. Usage: "Gogs web server",
  27. Description: `
  28. gogs web server is the only thing you need to run,
  29. and it takes care of all the other things for you`,
  30. Action: runWeb,
  31. Flags: []cli.Flag{},
  32. }
  33. func newMartini() *martini.ClassicMartini {
  34. r := martini.NewRouter()
  35. m := martini.New()
  36. m.Use(middleware.Logger())
  37. m.Use(martini.Recovery())
  38. m.Use(martini.Static("public"))
  39. m.MapTo(r, (*martini.Routes)(nil))
  40. m.Action(r.Handle)
  41. return &martini.ClassicMartini{m, r}
  42. }
  43. func runWeb(*cli.Context) {
  44. fmt.Println("Server is running...")
  45. routers.GlobalInit()
  46. log.Info("%s %s", base.AppName, base.AppVer)
  47. m := newMartini()
  48. // Middlewares.
  49. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  50. m.Use(middleware.InitContext())
  51. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  52. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  53. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  54. // Routers.
  55. m.Get("/", ignSignIn, routers.Home)
  56. m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install)
  57. m.Get("/issues", reqSignIn, user.Issues)
  58. m.Get("/pulls", reqSignIn, user.Pulls)
  59. m.Get("/stars", reqSignIn, user.Stars)
  60. m.Get("/help", routers.Help)
  61. m.Group("/api/v1", func(r martini.Router) {
  62. r.Post("/markdown", v1.Markdown)
  63. })
  64. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  65. m.Get("/avatar/:hash", avt.ServeHTTP)
  66. m.Group("/user", func(r martini.Router) {
  67. r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  68. r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  69. }, reqSignOut)
  70. m.Group("/user", func(r martini.Router) {
  71. r.Any("/logout", user.SignOut)
  72. r.Any("/delete", user.Delete)
  73. r.Any("/setting", binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  74. }, reqSignIn)
  75. m.Group("/user", func(r martini.Router) {
  76. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  77. r.Get("/activate", user.Activate)
  78. })
  79. m.Group("/user/setting", func(r martini.Router) {
  80. r.Any("/password", binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  81. r.Any("/ssh", binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  82. r.Any("/notification", user.SettingNotification)
  83. r.Any("/security", user.SettingSecurity)
  84. }, reqSignIn)
  85. m.Get("/user/:username", ignSignIn, user.Profile)
  86. m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  87. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  88. m.Get("/admin", adminReq, admin.Dashboard)
  89. m.Group("/admin", func(r martini.Router) {
  90. r.Get("/users", admin.Users)
  91. r.Get("/repos", admin.Repositories)
  92. r.Get("/config", admin.Config)
  93. }, adminReq)
  94. m.Group("/admin/users", func(r martini.Router) {
  95. r.Any("/new", binding.BindIgnErr(auth.RegisterForm{}), admin.NewUser)
  96. r.Any("/:userid", binding.BindIgnErr(auth.AdminEditUserForm{}), admin.EditUser)
  97. r.Any("/:userid/delete", admin.DeleteUser)
  98. }, adminReq)
  99. if martini.Env == martini.Dev {
  100. m.Get("/template/**", dev.TemplatePreview)
  101. }
  102. m.Group("/:username/:reponame", func(r martini.Router) {
  103. r.Post("/settings", repo.SettingPost)
  104. r.Get("/settings", repo.Setting)
  105. r.Get("/action/:action", repo.Action)
  106. r.Any("/issues/new", binding.BindIgnErr(auth.CreateIssueForm{}), repo.CreateIssue)
  107. r.Post("/issues/:index", binding.BindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  108. r.Post("/comment/:action", repo.Comment)
  109. }, reqSignIn, middleware.RepoAssignment(true))
  110. m.Group("/:username/:reponame", func(r martini.Router) {
  111. r.Get("/issues", repo.Issues)
  112. r.Get("/issues/:index", repo.ViewIssue)
  113. r.Get("/pulls", repo.Pulls)
  114. r.Get("/branches", repo.Branches)
  115. }, ignSignIn, middleware.RepoAssignment(true))
  116. m.Group("/:username/:reponame", func(r martini.Router) {
  117. r.Get("/src/:branchname", repo.Single)
  118. r.Get("/src/:branchname/**", repo.Single)
  119. r.Get("/raw/:branchname/**", repo.SingleDownload)
  120. r.Get("/commits/:branchname", repo.Commits)
  121. r.Get("/commit/:branchname", repo.Diff)
  122. r.Get("/commit/:branchname/**", repo.Diff)
  123. }, ignSignIn, middleware.RepoAssignment(true, true))
  124. m.Group("/:username", func(r martini.Router) {
  125. r.Any("/:reponame/**", repo.Http)
  126. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  127. }, ignSignIn)
  128. // Not found handler.
  129. m.NotFound(routers.NotFound)
  130. listenAddr := fmt.Sprintf("%s:%s",
  131. base.Cfg.MustValue("server", "HTTP_ADDR"),
  132. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  133. log.Info("Listen: %s", listenAddr)
  134. if err := http.ListenAndServe(listenAddr, m); err != nil {
  135. fmt.Println(err.Error())
  136. //log.Critical(err.Error()) // not working now
  137. }
  138. }