// Serve starts the horizon system, binding it to a socket, setting up // the shutdown signals and starting the appropriate db-streaming pumps. func (a *App) Serve() { a.web.router.Compile() http.Handle("/", a.web.router) listenStr := fmt.Sprintf(":%d", a.config.Port) listener := bind.Socket(listenStr) log.Infof(a.ctx, "Starting horizon on %s", listener.Addr()) graceful.HandleSignals() bind.Ready() graceful.PreHook(func() { log.Info(a.ctx, "received signal, gracefully stopping") a.Cancel() }) graceful.PostHook(func() { log.Info(a.ctx, "stopped") }) sse.SetPump(a.ctx, a.pump.Subscribe()) err := graceful.Serve(listener, http.DefaultServeMux) if err != nil { log.Panic(a.ctx, err) } graceful.Wait() }
// initLogglyLog attaches a loggly hook to our logging system. func initLogglyLog(app *App) { if app.config.LogglyToken == "" { return } log.Infof(app.ctx, "Initializing loggly hook to: %s host: %s", app.config.LogglyToken, app.config.LogglyHost) hook := log.NewLogglyHook(app.config.LogglyToken) app.log.Logger.Hooks.Add(hook) go func() { <-app.ctx.Done() hook.Flush() }() }
// Serve starts the horizon system, binding it to a socket, setting up // the shutdown signals and starting the appropriate db-streaming pumps. func (a *App) Serve() { a.web.router.Compile() http.Handle("/", a.web.router) addr := fmt.Sprintf(":%d", a.config.Port) srv := &graceful.Server{ Timeout: 10 * time.Second, Server: &http.Server{ Addr: addr, Handler: http.DefaultServeMux, }, ShutdownInitiated: func() { log.Info("received signal, gracefully stopping") a.Close() }, } http2.ConfigureServer(srv.Server, nil) sse.SetPump(a.pump.Subscribe()) log.Infof("Starting horizon on %s", addr) var err error if a.config.TLSCert != "" { err = srv.ListenAndServeTLS(a.config.TLSCert, a.config.TLSKey) } else { err = srv.ListenAndServe() } if err != nil { log.Panic(err) } log.Info("stopped") }
// initSentryLog attaches a hook to our logging system that will report // errors and panics to the configured sentry server (from Config.SentryDSN). func initSentryLog(app *App) { if app.config.SentryDSN == "" { return } log.Infof(app.ctx, "Initializing sentry hook to: %s", app.config.SentryDSN) hook, err := logrus_sentry.NewSentryHook(app.config.SentryDSN, []logrus.Level{ logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, }) hook.Timeout = 1 * time.Second if err != nil { panic(err) } app.log.Logger.Hooks.Add(hook) }