provide minimal default config, + some clean-up and standardization
This commit is contained in:
parent
0f054c36e8
commit
e21499234b
10 changed files with 82 additions and 72 deletions
|
@ -7,38 +7,75 @@ import (
|
||||||
lib "git.sr.ht/~cco/go-scopes"
|
lib "git.sr.ht/~cco/go-scopes"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cfg struct {
|
// default config
|
||||||
*BaseCfg
|
|
||||||
ConfigFormat string
|
|
||||||
}
|
|
||||||
|
|
||||||
func Start(ctx lib.Context) {
|
type DefaultCfg = defcfg
|
||||||
}
|
|
||||||
|
|
||||||
// definitions
|
type defcfg struct {
|
||||||
|
|
||||||
type BaseCfg = base
|
|
||||||
|
|
||||||
type base struct {
|
|
||||||
name string
|
name string
|
||||||
starter lib.StartProc
|
|
||||||
step lib.StepProc
|
|
||||||
msgHandler lib.MessageHandler
|
|
||||||
doneHandler lib.StepProc
|
|
||||||
actions []lib.ActionConfig
|
actions []lib.ActionConfig
|
||||||
children []lib.Config
|
children []lib.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// lib.Config implementation
|
func (cfg *defcfg) Name() string {
|
||||||
|
|
||||||
func (cfg *base) Name() string {
|
|
||||||
return cfg.name
|
return cfg.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *base) SetName(n string) {
|
func (cfg *defcfg) SetName(n string) {
|
||||||
cfg.name = n
|
cfg.name = n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) Actions() []lib.ActionConfig {
|
||||||
|
return cfg.actions
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) AddAction(pattern string, specs ...lib.ActionSpec) {
|
||||||
|
act := ActionConfig(pattern, specs)
|
||||||
|
cfg.actions = append(cfg.actions, act)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) Children() []lib.Config {
|
||||||
|
return cfg.children
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) Add(c ...lib.Config) {
|
||||||
|
cfg.children = append(cfg.children, c...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) Starter() lib.StartProc {
|
||||||
|
return DefaultStart
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) Step() lib.StepProc {
|
||||||
|
return DefaultStep
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) MessageHandler() lib.MessageHandler {
|
||||||
|
return DefaultMsgHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *defcfg) DoneHandler() lib.StepProc {
|
||||||
|
return DefaultDoneHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func Default(name string) lib.Config {
|
||||||
|
return &defcfg{
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// base config
|
||||||
|
|
||||||
|
type BaseCfg = base
|
||||||
|
|
||||||
|
type base struct {
|
||||||
|
*defcfg
|
||||||
|
starter lib.StartProc
|
||||||
|
step lib.StepProc
|
||||||
|
msgHandler lib.MessageHandler
|
||||||
|
doneHandler lib.StepProc
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *base) Starter() lib.StartProc {
|
func (cfg *base) Starter() lib.StartProc {
|
||||||
return cfg.starter
|
return cfg.starter
|
||||||
}
|
}
|
||||||
|
@ -55,19 +92,6 @@ func (cfg *base) DoneHandler() lib.StepProc {
|
||||||
return cfg.doneHandler
|
return cfg.doneHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *base) Actions() []lib.ActionConfig {
|
|
||||||
return cfg.actions
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *base) Children() []lib.Config {
|
|
||||||
return cfg.children
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *base) Add(c ...lib.Config) lib.Config {
|
|
||||||
cfg.children = append(cfg.children, c...)
|
|
||||||
return cfg
|
|
||||||
}
|
|
||||||
|
|
||||||
// implementation-specific methods and functions
|
// implementation-specific methods and functions
|
||||||
|
|
||||||
func (cfg *base) WithStep(step lib.StepProc) *base {
|
func (cfg *base) WithStep(step lib.StepProc) *base {
|
||||||
|
@ -85,24 +109,20 @@ func (cfg *base) WithDoneHandler(hdlr lib.StepProc) *base {
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *base) AddAction(pattern string, specs ...lib.ActionSpec) lib.Config {
|
|
||||||
act := ActionConfig(pattern, specs)
|
|
||||||
cfg.actions = append(cfg.actions, act)
|
|
||||||
return cfg
|
|
||||||
}
|
|
||||||
|
|
||||||
func Base(name string, starter lib.StartProc) *base {
|
func Base(name string, starter lib.StartProc) *base {
|
||||||
return &base{
|
return &base{
|
||||||
|
defcfg: &defcfg{
|
||||||
name: name,
|
name: name,
|
||||||
|
},
|
||||||
starter: starter,
|
starter: starter,
|
||||||
step: DefaultStep,
|
step: DefaultStep,
|
||||||
msgHandler: DefaultMsgHandler,
|
msgHandler: DefaultMsgHandler,
|
||||||
doneHandler: DefaultDoneHandler,
|
doneHandler: DefaultDoneHandler,
|
||||||
actions: nil,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// will be set by core.init()
|
// will be set by core.init()
|
||||||
|
var DefaultStart lib.StartProc
|
||||||
var DefaultStep lib.StepProc
|
var DefaultStep lib.StepProc
|
||||||
var DefaultMsgHandler lib.MessageHandler
|
var DefaultMsgHandler lib.MessageHandler
|
||||||
var DefaultDoneHandler lib.StepProc
|
var DefaultDoneHandler lib.StepProc
|
||||||
|
|
|
@ -46,7 +46,8 @@ func HandleDone(ctx lib.Context) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
config.DefaultStep = Step // avoid import cycle
|
config.DefaultStart = Start // avoid import cycle
|
||||||
|
config.DefaultStep = Step
|
||||||
config.DefaultMsgHandler = HandleMessage
|
config.DefaultMsgHandler = HandleMessage
|
||||||
config.DefaultDoneHandler = HandleDone
|
config.DefaultDoneHandler = HandleDone
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
func Config() lib.Config {
|
func Config() lib.Config {
|
||||||
ovr := Overrides().Use
|
ovr := Overrides().Use
|
||||||
b := config.Base
|
b := config.Base
|
||||||
|
|
||||||
app_c := app.Cfg{
|
app_c := app.Cfg{
|
||||||
BaseCfg: b("dummy", app.Start),
|
BaseCfg: b("dummy", app.Start),
|
||||||
Home: ovr(".", HOME),
|
Home: ovr(".", HOME),
|
||||||
|
@ -18,10 +19,7 @@ func Config() lib.Config {
|
||||||
Logfile: ovr("log/scopes.log", LOGFILE),
|
Logfile: ovr("log/scopes.log", LOGFILE),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app_c.Add(config.Cfg{
|
|
||||||
BaseCfg: b("config", config.Start),
|
|
||||||
ConfigFormat: "etc",
|
|
||||||
})
|
|
||||||
return &app_c
|
return &app_c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,14 @@ package matrix
|
||||||
import (
|
import (
|
||||||
lib "git.sr.ht/~cco/go-scopes"
|
lib "git.sr.ht/~cco/go-scopes"
|
||||||
"git.sr.ht/~cco/go-scopes/config"
|
"git.sr.ht/~cco/go-scopes/config"
|
||||||
"git.sr.ht/~cco/go-scopes/core"
|
|
||||||
"git.sr.ht/~cco/go-scopes/core/action"
|
"git.sr.ht/~cco/go-scopes/core/action"
|
||||||
"git.sr.ht/~cco/go-scopes/core/message"
|
"git.sr.ht/~cco/go-scopes/core/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(ctx lib.Context) {
|
func DefaultConfig(name string) lib.Config {
|
||||||
cfg := ctx.Config()
|
cfg := config.Default(name)
|
||||||
cfg.AddAction("create", action.Base(create))
|
cfg.AddAction("create", action.Base(create))
|
||||||
core.Start(ctx)
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func create(act lib.Action) bool {
|
func create(act lib.Action) bool {
|
||||||
|
@ -24,7 +23,7 @@ func create(act lib.Action) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCell(ctx lib.Context) lib.Context {
|
func createCell(ctx lib.Context) lib.Context {
|
||||||
cfg := config.Base("", core.Start)
|
cfg := DefaultConfig("")
|
||||||
cctx := ctx.ChildContext(cfg)
|
cctx := ctx.ChildContext(cfg)
|
||||||
cfg.Starter()(cctx)
|
cfg.Starter()(cctx)
|
||||||
return cctx
|
return cctx
|
||||||
|
|
|
@ -103,9 +103,9 @@ type Config interface {
|
||||||
MessageHandler() MessageHandler
|
MessageHandler() MessageHandler
|
||||||
DoneHandler() StepProc
|
DoneHandler() StepProc
|
||||||
Actions() []ActionConfig
|
Actions() []ActionConfig
|
||||||
AddAction(string, ...ActionSpec) Config
|
AddAction(string, ...ActionSpec)
|
||||||
Children() []Config
|
Children() []Config
|
||||||
Add(...Config) Config
|
Add(...Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ActionConfig interface {
|
type ActionConfig interface {
|
||||||
|
|
|
@ -71,9 +71,9 @@ func ActionTest(t *testing.T) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cfg := config.Base("testing", core.None).
|
cfg := config.Base("testing", core.None)
|
||||||
AddAction("start", action.Base(hdlrGen("started"))).
|
cfg.AddAction("start", action.Base(hdlrGen("started")))
|
||||||
AddAction("scopes|doit|task", action.Base(hdlrGen("done")))
|
cfg.AddAction("scopes|doit|task", action.Base(hdlrGen("done")))
|
||||||
ctx := context.AppContext(cfg)
|
ctx := context.AppContext(cfg)
|
||||||
lib.HandleMsg(ctx, message.SimpleMessage("start"))
|
lib.HandleMsg(ctx, message.SimpleMessage("start"))
|
||||||
t.AssertEqual(result.data, "started")
|
t.AssertEqual(result.data, "started")
|
||||||
|
|
|
@ -27,11 +27,6 @@ func Config() lib.Config {
|
||||||
}
|
}
|
||||||
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
||||||
|
|
||||||
config_c := &config.Cfg{
|
|
||||||
BaseCfg: b("config", config.Start),
|
|
||||||
ConfigFormat: "etc",
|
|
||||||
}
|
|
||||||
|
|
||||||
server_c := &server.Cfg{
|
server_c := &server.Cfg{
|
||||||
BaseCfg: b("server", server.Start),
|
BaseCfg: b("server", server.Start),
|
||||||
Port: ovr("8123", SERVER_PORT),
|
Port: ovr("8123", SERVER_PORT),
|
||||||
|
@ -43,10 +38,10 @@ func Config() lib.Config {
|
||||||
}
|
}
|
||||||
test_client.AddAction("demo", action.Base(client.Send))
|
test_client.AddAction("demo", action.Base(client.Send))
|
||||||
|
|
||||||
test_rcvr := b("test-receiver", core.Start).
|
test_rcvr := b("test-receiver", core.Start)
|
||||||
AddAction("demo", action.Base(AH_Receiver))
|
test_rcvr.AddAction("demo", action.Base(AH_Receiver))
|
||||||
|
|
||||||
app_c.Add(config_c, server_c, test_client, test_rcvr)
|
app_c.Add(server_c, test_client, test_rcvr)
|
||||||
|
|
||||||
return app_c
|
return app_c
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"git.sr.ht/~cco/go-scopes/app"
|
"git.sr.ht/~cco/go-scopes/app"
|
||||||
"git.sr.ht/~cco/go-scopes/common/testing"
|
"git.sr.ht/~cco/go-scopes/common/testing"
|
||||||
"git.sr.ht/~cco/go-scopes/config"
|
"git.sr.ht/~cco/go-scopes/config"
|
||||||
"git.sr.ht/~cco/go-scopes/core"
|
|
||||||
"git.sr.ht/~cco/go-scopes/core/action"
|
"git.sr.ht/~cco/go-scopes/core/action"
|
||||||
"git.sr.ht/~cco/go-scopes/logging"
|
"git.sr.ht/~cco/go-scopes/logging"
|
||||||
"git.sr.ht/~cco/go-scopes/matrix"
|
"git.sr.ht/~cco/go-scopes/matrix"
|
||||||
|
@ -23,10 +22,10 @@ func ConfigMx() lib.Config {
|
||||||
}
|
}
|
||||||
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
|
||||||
|
|
||||||
test_rcvr := b("test-receiver", core.Start).
|
test_rcvr := config.Default("test-receiver")
|
||||||
AddAction("demo", action.Base(AH_MxReceiver))
|
test_rcvr.AddAction("demo", action.Base(AH_MxReceiver))
|
||||||
|
|
||||||
cell_0 := b("cell-0", matrix.Start)
|
cell_0 := matrix.DefaultConfig("cell-0")
|
||||||
|
|
||||||
app_c.Add(cell_0, test_rcvr)
|
app_c.Add(cell_0, test_rcvr)
|
||||||
|
|
||||||
|
|
|
@ -22,9 +22,9 @@ func TestMatrixApp(tb *tbase.T) {
|
||||||
func MxTest(t *testing.T) {
|
func MxTest(t *testing.T) {
|
||||||
ctx := t.Ctx
|
ctx := t.Ctx
|
||||||
t.AssertEqual(len(ctx.Services()), 3)
|
t.AssertEqual(len(ctx.Services()), 3)
|
||||||
rcvr := message.SimpleAddress("cell-0")
|
rcvr := ctx.Services()["cell-0"]
|
||||||
msg := message.SimpleMessage("create")
|
msg := message.SimpleMessage("create").WithSender(rcvr)
|
||||||
lib.Send(ctx, rcvr, msg)
|
rcvr.Send(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// action handlers
|
// action handlers
|
||||||
|
|
|
@ -24,7 +24,7 @@ func TestScopesApp(tb *tbase.T) {
|
||||||
|
|
||||||
func AppTest(t *testing.T) {
|
func AppTest(t *testing.T) {
|
||||||
ctx := t.Ctx
|
ctx := t.Ctx
|
||||||
t.AssertEqual(len(ctx.Services()), 5)
|
t.AssertEqual(len(ctx.Services()), 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigTest(t *testing.T) {
|
func ConfigTest(t *testing.T) {
|
||||||
|
@ -32,8 +32,6 @@ func ConfigTest(t *testing.T) {
|
||||||
t.AssertEqual(cfg.Name(), "testing")
|
t.AssertEqual(cfg.Name(), "testing")
|
||||||
appCfg := cfg.(*app.Cfg)
|
appCfg := cfg.(*app.Cfg)
|
||||||
t.AssertEqual(appCfg.Home, ".")
|
t.AssertEqual(appCfg.Home, ".")
|
||||||
confCtx := t.Ctx.Services()["config"]
|
|
||||||
t.AssertEqual(confCtx.Config().Name(), "config")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendTest(t *testing.T) {
|
func SendTest(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue