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.

82 lines
2.5 KiB

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