web.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/gogs/routers"
  14. "github.com/gogits/gogs/routers/repo"
  15. "github.com/gogits/gogs/routers/user"
  16. "github.com/gogits/gogs/utils"
  17. "github.com/gogits/gogs/utils/log"
  18. )
  19. var CmdWeb = cli.Command{
  20. Name: "web",
  21. Usage: "just run",
  22. Description: `
  23. gogs web`,
  24. Action: runWeb,
  25. Flags: []cli.Flag{
  26. //cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
  27. //cli.BoolFlag{"verbose, v", "show process details"},
  28. },
  29. }
  30. var AppHelpers template.FuncMap = map[string]interface{}{
  31. "AppName": func() string {
  32. return utils.Cfg.MustValue("", "APP_NAME")
  33. },
  34. }
  35. func runWeb(*cli.Context) {
  36. log.Info("%s %s", utils.Cfg.MustValue("", "APP_NAME"), APP_VER)
  37. m := martini.Classic()
  38. // Middleware.
  39. m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
  40. // TODO: should use other store because cookie store is not secure.
  41. store := sessions.NewCookieStore([]byte("secret123"))
  42. m.Use(sessions.Sessions("my_session", store))
  43. // Routers.
  44. m.Get("/", routers.Dashboard)
  45. m.Any("/login", user.SignIn)
  46. m.Any("/user/signin", user.SignIn)
  47. m.Any("/sign-up", user.SignUp)
  48. m.Any("/user/signup", user.SignUp)
  49. m.Get("/user/profile", user.Profile) // should be /username
  50. m.Any("/user/delete", user.Delete)
  51. m.Any("/user/publickey/add", user.AddPublicKey)
  52. m.Any("/repo/create", repo.Create)
  53. m.Any("/repo/delete", repo.Delete)
  54. listenAddr := fmt.Sprintf("%s:%s",
  55. utils.Cfg.MustValue("server", "HTTP_ADDR"),
  56. utils.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  57. log.Info("Listen: %s", listenAddr)
  58. http.ListenAndServe(listenAddr, m)
  59. }