basic context implementations + start of component instances
This commit is contained in:
		
							parent
							
								
									ca37aead92
								
							
						
					
					
						commit
						e30052b297
					
				
					 6 changed files with 74 additions and 19 deletions
				
			
		| 
						 | 
					@ -1,6 +1,64 @@
 | 
				
			||||||
package common
 | 
					package common
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Config interface {
 | 
				
			||||||
 | 
						Name() string
 | 
				
			||||||
 | 
						Starter() StartFct
 | 
				
			||||||
 | 
						Children() []Config
 | 
				
			||||||
 | 
						Add(Config)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Context interface {
 | 
					type Context interface {
 | 
				
			||||||
 | 
						Config() Config
 | 
				
			||||||
 | 
						Parent() Context
 | 
				
			||||||
 | 
						Services() map[string]Context
 | 
				
			||||||
 | 
						ChildContext(Config) Context
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type StartFct = func(Context)
 | 
					type StartFct = func(Context)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Context implementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type context struct {
 | 
				
			||||||
 | 
						cfg    Config
 | 
				
			||||||
 | 
						parent Context
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ctx *context) Config() Config {
 | 
				
			||||||
 | 
						return ctx.cfg
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ctx *context) Parent() Context {
 | 
				
			||||||
 | 
						return ctx.parent
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ctx *context) Services() map[string]Context {
 | 
				
			||||||
 | 
						return ctx.parent.Services()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ctx *context) ChildContext(cfg Config) Context {
 | 
				
			||||||
 | 
						cctx := context{cfg, ctx}
 | 
				
			||||||
 | 
						ctx.Services()[cfg.Name()] = &cctx
 | 
				
			||||||
 | 
						return &cctx
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type appContext struct {
 | 
				
			||||||
 | 
						*context
 | 
				
			||||||
 | 
						services map[string]Context
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ctx *appContext) ChildContext(cfg Config) Context {
 | 
				
			||||||
 | 
						cctx := context{cfg, ctx}
 | 
				
			||||||
 | 
						ctx.services[cfg.Name()] = &cctx
 | 
				
			||||||
 | 
						return &cctx
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ctx *appContext) Services() map[string]Context {
 | 
				
			||||||
 | 
						return ctx.services
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func AppContext(cfg Config) Context {
 | 
				
			||||||
 | 
						return &appContext{
 | 
				
			||||||
 | 
							context:  &context{cfg, nil},
 | 
				
			||||||
 | 
							services: map[string]Context{},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,17 +17,10 @@ func Start(ctx common.Context) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// definitions
 | 
					// definitions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Config interface {
 | 
					 | 
				
			||||||
	Name() string
 | 
					 | 
				
			||||||
	Starter() common.StartFct
 | 
					 | 
				
			||||||
	Children() []Config
 | 
					 | 
				
			||||||
	Add(Config)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Base struct {
 | 
					type Base struct {
 | 
				
			||||||
	name     string
 | 
						name     string
 | 
				
			||||||
	starter  common.StartFct
 | 
						starter  common.StartFct
 | 
				
			||||||
	children []Config
 | 
						children []common.Config
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (cfg *Base) Name() string {
 | 
					func (cfg *Base) Name() string {
 | 
				
			||||||
| 
						 | 
					@ -38,11 +31,11 @@ func (cfg *Base) Starter() common.StartFct {
 | 
				
			||||||
	return cfg.starter
 | 
						return cfg.starter
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (cfg *Base) Children() []Config {
 | 
					func (cfg *Base) Children() []common.Config {
 | 
				
			||||||
	return cfg.children
 | 
						return cfg.children
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (cfg *Base) Add(child Config) {
 | 
					func (cfg *Base) Add(child common.Config) {
 | 
				
			||||||
	cfg.children = append(cfg.children, child)
 | 
						cfg.children = append(cfg.children, child)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,7 +46,7 @@ func MakeBase(name string, starter common.StartFct) *Base {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Setup(cfg Config) Config {
 | 
					func Setup(cfg common.Config) common.Config {
 | 
				
			||||||
	return cfg
 | 
						return cfg
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,10 +2,11 @@ package etc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"git.sr.ht/~cco/go-scopes"
 | 
						"git.sr.ht/~cco/go-scopes"
 | 
				
			||||||
 | 
						"git.sr.ht/~cco/go-scopes/common"
 | 
				
			||||||
	"git.sr.ht/~cco/go-scopes/config"
 | 
						"git.sr.ht/~cco/go-scopes/config"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Config() config.Config {
 | 
					func Config() common.Config {
 | 
				
			||||||
	ovr := Overrides().Use
 | 
						ovr := Overrides().Use
 | 
				
			||||||
	b := config.MakeBase
 | 
						b := config.MakeBase
 | 
				
			||||||
	cfg := scopes.Cfg{
 | 
						cfg := scopes.Cfg{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,10 +12,15 @@ type Cfg struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Start(ctx common.Context) {
 | 
					func Start(ctx common.Context) {
 | 
				
			||||||
 | 
						for _, cfg := range ctx.Config().Children() {
 | 
				
			||||||
 | 
							cctx := ctx.ChildContext(cfg)
 | 
				
			||||||
 | 
							cfg.Starter()(cctx)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// definitions
 | 
					// definitions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func RunApp(cfg config.Config) {
 | 
					func RunApp(cfg common.Config) {
 | 
				
			||||||
	_ = cfg
 | 
						ctx := common.AppContext(cfg)
 | 
				
			||||||
 | 
						cfg.Starter()(ctx)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,10 +2,11 @@ package etc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"git.sr.ht/~cco/go-scopes"
 | 
						"git.sr.ht/~cco/go-scopes"
 | 
				
			||||||
 | 
						"git.sr.ht/~cco/go-scopes/common"
 | 
				
			||||||
	"git.sr.ht/~cco/go-scopes/config"
 | 
						"git.sr.ht/~cco/go-scopes/config"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Config() config.Config {
 | 
					func Config() common.Config {
 | 
				
			||||||
	ovr := Overrides().Use
 | 
						ovr := Overrides().Use
 | 
				
			||||||
	b := config.MakeBase
 | 
						b := config.MakeBase
 | 
				
			||||||
	cfg := scopes.Cfg{
 | 
						cfg := scopes.Cfg{
 | 
				
			||||||
| 
						 | 
					@ -20,15 +21,13 @@ func Config() config.Config {
 | 
				
			||||||
// collect here the names of fields that may be overridden via
 | 
					// collect here the names of fields that may be overridden via
 | 
				
			||||||
// explicit Override() or SCOPES_* environment settings.
 | 
					// explicit Override() or SCOPES_* environment settings.
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	NAME = "name"
 | 
					 | 
				
			||||||
	HOME = "home"
 | 
						HOME = "home"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// in a production scenario this should be put in separate
 | 
					// in a production scenario this should be put in a separate
 | 
				
			||||||
// file `settings.go` that should not be stored in code repository.
 | 
					// file `settings.go` that should not be stored in code repository.
 | 
				
			||||||
func Overrides() config.Settings {
 | 
					func Overrides() config.Settings {
 | 
				
			||||||
	return config.Settings{
 | 
						return config.Settings{
 | 
				
			||||||
		NAME: "overridden",
 | 
					 | 
				
			||||||
		HOME: "/home/scopes",
 | 
							HOME: "/home/scopes",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,6 @@ func TestConfig(tb *tbase.T) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ConfigTest(t *testing.T) {
 | 
					func ConfigTest(t *testing.T) {
 | 
				
			||||||
	t.AssertEqual(etc.Overrides()[etc.NAME], "overridden")
 | 
					 | 
				
			||||||
	cfg := config.Setup(etc.Config())
 | 
						cfg := config.Setup(etc.Config())
 | 
				
			||||||
	t.AssertEqual(cfg.Name(), "dummy")
 | 
						t.AssertEqual(cfg.Name(), "dummy")
 | 
				
			||||||
	appCfg := cfg.(*scopes.Cfg)
 | 
						appCfg := cfg.(*scopes.Cfg)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue