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.

53 lines
1.0 KiB

6 years ago
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "sync"
  7. )
  8. // Config represents the logbot's configuration.
  9. type Config struct {
  10. Bot struct {
  11. Nicks []string `json:"nicks"`
  12. Server string `json:"server"`
  13. CommandChannel string `json:"commandChannel"`
  14. } `json:"bot"`
  15. API struct {
  16. Endpoint string `json:"endpoint"`
  17. Username string `json:"username"`
  18. Key struct {
  19. ID string `json:"id"`
  20. Secret string `json:"secret"`
  21. } `json:"key"`
  22. } `json:"api"`
  23. }
  24. var mutex sync.Mutex
  25. var config *Config
  26. // Get lazy-loads the configuration in a thread-safe manner.
  27. func Get() Config {
  28. mutex.Lock()
  29. defer mutex.Unlock()
  30. if config == nil {
  31. config = &Config{}
  32. file, err := os.Open("/etc/aiterp/logbot3.json")
  33. if err != nil {
  34. fmt.Fprintf(os.Stderr, "Failed to load config: %s\n", err)
  35. os.Exit(1)
  36. }
  37. defer file.Close()
  38. err = json.NewDecoder(file).Decode(&config)
  39. if err != nil {
  40. fmt.Fprintf(os.Stderr, "Failed to parse config: %s\n", err)
  41. os.Exit(1)
  42. }
  43. }
  44. return *config
  45. }