web.go 2.8 KB

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