The new logbot, not committed from the wrong terminal window this time.
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.
 
 

68 lines
1.4 KiB

package config
import (
"encoding/json"
"fmt"
"os"
"sync"
)
// Config represents the logbot's configuration.
type Config struct {
Bot struct {
Nicks []string `json:"nicks"`
User string `json:"user"`
RealName string `json:"realName"`
} `json:"bot"`
Debug struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
} `json:"debug"`
Server struct {
Address string `json:"server"`
SSL bool `json:"ssl"`
} `json:"server"`
API struct {
Endpoint string `json:"endpoint"`
Username string `json:"username"`
Key struct {
ID string `json:"id"`
Secret string `json:"secret"`
} `json:"key"`
} `json:"api"`
Commands struct {
OnJoinOp []string `json:"onJoinOp"`
OnReady []string `json:"onReady"`
} `json:"commands"`
Names struct {
ChanServ string `json:"chanserv"`
} `json:"names"`
}
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
}