statping/core/configs.go

63 lines
1.6 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package core
import (
"errors"
2018-06-30 00:57:05 +00:00
"github.com/go-yaml/yaml"
"github.com/hunterlong/statup/types"
"io/ioutil"
"os"
2018-06-30 00:57:05 +00:00
)
func LoadConfig() (*types.Config, error) {
if os.Getenv("DB_CONN") != "" {
return LoadUsingEnv()
}
Configs = new(types.Config)
2018-06-30 00:57:05 +00:00
file, err := ioutil.ReadFile("config.yml")
if err != nil {
return nil, errors.New("config.yml file not found - starting in setup mode")
}
err = yaml.Unmarshal(file, &Configs)
CoreApp.DbConnection = Configs.Connection
return Configs, err
}
func LoadUsingEnv() (*types.Config, error) {
Configs = new(types.Config)
if os.Getenv("DB_CONN") == "" {
return nil, errors.New("Missing DB_CONN environment variable")
}
if os.Getenv("DB_HOST") == "" {
return nil, errors.New("Missing DB_HOST environment variable")
}
if os.Getenv("DB_USER") == "" {
return nil, errors.New("Missing DB_USER environment variable")
}
if os.Getenv("DB_PASS") == "" {
return nil, errors.New("Missing DB_PASS environment variable")
}
if os.Getenv("DB_DATABASE") == "" {
return nil, errors.New("Missing DB_DATABASE environment variable")
}
Configs.Connection = os.Getenv("DB_CONN")
Configs.Host = os.Getenv("DB_HOST")
Configs.Port = os.Getenv("DB_PORT")
Configs.User = os.Getenv("DB_USER")
Configs.Password = os.Getenv("DB_PASS")
Configs.Database = os.Getenv("DB_DATABASE")
CoreApp.DbConnection = os.Getenv("DB_CONN")
CoreApp.Name = os.Getenv("NAME")
CoreApp.Domain = os.Getenv("DOMAIN")
if os.Getenv("USE_CDN") == "true" {
CoreApp.UseCdn = true
}
return Configs, nil
}
func ifOr(val, def string) string {
if val == "" {
return def
2018-06-30 00:57:05 +00:00
}
return val
2018-06-30 00:57:05 +00:00
}