provide separate app package for top-level appllication component
This commit is contained in:
parent
2f1efc9731
commit
aec3446d75
7 changed files with 65 additions and 63 deletions
53
app/app.go
Normal file
53
app/app.go
Normal 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
|
||||
}
|
|
@ -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",
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
54
scopes.go
54
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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Reference in a new issue