server.go 3.2 KB

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