package config import ( "encoding/json" "errors" "log" "os" "strconv" "strings" "sync" "gopkg.in/yaml.v2" ) var globalMutex sync.Mutex var global *Config // Config is configuration data type Config struct { Space Space `json:"space" yaml:"space"` Database Database `json:"database" yaml:"database"` Wiki Wiki `json:"wiki" yaml:"wiki"` } // Space is configuration for spaces. type Space struct { Enabled bool `json:"enabled" yaml:"enabled"` Host string `json:"host" yaml:"host"` AccessKey string `json:"accessKey" yaml:"accessKey"` SecretKey string `json:"secretKey" yaml:"secretKey"` Bucket string `json:"bucket" yaml:"bucket"` MaxSize int64 `json:"maxSize" yaml:"maxSize"` Root string `json:"root" yaml:"root"` URLRoot string `json:"urlRoot" yaml:"urlRoot"` } // Database is configuration for spaces. type Database struct { Driver string `json:"driver" yaml:"driver"` Host string `json:"host" yaml:"host"` Port int `json:"port" yaml:"port"` Db string `json:"db" yaml:"db"` Username string `json:"username" yaml:"username"` Password string `json:"password" yaml:"password"` Mechanism string `json:"mechanism" yaml:"mechanism"` RestoreIDs bool `json:"restoreIDs" yaml:"restoreIDs"` SSL bool `json:"ssl" yaml:"ssl"` } // Wiki is the wiki stuff. type Wiki struct { URL string `json:"url" yaml:"url"` } func (config *Config) LoadEnv() { config.Wiki.URL = os.Getenv("RPDATA_WIKI_URL") config.Space.Enabled = os.Getenv("RPDATA_SPACE_ENABLED") == "true" config.Space.Host = os.Getenv("RPDATA_SPACE_HOST") config.Space.AccessKey = os.Getenv("RPDATA_SPACE_ACCESS_KEY") config.Space.SecretKey = os.Getenv("RPDATA_SPACE_SECRET_KEY") config.Space.Bucket = os.Getenv("RPDATA_SPACE_BUCKET") config.Space.MaxSize, _ = strconv.ParseInt(os.Getenv("RPDATA_SPACE_MAX_SIZE"), 10, 64) config.Space.Root = os.Getenv("RPDATA_SPACE_ROOT") config.Space.URLRoot = os.Getenv("RPDATA_SPACE_URL_ROOT") config.Database.Driver = os.Getenv("RPDATA_DATABASE_DRIVER") config.Database.Host = os.Getenv("RPDATA_DATABASE_HOST") config.Database.Port, _ = strconv.Atoi(os.Getenv("RPDATA_DATABASE_PORT")) config.Database.Db = os.Getenv("RPDATA_DATABASE_DB") config.Database.Username = os.Getenv("RPDATA_DATABASE_USERNAME") config.Database.Password = os.Getenv("RPDATA_DATABASE_PASSWORD") config.Database.Mechanism = os.Getenv("RPDATA_DATABASE_MECHANISM") config.Database.RestoreIDs = os.Getenv("RPDATA_DATABASE_RESTORE_IDS") == "true" config.Database.SSL = os.Getenv("RPDATA_DATABASE_SSL") == "true" } // Load loads config stuff func (config *Config) Load(filename string) error { log.Println("Trying to load config from " + filename) stat, err := os.Stat(filename) if err != nil { return err } if stat.Size() < 1 { return errors.New("File is empty") } file, err := os.Open(filename) if err != nil { return err } if strings.HasSuffix(filename, ".json") { return json.NewDecoder(file).Decode(config) } return yaml.NewDecoder(file).Decode(config) } // LoadAny loads the first of these files it can find func (config *Config) LoadAny(filenames ...string) error { for _, filename := range filenames { if err := config.Load(filename); err != nil { continue } return nil } return errors.New("Failed to load configuration files") } // Global gets the global configuration, loading it if this is the first caller func Global() Config { globalMutex.Lock() if global == nil { global = &Config{} global.LoadEnv() err := global.LoadAny("/etc/aiterp/rpdata.yaml", "/etc/aiterp/rpdata.json", "./config.yaml", "./config.json") if err != nil { log.Println("No config files were loaded.") } } globalMutex.Unlock() return *global }