[hw-1] implement cart service

This commit is contained in:
Никита Шубин
2025-05-25 15:49:17 +00:00
parent 3d3f10647b
commit 5077f04b0c
28 changed files with 1151 additions and 2 deletions

View File

@@ -0,0 +1,64 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Service struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
LogLevel string `yaml:"log_level"`
Workers int `yaml:"workers"`
} `yaml:"service"`
Jaeger struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
} `yaml:"jaeger"`
ProductService struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Token string `yaml:"token"`
Limit int `yaml:"limit"`
Burst int `yaml:"burst"`
} `yaml:"product_service"`
LomsService struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
} `yaml:"loms_service"`
}
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
}