v2ray-core/config_loader.go

41 lines
850 B
Go
Raw Normal View History

2016-10-14 21:41:41 +00:00
package core
import (
"io"
"io/ioutil"
"v2ray.com/core/common"
"github.com/golang/protobuf/proto"
)
type ConfigLoader func(input io.Reader) (*Config, error)
2016-10-17 12:35:13 +00:00
var configLoaderCache = make(map[ConfigFormat]ConfigLoader)
2016-10-14 21:41:41 +00:00
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
}
2016-10-14 21:42:14 +00:00
func init() {
RegisterConfigLoader(ConfigFormat_Protobuf, LoadProtobufConfig)
}