web.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2014 Unknwon
  2. // Copyright 2014 Torkel Ödegaard
  3. package cmd
  4. import (
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. "github.com/Unknwon/macaron"
  14. "github.com/codegangsta/cli"
  15. "github.com/macaron-contrib/session"
  16. _ "github.com/macaron-contrib/session/mysql"
  17. _ "github.com/macaron-contrib/session/postgres"
  18. "github.com/grafana/grafana/pkg/api"
  19. "github.com/grafana/grafana/pkg/log"
  20. "github.com/grafana/grafana/pkg/middleware"
  21. "github.com/grafana/grafana/pkg/plugins"
  22. "github.com/grafana/grafana/pkg/services/eventpublisher"
  23. "github.com/grafana/grafana/pkg/setting"
  24. "github.com/grafana/grafana/pkg/social"
  25. )
  26. var Web = cli.Command{
  27. Name: "web",
  28. Usage: "Starts Grafana backend & web server",
  29. Description: "Starts Grafana backend & web server",
  30. Action: runWeb,
  31. }
  32. func newMacaron() *macaron.Macaron {
  33. macaron.Env = setting.Env
  34. m := macaron.New()
  35. m.Use(middleware.Logger())
  36. m.Use(macaron.Recovery())
  37. if setting.EnableGzip {
  38. m.Use(macaron.Gziper())
  39. }
  40. mapStatic(m, "", "public")
  41. mapStatic(m, "app", "app")
  42. mapStatic(m, "css", "css")
  43. mapStatic(m, "img", "img")
  44. mapStatic(m, "fonts", "fonts")
  45. m.Use(session.Sessioner(setting.SessionOptions))
  46. m.Use(macaron.Renderer(macaron.RenderOptions{
  47. Directory: path.Join(setting.StaticRootPath, "views"),
  48. IndentJSON: macaron.Env != macaron.PROD,
  49. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  50. }))
  51. m.Use(middleware.GetContextHandler())
  52. return m
  53. }
  54. func mapStatic(m *macaron.Macaron, dir string, prefix string) {
  55. m.Use(macaron.Static(
  56. path.Join(setting.StaticRootPath, dir),
  57. macaron.StaticOptions{
  58. SkipLogging: true,
  59. Prefix: prefix,
  60. Expires: func() string {
  61. return time.Now().UTC().Format(http.TimeFormat)
  62. },
  63. },
  64. ))
  65. }
  66. func runWeb(c *cli.Context) {
  67. initRuntime(c)
  68. writePIDFile(c)
  69. social.NewOAuthService()
  70. eventpublisher.Init()
  71. plugins.Init()
  72. var err error
  73. m := newMacaron()
  74. api.Register(m)
  75. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  76. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
  77. switch setting.Protocol {
  78. case setting.HTTP:
  79. err = http.ListenAndServe(listenAddr, m)
  80. case setting.HTTPS:
  81. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  82. default:
  83. log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
  84. }
  85. if err != nil {
  86. log.Fatal(4, "Fail to start server: %v", err)
  87. }
  88. }
  89. func writePIDFile(c *cli.Context) {
  90. path := c.GlobalString("pidfile")
  91. if path == "" {
  92. return
  93. }
  94. // Ensure the required directory structure exists.
  95. err := os.MkdirAll(filepath.Dir(path), 0700)
  96. if err != nil {
  97. log.Fatal(3, "Failed to verify pid directory", err)
  98. }
  99. // Retrieve the PID and write it.
  100. pid := strconv.Itoa(os.Getpid())
  101. if err := ioutil.WriteFile(path, []byte(pid), 0644); err != nil {
  102. log.Fatal(3, "Failed to write pidfile", err)
  103. }
  104. }