web.go 2.9 KB

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