mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-09-10 08:07:57 -04:00
config loader
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
type ConfigLoader func(input io.Reader) (*Config, error)
|
||||
|
||||
var configLoaderCache map[ConfigFormat]ConfigLoader
|
||||
|
||||
func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error {
|
||||
configLoaderCache[format] = loader
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
|
||||
loader, found := configLoaderCache[format]
|
||||
if !found {
|
||||
return nil, common.ErrBadConfiguration
|
||||
}
|
||||
return loader(input)
|
||||
}
|
||||
|
||||
func LoadProtobufConfig(input io.Reader) (*Config, error) {
|
||||
config := new(Config)
|
||||
data, _ := ioutil.ReadAll(input)
|
||||
if err := proto.Unmarshal(data, config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
Reference in New Issue
Block a user