From aec3446d751fcaa397b9ca100523c6d114f399e0 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 4 Jun 2023 11:19:55 +0200 Subject: [PATCH] provide separate app package for top-level appllication component --- app/app.go | 53 +++++++++++++++++++++++++++++++++++++++ examples/demo/etc/etc.go | 6 ++--- examples/demo/main.go | 3 ++- scopes.go | 54 +--------------------------------------- testing/testing.go | 4 +-- tests/etc/etc.go | 4 +-- tests/scopes_test.go | 4 +-- 7 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 app/app.go diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..0d7a0d9 --- /dev/null +++ b/app/app.go @@ -0,0 +1,53 @@ +package app + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "git.sr.ht/~cco/go-scopes/config" + "git.sr.ht/~cco/go-scopes/lib" +) + +type Cfg struct { + *config.Base + AppType string + Home string +} + +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) + fmt.Println(cfg.Name(), " started") + } + fmt.Println("running ", ctx.WaitGroup()) + 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: + fmt.Println("interrupted") + //ctx.LogInfo("Dispatcher interrupted", m.Map{}) + return false + case msg := <-ctx.Mailbox(): + fmt.Println("message", msg) + //ctx.LogDebug("dispatcherStep", m.Map{"msg": msg}) + if msg == "quit" { + //ctx.LogInfo("Dispatcher stopped", m.Map{}) + return false + } + //ctx.HandleMsg(&msg) + } + return true +} diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index 7663b51..8094c23 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -1,7 +1,7 @@ package etc import ( - "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" ) @@ -9,8 +9,8 @@ import ( func Config() lib.Config { ovr := Overrides().Use b := config.MakeBase - cfg := scopes.Cfg{ - Base: b("dummy", scopes.Start), + cfg := app.Cfg{ + Base: b("dummy", app.Start), Home: ovr(".", HOME), AppType: "standard", } diff --git a/examples/demo/main.go b/examples/demo/main.go index 610e882..3283ef5 100644 --- a/examples/demo/main.go +++ b/examples/demo/main.go @@ -6,12 +6,13 @@ import ( "demo/etc" "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/app" ) func main() { cfg := etc.Config() fmt.Println(cfg.Name()) - appCfg := cfg.(*scopes.Cfg) + appCfg := cfg.(*app.Cfg) fmt.Println(appCfg.Home) scopes.RunApp(cfg) } diff --git a/scopes.go b/scopes.go index 3c7ef66..491b420 100644 --- a/scopes.go +++ b/scopes.go @@ -1,58 +1,6 @@ package scopes -import ( - "fmt" - "os" - "os/signal" - "syscall" - - "git.sr.ht/~cco/go-scopes/config" - "git.sr.ht/~cco/go-scopes/lib" -) - -type Cfg struct { - *config.Base - AppType string - Home string -} - -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) - fmt.Println(cfg.Name(), " started") - } - fmt.Println("running ", ctx.WaitGroup()) - 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: - fmt.Println("interrupted") - //ctx.LogInfo("Dispatcher interrupted", m.Map{}) - return false - case msg := <-ctx.Mailbox(): - fmt.Println("message", msg) - //ctx.LogDebug("dispatcherStep", m.Map{"msg": msg}) - if msg == "quit" { - //ctx.LogInfo("Dispatcher stopped", m.Map{}) - return false - } - //ctx.HandleMsg(&msg) - } - return true -} - -// definitions +import "git.sr.ht/~cco/go-scopes/lib" func RunApp(cfg lib.Config) { ctx := lib.AppContext(cfg) diff --git a/testing/testing.go b/testing/testing.go index 146433f..a297a91 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -8,12 +8,12 @@ import ( "testing" "time" - "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/lib" ) func Start(ctx lib.Context) { - scopes.Start(ctx) + app.Start(ctx) } // definitions diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 536ec63..5766d1b 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -1,7 +1,7 @@ package etc import ( - "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/testing" @@ -10,7 +10,7 @@ import ( func Config() lib.Config { ovr := Overrides().Use b := config.MakeBase - cfg := scopes.Cfg{ + cfg := app.Cfg{ Base: b("testing", testing.Start), Home: ovr(".", HOME), AppType: "standard", diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 97ed977..386cb34 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -4,7 +4,7 @@ import ( "fmt" tbase "testing" - "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/tests/etc" ) @@ -24,7 +24,7 @@ func TestingTest(t *testing.T) { func ConfigTest(t *testing.T) { cfg := etc.Config() t.AssertEqual(cfg.Name(), "testing") - appCfg := cfg.(*scopes.Cfg) + appCfg := cfg.(*app.Cfg) t.AssertEqual(appCfg.Home, "tests") confCtx := t.Ctx.Services()["config"] t.AssertEqual(confCtx.Config().Name(), "config")