mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
control flow of router running with better context handling and logging in cmd
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go"
|
"github.com/jwetzell/showbridge-go"
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
@@ -39,7 +40,37 @@ func main() {
|
|||||||
Usage: "log using JSON",
|
Usage: "log using JSON",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
Action: run,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
|
defer cancel()
|
||||||
|
err := cmd.Run(ctx, os.Args)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func readConfig(configPath string) (config.Config, error) {
|
||||||
|
cfg := config.Config{}
|
||||||
|
|
||||||
|
configBytes, err := os.ReadFile(configPath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(configBytes, &cfg)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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 value cannot be empty")
|
||||||
@@ -68,47 +99,32 @@ func main() {
|
|||||||
logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions)
|
logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := slog.New(logHandler)
|
slog.SetDefault(slog.New(logHandler))
|
||||||
|
|
||||||
slog.SetDefault(logger)
|
commandLogger := slog.Default().With("component", "cmd")
|
||||||
|
|
||||||
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
|
routerContext, routerCancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
router, moduleErrors, routeErrors := showbridge.NewRouter(routerContext, config)
|
||||||
|
|
||||||
for _, moduleError := range moduleErrors {
|
for _, moduleError := range moduleErrors {
|
||||||
logger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, routeError := range routeErrors {
|
for _, routeError := range routeErrors {
|
||||||
logger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
routerRunner := sync.WaitGroup{}
|
||||||
|
|
||||||
|
routerRunner.Go(func() {
|
||||||
router.Run()
|
router.Run()
|
||||||
|
})
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
commandLogger.Debug("shutting down router")
|
||||||
|
routerCancel()
|
||||||
|
commandLogger.Debug("waiting for router to exit")
|
||||||
|
routerRunner.Wait()
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Interrupt)
|
|
||||||
defer cancel()
|
|
||||||
err := cmd.Run(ctx, os.Args)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func readConfig(configPath string) (config.Config, error) {
|
|
||||||
cfg := config.Config{}
|
|
||||||
|
|
||||||
configBytes, err := os.ReadFile(configPath)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return config.Config{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = yaml.Unmarshal(configBytes, &cfg)
|
|
||||||
if err != nil {
|
|
||||||
return config.Config{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user