web.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/routers"
  17. "github.com/gogits/gogs/routers/repo"
  18. "github.com/gogits/gogs/routers/user"
  19. "github.com/gogits/gogs/utils"
  20. "github.com/gogits/gogs/utils/log"
  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. //cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
  30. //cli.BoolFlag{"verbose, v", "show process details"},
  31. },
  32. }
  33. var AppHelpers template.FuncMap = map[string]interface{}{
  34. "AppName": func() string {
  35. return utils.Cfg.MustValue("", "APP_NAME")
  36. },
  37. }
  38. func runWeb(*cli.Context) {
  39. log.Info("%s %s", utils.Cfg.MustValue("", "APP_NAME"), APP_VER)
  40. m := martini.Classic()
  41. // Middleware.
  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("/", routers.Home)
  49. m.Any("/user/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  50. m.Any("/user/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  51. m.Get("/user/profile", user.Profile) // should be /username
  52. m.Any("/user/delete", user.Delete)
  53. m.Any("/user/publickey/add", user.AddPublicKey)
  54. m.Any("/repo/create", repo.Create)
  55. m.Any("/repo/delete", repo.Delete)
  56. listenAddr := fmt.Sprintf("%s:%s",
  57. utils.Cfg.MustValue("server", "HTTP_ADDR"),
  58. utils.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  59. log.Info("Listen: %s", listenAddr)
  60. http.ListenAndServe(listenAddr, m)
  61. }