示例#1
0
func NewPostgres(conf *PostgresConfig, log kite.Logger) *Postgres {
	if conf == nil {
		conf = new(PostgresConfig)

		envLoader := &multiconfig.EnvironmentLoader{Prefix: "kontrol_postgres"}
		configLoader := multiconfig.MultiLoader(
			&multiconfig.TagLoader{}, envLoader,
		)

		if err := configLoader.Load(conf); err != nil {
			fmt.Println("Valid environment variables are: ")
			envLoader.PrintEnvs(conf)
			panic(err)
		}

		err := multiconfig.MultiValidator(&multiconfig.RequiredValidator{}).Validate(conf)
		if err != nil {
			fmt.Println("Valid environment variables are: ")
			envLoader.PrintEnvs(conf)
			panic(err)
		}
	}

	connString := fmt.Sprintf(
		"host=%s port=%d dbname=%s user=%s password=%s sslmode=disable connect_timeout=%d",
		conf.Host, conf.Port, conf.DBName, conf.Username, conf.Password, conf.ConnectTimeout,
	)

	db, err := sql.Open("postgres", connString)
	if err != nil {
		panic(err)
	}

	p := &Postgres{
		DB:  db,
		Log: log,
	}

	cleanInterval := 120 * time.Second // clean every 120 second
	go p.RunCleaner(cleanInterval, KeyTTL)

	return p
}
示例#2
0
文件: hero.go 项目: gernest/hero
func getConfig(path string) (*hero.Config, error) {
	if path == "" {
		path = configName
	}
	loader := multiconfig.MultiLoader(
		&multiconfig.TagLoader{},
		&multiconfig.EnvironmentLoader{},
		&multiconfig.JSONLoader{Path: path},
	)
	d := &multiconfig.DefaultLoader{}
	d.Loader = loader
	d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{})

	cfg := &hero.Config{}

	err := d.Load(cfg)
	if err != nil {
		return nil, err
	}
	return cfg, nil
}
示例#3
0
文件: kontrol.go 项目: koding/koding
func main() {
	loader := multiconfig.MultiLoader(
		&multiconfig.TagLoader{},
		&multiconfig.EnvironmentLoader{Prefix: "kontrol"},
		&multiconfig.EnvironmentLoader{Prefix: "KONFIG_KONTROL"},
		&multiconfig.FlagLoader{EnvPrefix: "kontrol"},
	)

	conf := new(kontrol.Config)

	// Load the config, it's reads from the file, environment variables and
	// lastly from flags in order
	if err := loader.Load(conf); err != nil {
		panic(err)
	}

	if err := multiconfig.MultiValidator(&multiconfig.RequiredValidator{}).Validate(conf); err != nil {
		panic(err)
	}

	fmt.Printf("Kontrol loaded with following variables: %+v\n", conf)

	k := kontrol.New(conf)

	k.Kite.HandleHTTPFunc("/healthCheck", artifact.HealthCheckHandler(Name))
	k.Kite.HandleHTTPFunc("/version", artifact.VersionHandler())

	if conf.Debug {
		k.Kite.SetLogLevel(kite.DEBUG)
	}

	go func() {
		// Kloud runs on 6060, so we choose 6061 for kontrol
		err := http.ListenAndServe("0.0.0.0:6061", nil)
		k.Kite.Log.Error(err.Error())
	}()

	k.Run()
}