177 lines
4.7 KiB
Go
177 lines
4.7 KiB
Go
package serverimp
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
|
|
oracleclient "github.com/tech/sendico/fx/oracle/client"
|
|
chainclient "github.com/tech/sendico/gateway/chain/client"
|
|
mntxclient "github.com/tech/sendico/gateway/mntx/client"
|
|
ledgerclient "github.com/tech/sendico/ledger/client"
|
|
feesv1 "github.com/tech/sendico/pkg/proto/billing/fees/v1"
|
|
"go.uber.org/zap"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
func (i *Imp) initFeesClient(cfg clientConfig) (feesv1.FeeEngineClient, *grpc.ClientConn) {
|
|
addr := cfg.address()
|
|
if addr == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
dialCtx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout())
|
|
defer cancel()
|
|
|
|
creds := credentials.NewTLS(&tls.Config{})
|
|
if cfg.InsecureTransport {
|
|
creds = insecure.NewCredentials()
|
|
}
|
|
|
|
conn, err := grpc.DialContext(dialCtx, addr, grpc.WithTransportCredentials(creds))
|
|
if err != nil {
|
|
i.logger.Warn("Failed to connect to fees service", zap.String("address", addr), zap.Error(err))
|
|
return nil, nil
|
|
}
|
|
|
|
i.logger.Info("Connected to fees service", zap.String("address", addr))
|
|
return feesv1.NewFeeEngineClient(conn), conn
|
|
}
|
|
|
|
func (i *Imp) initLedgerClient(cfg clientConfig) ledgerclient.Client {
|
|
addr := cfg.address()
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout())
|
|
defer cancel()
|
|
|
|
client, err := ledgerclient.New(ctx, ledgerclient.Config{
|
|
Address: addr,
|
|
DialTimeout: cfg.dialTimeout(),
|
|
CallTimeout: cfg.callTimeout(),
|
|
Insecure: cfg.InsecureTransport,
|
|
})
|
|
if err != nil {
|
|
i.logger.Warn("Failed to connect to ledger service", zap.String("address", addr), zap.Error(err))
|
|
return nil
|
|
}
|
|
i.logger.Info("Connected to ledger service", zap.String("address", addr))
|
|
return client
|
|
}
|
|
|
|
func (i *Imp) initGatewayClient(cfg clientConfig) chainclient.Client {
|
|
addr := cfg.address()
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout())
|
|
defer cancel()
|
|
|
|
client, err := chainclient.New(ctx, chainclient.Config{
|
|
Address: addr,
|
|
DialTimeout: cfg.dialTimeout(),
|
|
CallTimeout: cfg.callTimeout(),
|
|
Insecure: cfg.InsecureTransport,
|
|
})
|
|
if err != nil {
|
|
i.logger.Warn("failed to connect to chain gateway service", zap.String("address", addr), zap.Error(err))
|
|
return nil
|
|
}
|
|
i.logger.Info("connected to chain gateway service", zap.String("address", addr))
|
|
return client
|
|
}
|
|
|
|
func (i *Imp) initPaymentGatewayClient(cfg clientConfig) chainclient.Client {
|
|
addr := cfg.address()
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout())
|
|
defer cancel()
|
|
|
|
client, err := chainclient.New(ctx, chainclient.Config{
|
|
Address: addr,
|
|
DialTimeout: cfg.dialTimeout(),
|
|
CallTimeout: cfg.callTimeout(),
|
|
Insecure: cfg.InsecureTransport,
|
|
})
|
|
if err != nil {
|
|
i.logger.Warn("failed to connect to payment gateway service", zap.String("address", addr), zap.Error(err))
|
|
return nil
|
|
}
|
|
i.logger.Info("connected to payment gateway service", zap.String("address", addr))
|
|
return client
|
|
}
|
|
|
|
func (i *Imp) initMntxClient(cfg clientConfig) mntxclient.Client {
|
|
addr := cfg.address()
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout())
|
|
defer cancel()
|
|
|
|
client, err := mntxclient.New(ctx, mntxclient.Config{
|
|
Address: addr,
|
|
DialTimeout: cfg.dialTimeout(),
|
|
CallTimeout: cfg.callTimeout(),
|
|
Logger: i.logger.Named("client.mntx"),
|
|
})
|
|
if err != nil {
|
|
i.logger.Warn("Failed to connect to mntx gateway service", zap.String("address", addr), zap.Error(err))
|
|
return nil
|
|
}
|
|
i.logger.Info("Connected to mntx gateway service", zap.String("address", addr))
|
|
return client
|
|
}
|
|
|
|
func (i *Imp) initOracleClient(cfg clientConfig) oracleclient.Client {
|
|
addr := cfg.address()
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout())
|
|
defer cancel()
|
|
|
|
client, err := oracleclient.New(ctx, oracleclient.Config{
|
|
Address: addr,
|
|
DialTimeout: cfg.dialTimeout(),
|
|
CallTimeout: cfg.callTimeout(),
|
|
Insecure: cfg.InsecureTransport,
|
|
})
|
|
if err != nil {
|
|
i.logger.Warn("Failed to connect to oracle service", zap.String("address", addr), zap.Error(err))
|
|
return nil
|
|
}
|
|
i.logger.Info("Connected to oracle service", zap.String("address", addr))
|
|
return client
|
|
}
|
|
|
|
func (i *Imp) closeClients() {
|
|
if i.ledgerClient != nil {
|
|
_ = i.ledgerClient.Close()
|
|
}
|
|
if i.gatewayClient != nil {
|
|
_ = i.gatewayClient.Close()
|
|
}
|
|
if i.paymentGatewayClient != nil {
|
|
_ = i.paymentGatewayClient.Close()
|
|
}
|
|
if i.mntxClient != nil {
|
|
_ = i.mntxClient.Close()
|
|
}
|
|
if i.oracleClient != nil {
|
|
_ = i.oracleClient.Close()
|
|
}
|
|
if i.feesConn != nil {
|
|
_ = i.feesConn.Close()
|
|
}
|
|
}
|