mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
create app struct
This commit is contained in:
@@ -84,6 +84,15 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type showbridgeApp struct {
|
||||||
|
ctx context.Context
|
||||||
|
configPath string
|
||||||
|
logger *slog.Logger
|
||||||
|
router *showbridge.Router
|
||||||
|
routerRunner *sync.WaitGroup
|
||||||
|
tracer trace.Tracer
|
||||||
|
}
|
||||||
|
|
||||||
func readConfig(configPath string) (config.Config, error) {
|
func readConfig(configPath string) (config.Config, error) {
|
||||||
cfg := config.Config{}
|
cfg := config.Config{}
|
||||||
|
|
||||||
@@ -104,12 +113,7 @@ func readConfig(configPath string) (config.Config, error) {
|
|||||||
func run(ctx context.Context, c *cli.Command) error {
|
func run(ctx context.Context, c *cli.Command) error {
|
||||||
configPath := c.String("config")
|
configPath := c.String("config")
|
||||||
if configPath == "" {
|
if configPath == "" {
|
||||||
return errors.New("config value cannot be empty")
|
return errors.New("config path cannot be empty")
|
||||||
}
|
|
||||||
|
|
||||||
config, err := readConfig(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logLevel := slog.LevelInfo
|
logLevel := slog.LevelInfo
|
||||||
@@ -150,8 +154,6 @@ func run(ctx context.Context, c *cli.Command) error {
|
|||||||
|
|
||||||
slog.SetDefault(slog.New(logHandler))
|
slog.SetDefault(slog.New(logHandler))
|
||||||
|
|
||||||
commandLogger := slog.Default().With("component", "cmd")
|
|
||||||
|
|
||||||
var tracer trace.Tracer
|
var tracer trace.Tracer
|
||||||
if c.Bool("trace") {
|
if c.Bool("trace") {
|
||||||
exporter, err := otlptracehttp.New(ctx)
|
exporter, err := otlptracehttp.New(ctx)
|
||||||
@@ -168,64 +170,80 @@ func run(ctx context.Context, c *cli.Command) error {
|
|||||||
tracer = otel.Tracer("showbridge")
|
tracer = otel.Tracer("showbridge")
|
||||||
}
|
}
|
||||||
|
|
||||||
router, moduleErrors, routeErrors := showbridge.NewRouter(config, tracer)
|
showbridgeApp := &showbridgeApp{
|
||||||
|
ctx: ctx,
|
||||||
for _, moduleError := range moduleErrors {
|
configPath: configPath,
|
||||||
commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
logger: slog.Default().With("component", "cmd"),
|
||||||
|
routerRunner: &sync.WaitGroup{},
|
||||||
|
tracer: tracer,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, routeError := range routeErrors {
|
router, err := showbridgeApp.getNewRouter()
|
||||||
commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to initialize router: %w", err)
|
||||||
}
|
}
|
||||||
|
showbridgeApp.router = router
|
||||||
|
|
||||||
routerRunner := sync.WaitGroup{}
|
showbridgeApp.routerRunner.Go(func() {
|
||||||
|
|
||||||
routerRunner.Go(func() {
|
|
||||||
router.Start(context.Background())
|
router.Start(context.Background())
|
||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
go showbridgeApp.handleHangup()
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-sigHangup:
|
|
||||||
commandLogger.Info("received SIGHUP, reloading configuration")
|
|
||||||
newConfig, err := readConfig(configPath)
|
|
||||||
if err != nil {
|
|
||||||
commandLogger.Error("error reading config file", "error", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
newRouter, moduleErrors, routeErrors := showbridge.NewRouter(newConfig, tracer)
|
|
||||||
|
|
||||||
for _, moduleError := range moduleErrors {
|
<-showbridgeApp.ctx.Done()
|
||||||
commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
showbridgeApp.logger.Debug("shutting down router")
|
||||||
}
|
showbridgeApp.router.Stop()
|
||||||
|
showbridgeApp.logger.Debug("waiting for router to exit")
|
||||||
for _, routeError := range routeErrors {
|
showbridgeApp.routerRunner.Wait()
|
||||||
commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
if moduleErrors == nil && routeErrors == nil {
|
|
||||||
router.Stop()
|
|
||||||
routerRunner.Wait()
|
|
||||||
router = newRouter
|
|
||||||
routerRunner.Go(func() {
|
|
||||||
router.Start(context.Background())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-ctx.Done()
|
|
||||||
commandLogger.Debug("shutting down router")
|
|
||||||
router.Stop()
|
|
||||||
commandLogger.Debug("waiting for router to exit")
|
|
||||||
routerRunner.Wait()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *showbridgeApp) handleHangup() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sigHangup:
|
||||||
|
app.logger.Info("received SIGHUP, reloading configuration")
|
||||||
|
newRouter, err := app.getNewRouter()
|
||||||
|
if err != nil {
|
||||||
|
app.logger.Error("failed to reload configuration", "error", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
app.router.Stop()
|
||||||
|
app.routerRunner.Wait()
|
||||||
|
app.router = newRouter
|
||||||
|
app.routerRunner.Go(func() {
|
||||||
|
app.router.Start(context.Background())
|
||||||
|
})
|
||||||
|
app.logger.Info("configuration reloaded successfully")
|
||||||
|
case <-app.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *showbridgeApp) getNewRouter() (*showbridge.Router, error) {
|
||||||
|
config, err := readConfig(app.configPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
router, moduleErrors, routeErrors := showbridge.NewRouter(config, app.tracer)
|
||||||
|
|
||||||
|
for _, moduleError := range moduleErrors {
|
||||||
|
app.logger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, routeError := range routeErrors {
|
||||||
|
app.logger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if moduleErrors != nil || routeErrors != nil {
|
||||||
|
return nil, fmt.Errorf("errors initializing modules or routes")
|
||||||
|
}
|
||||||
|
|
||||||
|
return router, nil
|
||||||
|
}
|
||||||
|
|
||||||
func newTracerProvider(exp sdktrace.SpanExporter) *sdktrace.TracerProvider {
|
func newTracerProvider(exp sdktrace.SpanExporter) *sdktrace.TracerProvider {
|
||||||
r, err := resource.Merge(
|
r, err := resource.Merge(
|
||||||
resource.Default(),
|
resource.Default(),
|
||||||
|
|||||||
Reference in New Issue
Block a user