This commit is contained in:
+52
-26
@@ -5,56 +5,82 @@ import (
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var globalMutex sync.Mutex
|
||||
var global *Config
|
||||
|
||||
// Config is configuration
|
||||
// Config is configuration data
|
||||
type Config struct {
|
||||
Space struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
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"`
|
||||
Space Space `json:"space" yaml:"space"`
|
||||
Database Database `json:"database" yaml:"database"`
|
||||
Wiki Wiki `json:"wiki" yaml:"wiki"`
|
||||
}
|
||||
|
||||
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"`
|
||||
// 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"`
|
||||
}
|
||||
|
||||
Wiki struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"wiki"`
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
return json.NewDecoder(file).Decode(config)
|
||||
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 {
|
||||
return nil
|
||||
if err := config.Load(filename); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
*config = Config{}
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("Failed to load configuration files")
|
||||
@@ -65,7 +91,7 @@ func Global() Config {
|
||||
globalMutex.Lock()
|
||||
if global == nil {
|
||||
global = &Config{}
|
||||
err := global.LoadAny("/etc/aiterp/rpdata.json", "./config.json")
|
||||
err := global.LoadAny("/etc/aiterp/rpdata.yaml", "/etc/aiterp/rpdata.json", "./config.yaml", "./config.json")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user