web.go 3.3 KB

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