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

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 {
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
}