[hw-3] loms service

This commit is contained in:
Никита Шубин
2025-06-20 10:11:59 +00:00
parent c8e056bc99
commit b88dfe6db5
73 changed files with 8837 additions and 52 deletions

View File

@@ -0,0 +1,74 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Service struct {
Host string `yaml:"host"`
GRPCPort string `yaml:"grpc_port"`
HTTPPort string `yaml:"http_port"`
LogLevel string `yaml:"log_level"`
} `yaml:"service"`
Jaeger struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
} `yaml:"jaeger"`
DatabaseMaster struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
DBName string `yaml:"db_name"`
} `yaml:"db_master"`
DatabaseReplica struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
DBName string `yaml:"db_name"`
} `yaml:"db_replica"`
Kafka struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
OrderTopic string `yaml:"order_topic"`
Brokers string `yaml:"brokers"`
} `yaml:"kafka"`
}
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
}