This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
/
config.go
172 lines (150 loc) · 3.72 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package config
import (
"errors"
"github.com/stellar/go/keypair"
"net/url"
"regexp"
)
// Config contains config params of the bridge server
type Config struct {
Port *int
Horizon string
Compliance string
LogFormat string `mapstructure:"log_format"`
MACKey string `mapstructure:"mac_key"`
APIKey string `mapstructure:"api_key"`
NetworkPassphrase string `mapstructure:"network_passphrase"`
Develop bool
Assets []Asset
Database struct {
Type string
URL string
}
Accounts
Callbacks
}
// Asset represents credit asset
type Asset struct {
Code string
Issuer string
}
// Accounts contains values of `accounts` config group
type Accounts struct {
AuthorizingSeed string `mapstructure:"authorizing_seed"`
BaseSeed string `mapstructure:"base_seed"`
IssuingAccountID string `mapstructure:"issuing_account_id"`
ReceivingAccountID string `mapstructure:"receiving_account_id"`
}
// Callbacks contains values of `callbacks` config group
type Callbacks struct {
Receive string
Error string
}
// Validate validates config and returns error if any of config values is incorrect
func (c *Config) Validate() (err error) {
if c.Port == nil {
err = errors.New("port param is required")
return
}
if c.Horizon == "" {
err = errors.New("horizon param is required")
return
}
_, err = url.Parse(c.Horizon)
if err != nil {
err = errors.New("Cannot parse horizon param")
return
}
if c.NetworkPassphrase == "" {
err = errors.New("network_passphrase param is required")
return
}
for _, asset := range c.Assets {
if asset.Issuer == "" {
if asset.Code != "XLM" {
err = errors.New("Issuer param is required for " + asset.Code)
return
}
}
if asset.Issuer != "" {
_, err = keypair.Parse(asset.Issuer)
if err != nil {
err = errors.New("Issuing account is invalid for " + asset.Code)
return
}
}
matched, err := regexp.MatchString("^[a-zA-Z0-9]{1,12}$", asset.Code)
if err != nil {
return err
}
if !matched {
err = errors.New("Invalid asset code: " + asset.Code)
return err
}
}
var dbURL *url.URL
dbURL, err = url.Parse(c.Database.URL)
if err != nil {
err = errors.New("Cannot parse database.url param")
return
}
switch c.Database.Type {
case "mysql":
// Add `parseTime=true` param to mysql url
query := dbURL.Query()
query.Set("parseTime", "true")
dbURL.RawQuery = query.Encode()
c.Database.URL = dbURL.String()
case "postgres":
break
case "":
// Allow to start gateway server with a single endpoint: /payment
break
default:
err = errors.New("Invalid database.type param")
return
}
if c.Accounts.AuthorizingSeed != "" {
_, err = keypair.Parse(c.Accounts.AuthorizingSeed)
if err != nil {
err = errors.New("accounts.authorizing_seed is invalid")
return
}
}
if c.Accounts.BaseSeed != "" {
_, err = keypair.Parse(c.Accounts.BaseSeed)
if err != nil {
err = errors.New("accounts.base_seed is invalid")
return
}
}
if c.Accounts.IssuingAccountID != "" {
_, err = keypair.Parse(c.Accounts.IssuingAccountID)
if err != nil {
err = errors.New("accounts.issuing_account_id is invalid")
return
}
}
if c.Accounts.ReceivingAccountID != "" {
_, err = keypair.Parse(c.Accounts.ReceivingAccountID)
if err != nil {
err = errors.New("accounts.receiving_account_id is invalid")
return
}
}
if c.Callbacks.Receive != "" {
_, err = url.Parse(c.Callbacks.Receive)
if err != nil {
err = errors.New("Cannot parse callbacks.receive param")
return
}
}
if c.Callbacks.Error != "" {
_, err = url.Parse(c.Callbacks.Error)
if err != nil {
err = errors.New("Cannot parse callbacks.error param")
return
}
}
return
}