First Commit

This commit is contained in:
2018-11-11 13:18:05 +01:00
commit 0ecbc16bdb
15 changed files with 686 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
package config
import (
"encoding/json"
"fmt"
"os"
"sync"
)
// Config represents the logbot's configuration.
type Config struct {
Bot struct {
Nicks []string `json:"nicks"`
Server string `json:"server"`
CommandChannel string `json:"commandChannel"`
} `json:"bot"`
API struct {
Endpoint string `json:"endpoint"`
Username string `json:"username"`
Key struct {
ID string `json:"id"`
Secret string `json:"secret"`
} `json:"key"`
} `json:"api"`
}
var mutex sync.Mutex
var config *Config
// Get lazy-loads the configuration in a thread-safe manner.
func Get() Config {
mutex.Lock()
defer mutex.Unlock()
if config == nil {
config = &Config{}
file, err := os.Open("/etc/aiterp/logbot3.json")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load config: %s\n", err)
os.Exit(1)
}
defer file.Close()
err = json.NewDecoder(file).Decode(&config)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse config: %s\n", err)
os.Exit(1)
}
}
return *config
}