server.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "time"
  8. "gopkg.in/macaron.v1"
  9. "golang.org/x/sync/errgroup"
  10. "github.com/grafana/grafana/pkg/api"
  11. "github.com/grafana/grafana/pkg/log"
  12. "github.com/grafana/grafana/pkg/login"
  13. "github.com/grafana/grafana/pkg/metrics"
  14. "github.com/grafana/grafana/pkg/models"
  15. "github.com/grafana/grafana/pkg/plugins"
  16. "github.com/grafana/grafana/pkg/services/alerting"
  17. "github.com/grafana/grafana/pkg/services/cleanup"
  18. "github.com/grafana/grafana/pkg/services/eventpublisher"
  19. "github.com/grafana/grafana/pkg/services/notifications"
  20. "github.com/grafana/grafana/pkg/services/search"
  21. "github.com/grafana/grafana/pkg/setting"
  22. "github.com/grafana/grafana/pkg/social"
  23. )
  24. func NewGrafanaServer() models.GrafanaServer {
  25. rootCtx, shutdownFn := context.WithCancel(context.Background())
  26. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  27. return &GrafanaServerImpl{
  28. context: childCtx,
  29. shutdownFn: shutdownFn,
  30. childRoutines: childRoutines,
  31. log: log.New("server"),
  32. }
  33. }
  34. type GrafanaServerImpl struct {
  35. context context.Context
  36. shutdownFn context.CancelFunc
  37. childRoutines *errgroup.Group
  38. log log.Logger
  39. }
  40. func (g *GrafanaServerImpl) Start() {
  41. go listenToSystemSignals(g)
  42. writePIDFile()
  43. initRuntime()
  44. initSql()
  45. metrics.Init()
  46. search.Init()
  47. login.Init()
  48. social.NewOAuthService()
  49. eventpublisher.Init()
  50. plugins.Init()
  51. // init alerting
  52. if setting.AlertingEnabled && setting.ExecuteAlerts {
  53. engine := alerting.NewEngine()
  54. g.childRoutines.Go(func() error { return engine.Run(g.context) })
  55. }
  56. // cleanup service
  57. cleanUpService := cleanup.NewCleanUpService()
  58. g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
  59. if err := notifications.Init(); err != nil {
  60. g.log.Error("Notification service failed to initialize", "erro", err)
  61. g.Shutdown(1, "Startup failed")
  62. return
  63. }
  64. g.startHttpServer()
  65. }
  66. func (g *GrafanaServerImpl) startHttpServer() {
  67. logger = log.New("http.server")
  68. var err error
  69. m := newMacaron()
  70. api.Register(m)
  71. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  72. g.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl)
  73. switch setting.Protocol {
  74. case setting.HTTP:
  75. err = http.ListenAndServe(listenAddr, m)
  76. case setting.HTTPS:
  77. err = ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  78. default:
  79. g.log.Error("Invalid protocol", "protocol", setting.Protocol)
  80. g.Shutdown(1, "Startup failed")
  81. }
  82. if err != nil {
  83. g.log.Error("Fail to start server", "error", err)
  84. g.Shutdown(1, "Startup failed")
  85. return
  86. }
  87. }
  88. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  89. g.log.Info("Shutdown started", "code", code, "reason", reason)
  90. g.shutdownFn()
  91. err := g.childRoutines.Wait()
  92. g.log.Info("Shutdown completed", "reason", err)
  93. log.Close()
  94. os.Exit(code)
  95. }
  96. func ListenAndServeTLS(listenAddr, certfile, keyfile string, m *macaron.Macaron) error {
  97. if certfile == "" {
  98. return fmt.Errorf("cert_file cannot be empty when using HTTPS")
  99. }
  100. if keyfile == "" {
  101. return fmt.Errorf("cert_key cannot be empty when using HTTPS")
  102. }
  103. if _, err := os.Stat(setting.CertFile); os.IsNotExist(err) {
  104. return fmt.Errorf(`Cannot find SSL cert_file at %v`, setting.CertFile)
  105. }
  106. if _, err := os.Stat(setting.KeyFile); os.IsNotExist(err) {
  107. return fmt.Errorf(`Cannot find SSL key_file at %v`, setting.KeyFile)
  108. }
  109. return http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  110. }
  111. // implement context.Context
  112. func (g *GrafanaServerImpl) Deadline() (deadline time.Time, ok bool) {
  113. return g.context.Deadline()
  114. }
  115. func (g *GrafanaServerImpl) Done() <-chan struct{} {
  116. return g.context.Done()
  117. }
  118. func (g *GrafanaServerImpl) Err() error {
  119. return g.context.Err()
  120. }
  121. func (g *GrafanaServerImpl) Value(key interface{}) interface{} {
  122. return g.context.Value(key)
  123. }