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.

90 lines
3.7 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
  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/codegangsta/martini"
  11. "github.com/martini-contrib/render"
  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/repo"
  20. "github.com/gogits/gogs/routers/user"
  21. )
  22. var CmdWeb = cli.Command{
  23. Name: "web",
  24. Usage: "just run",
  25. Description: `
  26. gogs web`,
  27. Action: runWeb,
  28. Flags: []cli.Flag{},
  29. }
  30. func runWeb(*cli.Context) {
  31. log.Info("%s %s", base.AppName, base.AppVer)
  32. m := martini.Classic()
  33. // Middlewares.
  34. m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  35. // TODO: should use other store because cookie store is not secure.
  36. store := sessions.NewCookieStore([]byte("secret123"))
  37. m.Use(sessions.Sessions("my_session", store))
  38. m.Use(middleware.InitContext())
  39. // Routers.
  40. m.Get("/", middleware.SignInRequire(false), routers.Home)
  41. m.Get("/issues", middleware.SignInRequire(true), user.Issues)
  42. m.Get("/pulls", middleware.SignInRequire(true), user.Pulls)
  43. m.Get("/stars", middleware.SignInRequire(true), user.Stars)
  44. m.Any("/user/login", middleware.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  45. m.Any("/user/logout", middleware.SignInRequire(true), user.SignOut)
  46. m.Any("/user/sign_up", middleware.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  47. m.Any("/user/delete", middleware.SignInRequire(true), user.Delete)
  48. m.Get("/user/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  49. m.Any("/user/setting", middleware.SignInRequire(true), binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  50. m.Any("/user/setting/password", middleware.SignInRequire(true), binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  51. m.Any("/user/setting/ssh", middleware.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  52. m.Any("/user/setting/notification", middleware.SignInRequire(true), user.SettingNotification)
  53. m.Any("/user/setting/security", middleware.SignInRequire(true), user.SettingSecurity)
  54. m.Get("/user/:username", middleware.SignInRequire(false), user.Profile)
  55. m.Any("/repo/create", middleware.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  56. m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete)
  57. m.Get("/help", routers.Help)
  58. m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Setting)
  59. m.Get("/:username/:reponame/commits", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Commits)
  60. m.Get("/:username/:reponame/issues", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Issues)
  61. m.Get("/:username/:reponame/pulls", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Pulls)
  62. m.Get("/:username/:reponame/tree/:branchname/**",
  63. middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
  64. m.Get("/:username/:reponame/tree/:branchname",
  65. middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
  66. m.Get("/:username/:reponame", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
  67. listenAddr := fmt.Sprintf("%s:%s",
  68. base.Cfg.MustValue("server", "HTTP_ADDR"),
  69. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  70. log.Info("Listen: %s", listenAddr)
  71. http.ListenAndServe(listenAddr, m)
  72. }