web.go 3.1 KB

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