GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
1.5 KiB

package config
import (
"encoding/json"
"errors"
"log"
"os"
"sync"
)
var globalMutex sync.Mutex
var global *Config
// Config is configuration
type Config struct {
Space struct {
Host string `json:"host"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Bucket string `json:"bucket"`
MaxSize int64 `json:"maxSize"`
Root string `json:"root"`
} `json:"space"`
Database struct {
Host string `json:"host"`
Port int `json:"port"`
Db string `json:"db"`
Username string `json:"username"`
Password string `json:"password"`
Mechanism string `json:"mechanism"`
} `json:"database"`
Wiki struct {
URL string `json:"url"`
} `json:"wiki"`
}
// Load loads config stuff
func (config *Config) Load(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
return json.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 {
return nil
}
*config = Config{}
}
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{}
err := global.LoadAny("/etc/aiterp/rpdata.json", "./config.json")
if err != nil {
log.Fatalln(err)
}
}
globalMutex.Unlock()
return *global
}