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.

57 lines
1.1 KiB

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. }
  28. var mutex sync.Mutex
  29. var config *Config
  30. // Get lazy-loads the configuration in a thread-safe manner.
  31. func Get() Config {
  32. mutex.Lock()
  33. defer mutex.Unlock()
  34. if config == nil {
  35. config = &Config{}
  36. file, err := os.Open("/etc/aiterp/logbot3.json")
  37. if err != nil {
  38. fmt.Fprintf(os.Stderr, "Failed to load config: %s\n", err)
  39. os.Exit(1)
  40. }
  41. defer file.Close()
  42. err = json.NewDecoder(file).Decode(&config)
  43. if err != nil {
  44. fmt.Fprintf(os.Stderr, "Failed to parse config: %s\n", err)
  45. os.Exit(1)
  46. }
  47. }
  48. return *config
  49. }