diff --git a/mvc/models/config.go b/mvc/models/config.go index 0c7b74a..faf36f4 100644 --- a/mvc/models/config.go +++ b/mvc/models/config.go @@ -11,32 +11,48 @@ const ( ) type Config struct { - PostsDir string - PostsMaxCountOnPage int - AssetsDir string - TemplatesDir string - TemplatesExt string - LocalIP string - LocalPort string - ServerIP string - ServerPort string - ServerDomain string - Port string - LastFMUsername string - LastFMToken string - LastFMUpdateInterval time.Duration - CacheLogInterval time.Duration + pathsConfig + serverConfig + lastFMConfig + cacheConfig } -func loadConfig(configPath string) (*Config, error) { - cfg := &Config{} - configFile, err := os.ReadFile(configPath) - if err != nil { - return nil, err - } - err = json.Unmarshal(configFile, cfg) - if err != nil { - return nil, err - } - return cfg, nil +type pathsConfig struct { + PostsDir string `json:"posts_dir"` + AssetsDir string `json:"assets_dir"` + TemplatesDir string `json:"templates_dir"` + TemplatesExt string `json:"templates_ext"` + PostsMaxCountOnPage int `json:"posts_max_count"` +} + +type serverConfig struct { + LocalIP string `json:"local_ip"` + LocalPort string `json:"local_port"` + ServerIP string `json:"server_ip"` + ServerPort string `json:"server_port"` + ServerDomain string `json:"server_domain"` +} + +type lastFMConfig struct { + LastFMUsername string `json:"lastfm_username"` + LastFMToken string `json:"lastfm_token"` + LastFMUpdateInterval time.Duration `json:"lastfm_update_interval"` +} + +type cacheConfig struct { + CacheLogInterval time.Duration `json:"cache_log_interval"` +} + +func loadConfig(path string) (*Config, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + var cfg Config + if err := json.Unmarshal(data, &cfg); err != nil { + return nil, err + } + + return &cfg, nil }