web.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2014 Unknwon
  2. // Copyright 2014 Torkel Ödegaard
  3. package cmd
  4. import (
  5. "fmt"
  6. "net/http"
  7. "path"
  8. "time"
  9. "github.com/Unknwon/macaron"
  10. "github.com/codegangsta/cli"
  11. "github.com/macaron-contrib/session"
  12. "github.com/torkelo/grafana-pro/pkg/api"
  13. "github.com/torkelo/grafana-pro/pkg/log"
  14. "github.com/torkelo/grafana-pro/pkg/middleware"
  15. "github.com/torkelo/grafana-pro/pkg/services/sqlstore"
  16. "github.com/torkelo/grafana-pro/pkg/setting"
  17. "github.com/torkelo/grafana-pro/pkg/social"
  18. )
  19. var CmdWeb = cli.Command{
  20. Name: "web",
  21. Usage: "grafana web",
  22. Description: "Starts Grafana backend & web server",
  23. Action: runWeb,
  24. Flags: []cli.Flag{
  25. cli.StringFlag{
  26. Name: "config",
  27. Value: "grafana.ini",
  28. Usage: "path to config file",
  29. },
  30. },
  31. }
  32. func newMacaron() *macaron.Macaron {
  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. log.Info("Starting Grafana")
  64. log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
  65. setting.NewConfigContext()
  66. social.NewOAuthService()
  67. sqlstore.NewEngine()
  68. sqlstore.EnsureAdminUser()
  69. m := newMacaron()
  70. api.Register(m)
  71. var err error
  72. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  73. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
  74. switch setting.Protocol {
  75. case setting.HTTP:
  76. err = http.ListenAndServe(listenAddr, m)
  77. case setting.HTTPS:
  78. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  79. default:
  80. log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
  81. }
  82. if err != nil {
  83. log.Fatal(4, "Fail to start server: %v", err)
  84. }
  85. }