First Commit
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user