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.

web.go 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "str2html": base.Str2html,
  37. "TimeSince": base.TimeSince,
  38. "Subtract": base.Subtract,
  39. "ActionIcon": base.ActionIcon,
  40. "ActionDesc": base.ActionDesc,
  41. }
  42. func runWeb(*cli.Context) {
  43. log.Info("%s %s", base.Cfg.MustValue("", "APP_NAME"), APP_VER)
  44. m := martini.Classic()
  45. // Middlewares.
  46. m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
  47. m.Use(base.InitContext())
  48. // TODO: should use other store because cookie store is not secure.
  49. store := sessions.NewCookieStore([]byte("secret123"))
  50. m.Use(sessions.Sessions("my_session", store))
  51. // Routers.
  52. m.Get("/", auth.SignInRequire(false), routers.Home)
  53. m.Any("/user/login", auth.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  54. m.Any("/user/logout", auth.SignInRequire(true), user.SignOut)
  55. m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  56. m.Any("/user/delete", auth.SignInRequire(true), user.Delete)
  57. m.Get("/user/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  58. m.Any("/user/setting", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  59. m.Any("/user/setting/password", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  60. m.Any("/user/setting/ssh", auth.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  61. m.Any("/user/setting/notification", auth.SignInRequire(true), user.SettingNotification)
  62. m.Any("/user/setting/security", auth.SignInRequire(true), user.SettingSecurity)
  63. m.Get("/user/:username", auth.SignInRequire(false), user.Profile)
  64. m.Any("/repo/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  65. m.Any("/repo/delete", auth.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete)
  66. m.Any("/repo/list", auth.SignInRequire(false), repo.List)
  67. m.Get("/:username/:reponame/settings", auth.SignInRequire(false), auth.RepoAssignment(true), repo.Setting)
  68. m.Get("/:username/:reponame/tree/:branchname/**",
  69. auth.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
  70. m.Get("/:username/:reponame/tree/:branchname",
  71. auth.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
  72. m.Get("/:username/:reponame", auth.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
  73. //m.Get("/:username/:reponame", repo.Repo)
  74. listenAddr := fmt.Sprintf("%s:%s",
  75. base.Cfg.MustValue("server", "HTTP_ADDR"),
  76. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  77. log.Info("Listen: %s", listenAddr)
  78. http.ListenAndServe(listenAddr, m)
  79. }