[hw-8] add: comment service

This commit is contained in:
3ybacTuK
2025-07-26 23:47:18 +03:00
parent 6420eaf3d7
commit 6e0d90a6d5
29 changed files with 1249 additions and 725 deletions

View File

@@ -0,0 +1,59 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v3"
)
type Config struct {
App struct {
EditInterval time.Duration `yaml:"edit_interval"`
}
Service struct {
Host string `yaml:"host"`
GRPCPort string `yaml:"grpc_port"`
HTTPPort string `yaml:"http_port"`
LogLevel string `yaml:"log_level"`
} `yaml:"service"`
DbShards []struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
DBName string `yaml:"db_name"`
} `yaml:"db_shards"`
}
func LoadConfig(filename string) (*Config, error) {
workDir, err := os.Getwd()
if err != nil {
return nil, err
}
cfgRoot := filepath.Join(workDir, "configs")
absCfgRoot, _ := filepath.Abs(cfgRoot)
filePath := filepath.Join(absCfgRoot, filepath.Clean(filename))
if !strings.HasPrefix(filePath, absCfgRoot) {
return nil, fmt.Errorf("invalid path")
}
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
config := &Config{}
if err := yaml.NewDecoder(f).Decode(config); err != nil {
return nil, err
}
return config, nil
}