web.go 3.3 KB

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