1
0
Fork 0

Refactor app startup

This commit is contained in:
Gregory Eremin 2015-02-12 02:50:04 +07:00
parent ac269889ed
commit 8e2cc9b1d7
2 changed files with 42 additions and 6 deletions

View File

@ -1,6 +1,8 @@
package core package core
import ( import (
"fmt"
"github.com/localhots/yeast/chain" "github.com/localhots/yeast/chain"
"github.com/localhots/yeast/unit" "github.com/localhots/yeast/unit"
) )
@ -9,22 +11,52 @@ type (
App struct { App struct {
config *Config config *Config
chains *chain.Bank chains *chain.Bank
sv *Supervisor
} }
) )
func NewApp() *App { func NewApp() *App {
a := &App{ conf := &Config{}
config: &Config{}, conf.Init()
}
a.config.Init()
ub := unit.NewBank(a.Conf().UnitsConfig) ub := unit.NewBank(conf.C().UnitsConfig)
a.chains = chain.NewBank(a.Conf().ChainsConfig, ub) a := &App{
config: conf,
chains: chain.NewBank(conf.C().ChainsConfig, ub),
sv: &Supervisor{
Bin: conf.C().Python.BinPath,
Wrapper: conf.C().Python.WrapperPath,
},
}
a.chains.Reload() a.chains.Reload()
return a return a
} }
func (a *App) Conf() Config { func (a *App) Conf() Config {
// This is terrible
return a.config.conf.Config().(Config) return a.config.conf.Config().(Config)
} }
func (a *App) Call(chainName string, data []byte) (resp []byte, err error) {
if c, ok := a.chains.Chain(chainName); ok {
return c.Call(data)
} else {
return nil, fmt.Errorf("Unknown chain: %s", chainName)
}
}
func (a *App) BootChain(name string) {
if c, ok := a.chains.Chain(name); ok {
a.sv.Start(c.Units()...)
return
}
panic(fmt.Errorf("Unknown chain: %s", name))
}
func (a *App) ChainUnits(name string) []string {
if c, ok := a.chains.Chain(name); ok {
return c.Units()
}
panic(fmt.Errorf("Unknown chain: %s", name))
}

View File

@ -31,6 +31,10 @@ func (c *Config) Init() {
c.conf.RequireConfig() c.conf.RequireConfig()
} }
func (c *Config) C() Config {
return c.conf.Config().(Config)
}
func (c *Config) decoder(b []byte) interface{} { func (c *Config) decoder(b []byte) interface{} {
var newConf Config var newConf Config
if err := json.Unmarshal(b, &newConf); err != nil { if err := json.Unmarshal(b, &newConf); err != nil {