web.go 2.8 KB

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