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
import (
"fmt"
"github.com/localhots/yeast/chain"
"github.com/localhots/yeast/unit"
)
@ -9,22 +11,52 @@ type (
App struct {
config *Config
chains *chain.Bank
sv *Supervisor
}
)
func NewApp() *App {
a := &App{
config: &Config{},
}
a.config.Init()
conf := &Config{}
conf.Init()
ub := unit.NewBank(a.Conf().UnitsConfig)
a.chains = chain.NewBank(a.Conf().ChainsConfig, ub)
ub := unit.NewBank(conf.C().UnitsConfig)
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()
return a
}
func (a *App) Conf() Config {
// This is terrible
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()
}
func (c *Config) C() Config {
return c.conf.Config().(Config)
}
func (c *Config) decoder(b []byte) interface{} {
var newConf Config
if err := json.Unmarshal(b, &newConf); err != nil {