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