forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_loader.go
40 lines (31 loc) · 850 Bytes
/
config_loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 = make(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
}
func init() {
RegisterConfigLoader(ConfigFormat_Protobuf, LoadProtobufConfig)
}