forked from btcboost/copernicus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:btcboost/copernicus into yyx
- Loading branch information
Showing
6 changed files
with
155 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
go_version: 1.9.2 | ||
version: 1.0.0 | ||
build_date: 20180428 | ||
|
||
service: | ||
address: 10.0.0.0/8 | ||
|
||
http: | ||
host: 127.0.0.1 | ||
port: 8080 | ||
mode: test | ||
|
||
rpc: | ||
host: 127.0.0.1 | ||
port: 9552 | ||
|
||
log: | ||
level: error | ||
format: json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package conf | ||
// | ||
//import ( | ||
// "fmt" | ||
// "io/ioutil" | ||
// "math/rand" | ||
// "os" | ||
// "testing" | ||
// | ||
// . "github.com/smartystreets/goconvey/convey" | ||
//) | ||
// | ||
//var confData = []byte(` | ||
//go_version: 1.9.2 | ||
//version: 1.0.0 | ||
//build_date: 20180428 | ||
//service: | ||
// address: 10.0.0.0/8 | ||
//http: | ||
// host: 127.0.0.1 | ||
// port: 8080 | ||
// mode: test | ||
//rpc: | ||
// host: 127.0.0.1 | ||
// port: 9552 | ||
//log: | ||
// level: error | ||
// format: json | ||
//`) | ||
// | ||
//func TestInitConfig(t *testing.T) { | ||
// Convey("Given config file", t, func() { | ||
// filename := fmt.Sprintf("conf_test%04d.yml", rand.Intn(9999)) | ||
// os.Setenv(ConfEnv, filename) | ||
// ioutil.WriteFile(filename, confData, 0664) | ||
// | ||
// Convey("When init configuration", func() { | ||
// config := initConfig() | ||
// | ||
// Convey("Configuration should resemble default configuration", func() { | ||
// expected := &Configuration{} | ||
// expected.Service.Address = "10.0.0.0/8" | ||
// expected.HTTP.Host = "127.0.0.1" | ||
// expected.HTTP.Port = 8080 | ||
// expected.HTTP.Mode = "test" | ||
// expected.Log.Format = "json" | ||
// expected.Log.Level = "error" | ||
// expected.GoVersion = "1.9.2" | ||
// expected.Version = "1.0.0" | ||
// expected.BuildDate = "20180428" | ||
// expected.RPC.Host = "127.0.0.1" | ||
// expected.RPC.Port = 9552 | ||
// So(config, ShouldResemble, expected) | ||
// }) | ||
// }) | ||
// | ||
// Reset(func() { | ||
// os.Unsetenv(ConfEnv) | ||
// os.Remove(filename) | ||
// }) | ||
// }) | ||
//} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package conf | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/viper" | ||
//"gopkg.in/go-playground/validator.v8" | ||
) | ||
|
||
const ( | ||
ConfEnv = "DSP_ALLOT_CONF" | ||
) | ||
|
||
func initConfig() *Configuration { | ||
config := &Configuration{} | ||
|
||
viper.SetEnvPrefix("copernicus") | ||
viper.AutomaticEnv() | ||
viper.SetConfigType("yaml") | ||
viper.SetDefault("conf", "./conf.yml") | ||
|
||
// get config file path from environment | ||
conf := viper.GetString("conf") | ||
|
||
// parse config | ||
file := Must(os.Open(conf)).(*os.File) | ||
defer file.Close() | ||
|
||
Must(nil, viper.ReadConfig(file)) | ||
Must(nil, viper.Unmarshal(config)) | ||
//TODO: | ||
//Must(nil, config.Validate()) | ||
|
||
return config | ||
} | ||
|
||
type Configuration struct { | ||
GoVersion string `mapstructure:"go_version" validate:"required"` | ||
Version string `mapstructure:"version" validate:"required"` | ||
BuildDate string `mapstructure:"build_date" validate:"required"` | ||
Service struct { | ||
Address string `mapstructure:"address" validate:"required,cidr"` | ||
} `mapstructure:"service" validate:"required"` | ||
HTTP struct { | ||
Host string `mapstructure:"host" validate:"required,ip"` | ||
Port int `mapstructure:"port" validate:"required"` | ||
Mode string `mapstructure:"mode" validate:"required,eq=release|eq=test|eq=debug"` | ||
} `mapstructure:"http" validate:"required"` | ||
RPC struct { | ||
Host string `mapstructure:"host" validate:"required,ip"` | ||
Port int `mapstructure:"port" validate:"required"` | ||
} `mapstructure:"rpc" validate:"required"` | ||
Log struct { | ||
Level string `mapstructure:"level" validate:"required,eq=debug|eq=info|eq=warn|eq=error|eq=fatal|eq=panic"` | ||
Format string `mapstructure:"format" validate:"required,eq=text|eq=json"` | ||
} `mapstructure:"log" validate:"required"` | ||
} | ||
|
||
func Must(i interface{}, err error) interface{} { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return i | ||
} | ||
|
||
// Validate validates configuration | ||
//func (c Configuration) Validate() error { | ||
// validate := validator.New(&validator.Config{TagName: "validate"}) | ||
// | ||
// return validate.Struct(c) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.