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.

136 lines
4.8 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
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. "strings"
  10. "github.com/codegangsta/cli"
  11. "github.com/codegangsta/martini"
  12. "github.com/martini-contrib/sessions"
  13. "github.com/gogits/binding"
  14. "github.com/gogits/gogs/modules/auth"
  15. "github.com/gogits/gogs/modules/base"
  16. "github.com/gogits/gogs/modules/log"
  17. "github.com/gogits/gogs/modules/middleware"
  18. "github.com/gogits/gogs/routers"
  19. "github.com/gogits/gogs/routers/admin"
  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: "just run",
  27. Description: `
  28. gogs web`,
  29. Action: runWeb,
  30. Flags: []cli.Flag{},
  31. }
  32. // Check run mode(Default of martini is Dev).
  33. func checkRunMode() {
  34. switch base.Cfg.MustValue("", "RUN_MODE") {
  35. case "prod":
  36. martini.Env = martini.Prod
  37. case "test":
  38. martini.Env = martini.Test
  39. }
  40. log.Info("Run Mode: %s", strings.Title(martini.Env))
  41. }
  42. func newMartini() *martini.ClassicMartini {
  43. r := martini.NewRouter()
  44. m := martini.New()
  45. m.Use(middleware.Logger())
  46. m.Use(martini.Recovery())
  47. m.Use(martini.Static("public"))
  48. m.MapTo(r, (*martini.Routes)(nil))
  49. m.Action(r.Handle)
  50. return &martini.ClassicMartini{m, r}
  51. }
  52. func runWeb(*cli.Context) {
  53. base.NewServices()
  54. checkRunMode()
  55. log.Info("%s %s", base.AppName, base.AppVer)
  56. m := newMartini()
  57. // Middlewares.
  58. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  59. // TODO: should use other store because cookie store is not secure.
  60. store := sessions.NewCookieStore([]byte("secret123"))
  61. m.Use(sessions.Sessions("my_session", store))
  62. m.Use(middleware.InitContext())
  63. reqSignIn, ignSignIn := middleware.SignInRequire(true), middleware.SignInRequire(false)
  64. reqSignOut := middleware.SignOutRequire()
  65. // Routers.
  66. m.Get("/", ignSignIn, routers.Home)
  67. m.Get("/issues", reqSignIn, user.Issues)
  68. m.Get("/pulls", reqSignIn, user.Pulls)
  69. m.Get("/stars", reqSignIn, user.Stars)
  70. m.Any("/user/login", reqSignOut, binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  71. m.Any("/user/logout", reqSignIn, user.SignOut)
  72. m.Any("/user/sign_up", reqSignOut, binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  73. m.Any("/user/delete", reqSignIn, user.Delete)
  74. m.Get("/user/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  75. m.Get("/user/activate", user.Activate)
  76. m.Any("/user/setting", reqSignIn, binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  77. m.Any("/user/setting/password", reqSignIn, binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  78. m.Any("/user/setting/ssh", reqSignIn, binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  79. m.Any("/user/setting/notification", reqSignIn, user.SettingNotification)
  80. m.Any("/user/setting/security", reqSignIn, user.SettingSecurity)
  81. m.Get("/user/:username", ignSignIn, user.Profile)
  82. m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  83. m.Get("/help", routers.Help)
  84. adminReq := middleware.AdminRequire()
  85. m.Any("/admin", reqSignIn, adminReq, admin.Dashboard)
  86. m.Any("/admin/users", reqSignIn, adminReq, admin.Users)
  87. m.Any("/admin/repos", reqSignIn, adminReq, admin.Repositories)
  88. m.Post("/:username/:reponame/settings", reqSignIn, middleware.RepoAssignment(true), repo.SettingPost)
  89. m.Get("/:username/:reponame/settings", reqSignIn, middleware.RepoAssignment(true), repo.Setting)
  90. m.Get("/:username/:reponame/commits/:branchname", ignSignIn, middleware.RepoAssignment(true), repo.Commits)
  91. m.Get("/:username/:reponame/issues", ignSignIn, middleware.RepoAssignment(true), repo.Issues)
  92. m.Get("/:username/:reponame/pulls", ignSignIn, middleware.RepoAssignment(true), repo.Pulls)
  93. m.Get("/:username/:reponame/branches", ignSignIn, middleware.RepoAssignment(true), repo.Branches)
  94. m.Get("/:username/:reponame/action/:action", reqSignIn, middleware.RepoAssignment(true), repo.Action)
  95. m.Get("/:username/:reponame/src/:branchname/**",
  96. ignSignIn, middleware.RepoAssignment(true), repo.Single)
  97. m.Get("/:username/:reponame/src/:branchname",
  98. ignSignIn, middleware.RepoAssignment(true), repo.Single)
  99. m.Get("/:username/:reponame/commit/:commitid/**", ignSignIn, middleware.RepoAssignment(true), repo.Single)
  100. m.Get("/:username/:reponame/commit/:commitid", ignSignIn, middleware.RepoAssignment(true), repo.Single)
  101. m.Get("/:username/:reponame", ignSignIn, middleware.RepoAssignment(true), repo.Single)
  102. if martini.Env == martini.Dev {
  103. m.Get("/template/**", dev.TemplatePreview)
  104. }
  105. listenAddr := fmt.Sprintf("%s:%s",
  106. base.Cfg.MustValue("server", "HTTP_ADDR"),
  107. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  108. log.Info("Listen: %s", listenAddr)
  109. if err := http.ListenAndServe(listenAddr, m); err != nil {
  110. log.Critical(err.Error())
  111. }
  112. }