web.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/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. m.Use(macaron.Static(
  57. path.Join(setting.StaticRootPath, dir),
  58. macaron.StaticOptions{
  59. SkipLogging: true,
  60. Prefix: prefix,
  61. Expires: func() string {
  62. return time.Now().UTC().Format(http.TimeFormat)
  63. },
  64. },
  65. ))
  66. }
  67. func runWeb(c *cli.Context) {
  68. initRuntime(c)
  69. writePIDFile(c)
  70. social.NewOAuthService()
  71. eventpublisher.Init()
  72. plugins.Init()
  73. var err error
  74. m := newMacaron()
  75. api.Register(m)
  76. if setting.ReportingEnabled {
  77. go metrics.StartUsageReportLoop()
  78. }
  79. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  80. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
  81. switch setting.Protocol {
  82. case setting.HTTP:
  83. err = http.ListenAndServe(listenAddr, m)
  84. case setting.HTTPS:
  85. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  86. default:
  87. log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
  88. }
  89. if err != nil {
  90. log.Fatal(4, "Fail to start server: %v", err)
  91. }
  92. }
  93. func writePIDFile(c *cli.Context) {
  94. path := c.GlobalString("pidfile")
  95. if path == "" {
  96. return
  97. }
  98. // Ensure the required directory structure exists.
  99. err := os.MkdirAll(filepath.Dir(path), 0700)
  100. if err != nil {
  101. log.Fatal(3, "Failed to verify pid directory", err)
  102. }
  103. // Retrieve the PID and write it.
  104. pid := strconv.Itoa(os.Getpid())
  105. if err := ioutil.WriteFile(path, []byte(pid), 0644); err != nil {
  106. log.Fatal(3, "Failed to write pidfile", err)
  107. }
  108. }