64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
lib "git.sr.ht/~cco/go-scopes"
|
|
"git.sr.ht/~cco/go-scopes/config"
|
|
"git.sr.ht/~cco/go-scopes/core/context"
|
|
"git.sr.ht/~cco/go-scopes/core/message"
|
|
"git.sr.ht/~cco/go-scopes/logging"
|
|
)
|
|
|
|
type Cfg struct {
|
|
*config.BaseCfg
|
|
Home string
|
|
Logging *logging.Cfg
|
|
}
|
|
|
|
func Start(ctx lib.Context) {
|
|
lib.RunCtx(ctx, start)
|
|
}
|
|
|
|
func start(ctx lib.Context) {
|
|
defer ctx.Stop()
|
|
for _, cfg := range ctx.Config().Children() {
|
|
cctx := ctx.ChildContext(cfg)
|
|
cfg.Starter()(cctx)
|
|
}
|
|
logging.Info(ctx).Msg("app.start: running")
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
for step(ctx, sig) {
|
|
}
|
|
}
|
|
|
|
func step(ctx lib.Context, sig <-chan os.Signal) bool {
|
|
select {
|
|
case <-sig:
|
|
logging.Info(ctx).Msg("app.step: interrupted")
|
|
return false
|
|
case msg := <-ctx.Mailbox():
|
|
//log.Debug().Str("msg.action", msg.Action()).Msg("app.step: message")
|
|
if msg == message.Quit {
|
|
logging.Info(ctx).Msg("app.step: stopped")
|
|
return false
|
|
}
|
|
return lib.HandleMsg(ctx, msg)
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// set up and start the application
|
|
|
|
func RunApp(cfg lib.Config) {
|
|
ctx := context.AppContext(cfg)
|
|
appCfg := cfg.(*Cfg)
|
|
logging.Setup(ctx, appCfg.Logging, appCfg.Home)
|
|
cfg.Starter()(ctx)
|
|
ctx.WaitGroup().Wait()
|
|
}
|