web.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "github.com/Unknwon/macaron"
  13. "github.com/codegangsta/cli"
  14. "github.com/macaron-contrib/session"
  15. _ "github.com/macaron-contrib/session/mysql"
  16. _ "github.com/macaron-contrib/session/postgres"
  17. "github.com/grafana/grafana/pkg/api"
  18. "github.com/grafana/grafana/pkg/api/static"
  19. "github.com/grafana/grafana/pkg/log"
  20. "github.com/grafana/grafana/pkg/metrics"
  21. "github.com/grafana/grafana/pkg/middleware"
  22. "github.com/grafana/grafana/pkg/plugins"
  23. "github.com/grafana/grafana/pkg/services/eventpublisher"
  24. "github.com/grafana/grafana/pkg/setting"
  25. "github.com/grafana/grafana/pkg/social"
  26. )
  27. var Web = cli.Command{
  28. Name: "web",
  29. Usage: "Starts Grafana backend & web server",
  30. Description: "Starts Grafana backend & web server",
  31. Action: runWeb,
  32. }
  33. func newMacaron() *macaron.Macaron {
  34. macaron.Env = setting.Env
  35. m := macaron.New()
  36. m.Use(middleware.Logger())
  37. m.Use(macaron.Recovery())
  38. if setting.EnableGzip {
  39. m.Use(macaron.Gziper())
  40. }
  41. mapStatic(m, "", "public")
  42. mapStatic(m, "app", "app")
  43. mapStatic(m, "css", "css")
  44. mapStatic(m, "img", "img")
  45. mapStatic(m, "fonts", "fonts")
  46. m.Use(session.Sessioner(setting.SessionOptions))
  47. m.Use(macaron.Renderer(macaron.RenderOptions{
  48. Directory: path.Join(setting.StaticRootPath, "views"),
  49. IndentJSON: macaron.Env != macaron.PROD,
  50. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  51. }))
  52. m.Use(middleware.GetContextHandler())
  53. return m
  54. }
  55. func mapStatic(m *macaron.Macaron, dir string, prefix string) {
  56. headers := func(c *macaron.Context) {
  57. c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
  58. }
  59. if setting.Env == setting.DEV {
  60. headers = func(c *macaron.Context) {
  61. c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
  62. }
  63. }
  64. m.Use(httpstatic.Static(
  65. path.Join(setting.StaticRootPath, dir),
  66. httpstatic.StaticOptions{
  67. SkipLogging: true,
  68. Prefix: prefix,
  69. AddHeaders: headers,
  70. },
  71. ))
  72. }
  73. func runWeb(c *cli.Context) {
  74. initRuntime(c)
  75. writePIDFile(c)
  76. social.NewOAuthService()
  77. eventpublisher.Init()
  78. plugins.Init()
  79. var err error
  80. m := newMacaron()
  81. api.Register(m)
  82. if setting.ReportingEnabled {
  83. go metrics.StartUsageReportLoop()
  84. }
  85. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  86. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
  87. switch setting.Protocol {
  88. case setting.HTTP:
  89. err = http.ListenAndServe(listenAddr, m)
  90. case setting.HTTPS:
  91. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  92. default:
  93. log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
  94. }
  95. if err != nil {
  96. log.Fatal(4, "Fail to start server: %v", err)
  97. }
  98. }
  99. func writePIDFile(c *cli.Context) {
  100. path := c.GlobalString("pidfile")
  101. if path == "" {
  102. return
  103. }
  104. // Ensure the required directory structure exists.
  105. err := os.MkdirAll(filepath.Dir(path), 0700)
  106. if err != nil {
  107. log.Fatal(3, "Failed to verify pid directory", err)
  108. }
  109. // Retrieve the PID and write it.
  110. pid := strconv.Itoa(os.Getpid())
  111. if err := ioutil.WriteFile(path, []byte(pid), 0644); err != nil {
  112. log.Fatal(3, "Failed to write pidfile", err)
  113. }
  114. }