переписал конфиг чуть чуть, разбил на встраиваемые структуры

design
serr 2025-06-08 15:55:23 +03:00
parent ca6bd5bd63
commit 6d2403d2f5
1 changed files with 42 additions and 26 deletions

View File

@ -11,32 +11,48 @@ const (
) )
type Config struct { type Config struct {
PostsDir string pathsConfig
PostsMaxCountOnPage int serverConfig
AssetsDir string lastFMConfig
TemplatesDir string cacheConfig
TemplatesExt string
LocalIP string
LocalPort string
ServerIP string
ServerPort string
ServerDomain string
Port string
LastFMUsername string
LastFMToken string
LastFMUpdateInterval time.Duration
CacheLogInterval time.Duration
} }
func loadConfig(configPath string) (*Config, error) { type pathsConfig struct {
cfg := &Config{} PostsDir string `json:"posts_dir"`
configFile, err := os.ReadFile(configPath) AssetsDir string `json:"assets_dir"`
if err != nil { TemplatesDir string `json:"templates_dir"`
return nil, err TemplatesExt string `json:"templates_ext"`
} PostsMaxCountOnPage int `json:"posts_max_count"`
err = json.Unmarshal(configFile, cfg) }
if err != nil {
return nil, err type serverConfig struct {
} LocalIP string `json:"local_ip"`
return cfg, nil 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
} }