-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.go
107 lines (83 loc) · 3.41 KB
/
config.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package app
import (
"fmt"
"github.com/go-ozzo/ozzo-validation"
"github.com/spf13/viper"
"github.com/tomochain/dex-server/swap/config"
"github.com/tomochain/dex-server/utils"
)
// Config stores the application-wide configurations
var Config appConfig
var logger = utils.Logger
// var logger = utils.NoopLogger
type appConfig struct {
// the path to the error message file. Defaults to "config/errors.yaml"
ErrorFile string `mapstructure:"error_file"`
// the server port. Defaults to 8080
ServerPort int `mapstructure:"server_port"`
// the data source name (MongoURL) for connecting to the database. required.
MongoURL string `mapstructure:"mongo_url"`
MongoDBPassword string `mapstructure:"mongo_password"`
MongoDBUsername string `mapstructure:"mongo_username"`
// simulate the environment
Simulated bool `mapstructure:"simulated"`
// the data source name (DSN) for connecting to the database. required.
DBName string `mapstructure:"db_name"`
// the make fee is the percentage to charged from maker
MakeFee float64 `mapstructure:"make_fee"`
// the take fee is the percentage to charged from maker
TakeFee float64 `mapstructure:"take_fee"`
// the RabbitMQURL is the URI of rabbitmq to use
RabbitMQURL string `mapstructure:"rabbitmq_url"`
// the signing method for JWT. Defaults to "HS256"
JWTSigningMethod string `mapstructure:"jwt_signing_method"`
// JWT signing key. required.
JWTSigningKey string `mapstructure:"jwt_signing_key"`
// JWT verification key. required.
JWTVerificationKey string `mapstructure:"jwt_verification_key"`
// TickDuration is user by tick streaming cron
TickDuration map[string][]int64 `mapstructure:"tick_duration"`
Logs map[string]string `mapstructure:"logs"`
Ethereum map[string]string `mapstructure:"ethereum"`
Deposit *config.Config `mapstructure:"deposit"`
CoinmarketcapAPIUrl string `mapstructure:"coinmarketcap_api_url"`
CoinmarketcapAPIKey string `mapstructure:"coinmarketcap_api_key"`
SupportedCurrencies string `mapstructure:"supported_currencies"`
}
func (config appConfig) Validate() error {
return validation.ValidateStruct(&config,
validation.Field(&config.MongoURL, validation.Required),
)
}
// LoadConfig loads configuration from the given list of paths and populates it into the Config variable.
// The configuration file(s) should be named as app.yaml.
// Environment variables with the prefix "RESTFUL_" in their names are also read automatically.
func LoadConfig(configPath string, env string) error {
v := viper.New()
if env != "" {
v.SetConfigName("config." + env)
}
v.SetConfigType("yaml")
v.AddConfigPath(configPath)
err := v.ReadInConfig()
if err != nil {
return fmt.Errorf("Failed to read the configuration file: %s", err)
}
v.SetEnvPrefix("tomo")
v.AutomaticEnv()
err = v.Unmarshal(&Config)
if err != nil {
return err
}
// // update config, if yaml does not presented this config, we can still apply from env
// Config.Simulated = v.GetBool("simulated")
// log information
logger.Infof("Server port: %v", Config.ServerPort)
logger.Infof("Ethereum node HTTP url: %v", Config.Ethereum["http_url"])
logger.Infof("Ethereum node WS url: %v", Config.Ethereum["ws_url"])
logger.Infof("MongoDB url: %v", Config.MongoURL)
logger.Infof("RabbitMQ url: %v", Config.RabbitMQURL)
logger.Infof("Exchange contract address: %v", Config.Ethereum["exchange_address"])
logger.Infof("Fee Account: %v", Config.Ethereum["fee_account"])
return Config.Validate()
}