change config file load logic

This commit is contained in:
yuzuki999
2022-09-13 11:08:20 +08:00
parent 53b330b4e8
commit 3682114f06
6 changed files with 105 additions and 113 deletions

View File

@@ -1,13 +1,20 @@
package conf
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
"path"
)
type Conf struct {
LogConfig *LogConfig `mapstructure:"Log"`
DnsConfigPath string `mapstructure:"DnsConfigPath"`
InboundConfigPath string `mapstructure:"InboundConfigPath"`
OutboundConfigPath string `mapstructure:"OutboundConfigPath"`
RouteConfigPath string `mapstructure:"RouteConfigPath"`
ConnectionConfig *ConnetionConfig `mapstructure:"ConnectionConfig"`
NodesConfig []*NodeConfig `mapstructure:"Nodes"`
LogConfig *LogConfig `yaml:"Log"`
DnsConfigPath string `yaml:"DnsConfigPath"`
InboundConfigPath string `yaml:"InboundConfigPath"`
OutboundConfigPath string `yaml:"OutboundConfigPath"`
RouteConfigPath string `yaml:"RouteConfigPath"`
ConnectionConfig *ConnetionConfig `yaml:"ConnectionConfig"`
NodesConfig []*NodeConfig `yaml:"Nodes"`
}
func New() *Conf {
@@ -21,3 +28,18 @@ func New() *Conf {
NodesConfig: []*NodeConfig{},
}
}
func (p *Conf) LoadFromPath(filePath string) error {
confPath := path.Dir(filePath)
os.Setenv("XRAY_LOCATION_ASSET", confPath)
os.Setenv("XRAY_LOCATION_CONFIG", confPath)
f, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("open config file error: %v", err)
}
err = yaml.NewDecoder(f).Decode(p)
if err != nil {
return fmt.Errorf("decode config error: %v", err)
}
return nil
}