config.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package modules
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Config struct {
  8. Website WebsiteConfig `yaml:"website"`
  9. OpenWeatherMap OpenWeatherMapConfig `yaml:"openweathermap"`
  10. Addons []string `yaml:"addons"`
  11. }
  12. type WebsiteConfig struct {
  13. Title string `yaml:"title"`
  14. Description string `yaml:"description"`
  15. Language string `yaml:"language"`
  16. Localization string `yaml:"localization"`
  17. UseMetric bool `yaml:"useMetric"`
  18. Theme string `yaml:"theme"`
  19. }
  20. type OpenWeatherMapConfig struct {
  21. ApiKey string `yaml:"apiKey"`
  22. Longitude string `yaml:"lon"`
  23. Latitude string `yaml:"lat"`
  24. }
  25. var AppConfig Config
  26. func LoadConfig() {
  27. defaultConfig := Config{
  28. Website: WebsiteConfig{
  29. Title: "Magma Dashboard",
  30. Description: "",
  31. Language: "en",
  32. Localization: "en-us",
  33. UseMetric: true,
  34. Theme: "flame",
  35. },
  36. Addons: []string{},
  37. }
  38. AppConfig = defaultConfig
  39. yamlFile, err := ReadFile(filepath.Join("data", "config.yaml"))
  40. if err != nil {
  41. fmt.Printf("Error reading YAML file: %s\n", err)
  42. return
  43. }
  44. var yamlConfig Config
  45. err = yaml.Unmarshal(yamlFile, &yamlConfig)
  46. if err != nil {
  47. fmt.Printf("Error parsing YAML file: %s\n", err)
  48. return
  49. }
  50. fmt.Println("Loaded config:", yamlConfig)
  51. AppConfig = yamlConfig
  52. }