Example #1
0
File: run.go Project: itpkg/chaos
//IocAction ioc action
func IocAction(fn func(*cli.Context, *inject.Graph) error) cli.ActionFunc {
	return Action(func(ctx *cli.Context) error {
		var inj inject.Graph
		logger := Logger()
		if !IsProduction() {
			inj.Logger = logger
		}

		db, err := OpenDatabase()
		if err != nil {
			return err
		}
		rep := OpenRedis()

		wfg := viper.GetStringMapString("workers.config")
		wfg["server"] = fmt.Sprintf(
			"%s:%d",
			viper.GetString("redis.host"),
			viper.GetInt("redis.port"))
		wfg["database"] = viper.GetString("redis.db")
		workers.Configure(wfg)

		i1n := i18n.I18n{Locales: make(map[string]map[string]string)}
		if err := inj.Provide(
			&inject.Object{Value: logger},
			&inject.Object{Value: db},
			&inject.Object{Value: rep},
			&inject.Object{Value: &i18n.DatabaseProvider{}},
			&inject.Object{Value: &i1n},
		); err != nil {
			return err
		}
		Loop(func(en Engine) error {
			if e := en.Map(&inj); e != nil {
				return e
			}
			return inj.Provide(&inject.Object{Value: en})
		})
		if err := inj.Populate(); err != nil {
			return err
		}
		if err := i1n.Load("locales"); err != nil {
			return err
		}
		return fn(ctx, &inj)
	})
}