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/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. //cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
  29. //cli.BoolFlag{"verbose, v", "show process details"},
  30. },
  31. }
  32. var AppHelpers template.FuncMap = map[string]interface{}{
  33. "AppName": func() string {
  34. return base.Cfg.MustValue("", "APP_NAME")
  35. },
  36. "AppVer": func() string {
  37. return APP_VER
  38. },
  39. }
  40. func runWeb(*cli.Context) {
  41. log.Info("%s %s", base.Cfg.MustValue("", "APP_NAME"), APP_VER)
  42. m := martini.Classic()
  43. // Middlewares.
  44. m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
  45. m.Use(base.InitContext())
  46. // TODO: should use other store because cookie store is not secure.
  47. store := sessions.NewCookieStore([]byte("secret123"))
  48. m.Use(sessions.Sessions("my_session", store))
  49. // Routers.
  50. m.Get("/", auth.SignInRequire(false), routers.Home)
  51. m.Any("/user/login", auth.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  52. m.Any("/user/logout", auth.SignInRequire(true), user.SignOut)
  53. m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  54. m.Any("/user/delete", auth.SignInRequire(true), user.Delete)
  55. m.Any("/user/setting", auth.SignInRequire(true), user.Setting)
  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), repo.Delete)
  60. m.Any("/repo/list", auth.SignInRequire(false), repo.List)
  61. listenAddr := fmt.Sprintf("%s:%s",
  62. base.Cfg.MustValue("server", "HTTP_ADDR"),
  63. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  64. log.Info("Listen: %s", listenAddr)
  65. http.ListenAndServe(listenAddr, m)
  66. }