provide separate app package for top-level appllication component

This commit is contained in:
Helmut Merz 2023-06-04 11:19:55 +02:00
parent 2f1efc9731
commit aec3446d75
7 changed files with 65 additions and 63 deletions

53
app/app.go Normal file
View file

@ -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
}

View file

@ -1,7 +1,7 @@
package etc package etc
import ( 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/config"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
) )
@ -9,8 +9,8 @@ import (
func Config() lib.Config { func Config() lib.Config {
ovr := Overrides().Use ovr := Overrides().Use
b := config.MakeBase b := config.MakeBase
cfg := scopes.Cfg{ cfg := app.Cfg{
Base: b("dummy", scopes.Start), Base: b("dummy", app.Start),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",
} }

View file

@ -6,12 +6,13 @@ import (
"demo/etc" "demo/etc"
"git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/app"
) )
func main() { func main() {
cfg := etc.Config() cfg := etc.Config()
fmt.Println(cfg.Name()) fmt.Println(cfg.Name())
appCfg := cfg.(*scopes.Cfg) appCfg := cfg.(*app.Cfg)
fmt.Println(appCfg.Home) fmt.Println(appCfg.Home)
scopes.RunApp(cfg) scopes.RunApp(cfg)
} }

View file

@ -1,58 +1,6 @@
package scopes package scopes
import ( import "git.sr.ht/~cco/go-scopes/lib"
"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
func RunApp(cfg lib.Config) { func RunApp(cfg lib.Config) {
ctx := lib.AppContext(cfg) ctx := lib.AppContext(cfg)

View file

@ -8,12 +8,12 @@ import (
"testing" "testing"
"time" "time"
"git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
) )
func Start(ctx lib.Context) { func Start(ctx lib.Context) {
scopes.Start(ctx) app.Start(ctx)
} }
// definitions // definitions

View file

@ -1,7 +1,7 @@
package etc package etc
import ( 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/config"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/testing"
@ -10,7 +10,7 @@ import (
func Config() lib.Config { func Config() lib.Config {
ovr := Overrides().Use ovr := Overrides().Use
b := config.MakeBase b := config.MakeBase
cfg := scopes.Cfg{ cfg := app.Cfg{
Base: b("testing", testing.Start), Base: b("testing", testing.Start),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",

View file

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
tbase "testing" 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/testing"
"git.sr.ht/~cco/go-scopes/tests/etc" "git.sr.ht/~cco/go-scopes/tests/etc"
) )
@ -24,7 +24,7 @@ func TestingTest(t *testing.T) {
func ConfigTest(t *testing.T) { func ConfigTest(t *testing.T) {
cfg := etc.Config() cfg := etc.Config()
t.AssertEqual(cfg.Name(), "testing") t.AssertEqual(cfg.Name(), "testing")
appCfg := cfg.(*scopes.Cfg) appCfg := cfg.(*app.Cfg)
t.AssertEqual(appCfg.Home, "tests") t.AssertEqual(appCfg.Home, "tests")
confCtx := t.Ctx.Services()["config"] confCtx := t.Ctx.Services()["config"]
t.AssertEqual(confCtx.Config().Name(), "config") t.AssertEqual(confCtx.Config().Name(), "config")