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.

61 lines
1.2 KiB

6 years ago
6 years ago
6 years ago
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. User string `json:"user"`
  13. RealName string `json:"realName"`
  14. } `json:"bot"`
  15. Server struct {
  16. Address string `json:"server"`
  17. SSL bool `json:"ssl"`
  18. }
  19. API struct {
  20. Endpoint string `json:"endpoint"`
  21. Username string `json:"username"`
  22. Key struct {
  23. ID string `json:"id"`
  24. Secret string `json:"secret"`
  25. } `json:"key"`
  26. } `json:"api"`
  27. Commands struct {
  28. OnJoinOp []string `json:"onJoinOp"`
  29. OnReady []string `json:"onReady"`
  30. }
  31. }
  32. var mutex sync.Mutex
  33. var config *Config
  34. // Get lazy-loads the configuration in a thread-safe manner.
  35. func Get() Config {
  36. mutex.Lock()
  37. defer mutex.Unlock()
  38. if config == nil {
  39. config = &Config{}
  40. file, err := os.Open("/etc/aiterp/logbot3.json")
  41. if err != nil {
  42. fmt.Fprintf(os.Stderr, "Failed to load config: %s\n", err)
  43. os.Exit(1)
  44. }
  45. defer file.Close()
  46. err = json.NewDecoder(file).Decode(&config)
  47. if err != nil {
  48. fmt.Fprintf(os.Stderr, "Failed to parse config: %s\n", err)
  49. os.Exit(1)
  50. }
  51. }
  52. return *config
  53. }