conf loader as component

pull/1028/merge
Darien Raymond 2018-04-09 11:43:13 +02:00
parent b7f2f30244
commit 286e9a3835
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 94 additions and 19 deletions

View File

@ -0,0 +1,21 @@
package confloader
import (
"io"
"os"
)
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg confloader -path Main,ConfLoader
type configFileLoader func(string) (io.ReadCloser, error)
var (
EffectiveConfigFileLoader configFileLoader
)
func LoadConfig(file string) (io.ReadCloser, error) {
if EffectiveConfigFileLoader == nil {
return os.Stdin, nil
}
return EffectiveConfigFileLoader(file)
}

View File

@ -0,0 +1,5 @@
package confloader
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Main", "ConfLoader") }

View File

@ -0,0 +1,5 @@
package external
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Main", "ConfLoader", "External") }

54
main/confloader/external/external.go vendored Normal file
View File

@ -0,0 +1,54 @@
package external
import (
"io"
"os"
"strings"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform/ctlcmd"
"v2ray.com/core/main/confloader"
)
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg external -path Main,ConfLoader,External
type ClosableMultiBuffer struct {
buf.MultiBuffer
}
func (c *ClosableMultiBuffer) Close() error {
c.MultiBuffer.Release()
return nil
}
func loadConfigFile(configFile string) (io.ReadCloser, error) {
if configFile == "stdin:" {
return os.Stdin, nil
}
if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
if err != nil {
return nil, err
}
return &ClosableMultiBuffer{content}, nil
}
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
return nil, newError("config file not readable").Base(err)
}
defer file.Close()
content, err := buf.ReadAllToMultiBuffer(file)
if err != nil {
return nil, newError("failed to load config file: ", fixedFile).Base(err).AtWarning()
}
return &ClosableMultiBuffer{content}, nil
}
func init() {
confloader.EffectiveConfigFileLoader = loadConfigFile
}

View File

@ -48,4 +48,7 @@ import (
// JSON config format // JSON config format
_ "v2ray.com/core/main/json" _ "v2ray.com/core/main/json"
// Load config from file or http(s)
_ "v2ray.com/core/main/confloader/external"
) )

View File

@ -5,7 +5,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@ -14,7 +13,7 @@ import (
"v2ray.com/core" "v2ray.com/core"
"v2ray.com/core/common/platform" "v2ray.com/core/common/platform"
"v2ray.com/core/common/platform/ctlcmd" "v2ray.com/core/main/confloader"
_ "v2ray.com/core/main/distro/all" _ "v2ray.com/core/main/distro/all"
) )
@ -61,24 +60,12 @@ func GetConfigFormat() string {
func startV2Ray() (core.Server, error) { func startV2Ray() (core.Server, error) {
configFile := getConfigFilePath() configFile := getConfigFilePath()
var configInput io.Reader configInput, err := confloader.LoadConfig(configFile)
if configFile == "stdin:" { if err != nil {
configInput = os.Stdin return nil, newError("failed to load config: ", configFile).Base(err)
} else if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
if err != nil {
return nil, err
}
configInput = &content
} else {
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
return nil, newError("config file not readable").Base(err)
}
defer file.Close()
configInput = file
} }
defer configInput.Close()
config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput) config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
if err != nil { if err != nil {
return nil, newError("failed to read config file: ", configFile).Base(err) return nil, newError("failed to read config file: ", configFile).Base(err)