Skip to content

Commit

Permalink
Merge branch 'master' of github.com:btcboost/copernicus into yyx
Browse files Browse the repository at this point in the history
  • Loading branch information
ludete committed May 3, 2018
2 parents 60147b3 + 18c5095 commit b284d6f
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 41 deletions.
19 changes: 19 additions & 0 deletions conf/conf.yml
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
62 changes: 62 additions & 0 deletions conf/conf_test.go
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)
// })
// })
//}
40 changes: 0 additions & 40 deletions conf/config.go

This file was deleted.

71 changes: 71 additions & 0 deletions conf/init.go
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)
//}
4 changes: 3 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import:
version: 211f780988068502fe874c44dae530528ebd840f
- package: github.com/spf13/viper
version: ^1.0.0
#- packege: gopkg.in/go-playground/validator.v8
#- package: github.com/smartystreets/goconvey/convey
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
subpackages:
- assert
- assert
Binary file removed output.png
Binary file not shown.

0 comments on commit b284d6f

Please sign in to comment.