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.
104 lines
2.4 KiB
104 lines
2.4 KiB
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"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"`
|
|
}
|
|
|
|
// Wiki is the wiki stuff.
|
|
type Wiki struct {
|
|
URL string `json:"url" yaml:"url"`
|
|
}
|
|
|
|
// 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{}
|
|
err := global.LoadAny("/etc/aiterp/rpdata.yaml", "/etc/aiterp/rpdata.json", "./config.yaml", "./config.json")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|
|
globalMutex.Unlock()
|
|
|
|
return *global
|
|
}
|