mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 22:13:44 +03:00
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
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
|
|
}
|