139 lines
4.4 KiB
Go
139 lines
4.4 KiB
Go
package serverimp
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tech/sendico/pkg/api/routers"
|
|
"github.com/tech/sendico/pkg/server/grpcapp"
|
|
"go.uber.org/zap"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type config struct {
|
|
*grpcapp.Config `yaml:",inline"`
|
|
Fees clientConfig `yaml:"fees"`
|
|
Ledger clientConfig `yaml:"ledger"`
|
|
Gateway clientConfig `yaml:"gateway"`
|
|
PaymentGateway clientConfig `yaml:"payment_gateway"`
|
|
Mntx clientConfig `yaml:"mntx"`
|
|
Oracle clientConfig `yaml:"oracle"`
|
|
CardGateways map[string]cardGatewayRouteConfig `yaml:"card_gateways"`
|
|
FeeAccounts map[string]string `yaml:"fee_ledger_accounts"`
|
|
GatewayInstances []gatewayInstanceConfig `yaml:"gateway_instances"`
|
|
}
|
|
|
|
type clientConfig struct {
|
|
Address string `yaml:"address"`
|
|
DialTimeoutSecs int `yaml:"dial_timeout_seconds"`
|
|
CallTimeoutSecs int `yaml:"call_timeout_seconds"`
|
|
InsecureTransport bool `yaml:"insecure"`
|
|
}
|
|
|
|
type cardGatewayRouteConfig struct {
|
|
FundingAddress string `yaml:"funding_address"`
|
|
FeeAddress string `yaml:"fee_address"`
|
|
FeeWalletRef string `yaml:"fee_wallet_ref"`
|
|
}
|
|
|
|
type gatewayInstanceConfig struct {
|
|
ID string `yaml:"id"`
|
|
Rail string `yaml:"rail"`
|
|
Network string `yaml:"network"`
|
|
Currencies []string `yaml:"currencies"`
|
|
Capabilities gatewayCapabilitiesConfig `yaml:"capabilities"`
|
|
Limits limitsConfig `yaml:"limits"`
|
|
Version string `yaml:"version"`
|
|
IsEnabled *bool `yaml:"is_enabled"`
|
|
}
|
|
|
|
type gatewayCapabilitiesConfig struct {
|
|
CanPayIn bool `yaml:"can_pay_in"`
|
|
CanPayOut bool `yaml:"can_pay_out"`
|
|
CanReadBalance bool `yaml:"can_read_balance"`
|
|
CanSendFee bool `yaml:"can_send_fee"`
|
|
RequiresObserveConfirm bool `yaml:"requires_observe_confirm"`
|
|
CanBlock bool `yaml:"can_block"`
|
|
CanRelease bool `yaml:"can_release"`
|
|
}
|
|
|
|
type limitsConfig struct {
|
|
MinAmount string `yaml:"min_amount"`
|
|
MaxAmount string `yaml:"max_amount"`
|
|
PerTxMaxFee string `yaml:"per_tx_max_fee"`
|
|
PerTxMinAmount string `yaml:"per_tx_min_amount"`
|
|
PerTxMaxAmount string `yaml:"per_tx_max_amount"`
|
|
VolumeLimit map[string]string `yaml:"volume_limit"`
|
|
VelocityLimit map[string]int `yaml:"velocity_limit"`
|
|
CurrencyLimits map[string]limitsOverrideCfg `yaml:"currency_limits"`
|
|
}
|
|
|
|
type limitsOverrideCfg struct {
|
|
MaxVolume string `yaml:"max_volume"`
|
|
MinAmount string `yaml:"min_amount"`
|
|
MaxAmount string `yaml:"max_amount"`
|
|
MaxFee string `yaml:"max_fee"`
|
|
MaxOps int `yaml:"max_ops"`
|
|
}
|
|
|
|
func (c clientConfig) address() string {
|
|
return strings.TrimSpace(c.Address)
|
|
}
|
|
|
|
func (c clientConfig) dialTimeout() time.Duration {
|
|
if c.DialTimeoutSecs <= 0 {
|
|
return 5 * time.Second
|
|
}
|
|
return time.Duration(c.DialTimeoutSecs) * time.Second
|
|
}
|
|
|
|
func (c clientConfig) callTimeout() time.Duration {
|
|
if c.CallTimeoutSecs <= 0 {
|
|
return 3 * time.Second
|
|
}
|
|
return time.Duration(c.CallTimeoutSecs) * time.Second
|
|
}
|
|
|
|
func (i *Imp) loadConfig() (*config, error) {
|
|
data, err := os.ReadFile(i.file)
|
|
if err != nil {
|
|
i.logger.Error("Could not read configuration file", zap.String("config_file", i.file), zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
cfg := &config{Config: &grpcapp.Config{}}
|
|
if err := yaml.Unmarshal(data, cfg); err != nil {
|
|
i.logger.Error("Failed to parse configuration", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
if cfg.Runtime == nil {
|
|
cfg.Runtime = &grpcapp.RuntimeConfig{ShutdownTimeoutSeconds: 15}
|
|
}
|
|
|
|
if cfg.GRPC == nil {
|
|
cfg.GRPC = &routers.GRPCConfig{
|
|
Network: "tcp",
|
|
Address: ":50062",
|
|
EnableReflection: true,
|
|
EnableHealth: true,
|
|
}
|
|
} else {
|
|
if strings.TrimSpace(cfg.GRPC.Address) == "" {
|
|
cfg.GRPC.Address = ":50062"
|
|
}
|
|
if strings.TrimSpace(cfg.GRPC.Network) == "" {
|
|
cfg.GRPC.Network = "tcp"
|
|
}
|
|
}
|
|
|
|
if cfg.Metrics == nil {
|
|
cfg.Metrics = &grpcapp.MetricsConfig{Address: ":9403"}
|
|
} else if strings.TrimSpace(cfg.Metrics.Address) == "" {
|
|
cfg.Metrics.Address = ":9403"
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|