Skip to content

Commit

Permalink
config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Oct 14, 2016
1 parent 84f660b commit 35a0ed6
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 63 deletions.
16 changes: 0 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package core

import (
"io"

"v2ray.com/core/common"
"v2ray.com/core/proxy/registry"
)

Expand Down Expand Up @@ -31,16 +28,3 @@ func (this *InboundConnectionConfig) GetAllocationStrategyValue() *AllocationStr
func (this *InboundConnectionConfig) GetTypedSettings() (interface{}, error) {
return registry.MarshalInboundConfig(this.Protocol, this.Settings)
}

type ConfigLoader func(input io.Reader) (*Config, error)

var (
configLoader ConfigLoader
)

func LoadConfig(input io.Reader) (*Config, error) {
if configLoader == nil {
return nil, common.ErrBadConfiguration
}
return configLoader(input)
}
117 changes: 70 additions & 47 deletions config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import "v2ray.com/core/transport/config.proto";

import "google/protobuf/any.proto";

enum ConfigFormat {
Protobuf = 0;
JSON = 1;
}

message AllocationStrategyConcurrency {
uint32 value = 1;
}
Expand Down
36 changes: 36 additions & 0 deletions config_loader.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 35a0ed6

Please sign in to comment.