separated quotation and payments
This commit is contained in:
107
api/payments/quotation/internal/server/internal/dependencies.go
Normal file
107
api/payments/quotation/internal/server/internal/dependencies.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package serverimp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
oracleclient "github.com/tech/sendico/fx/oracle/client"
|
||||
chainclient "github.com/tech/sendico/gateway/chain/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) initDependencies(cfg *config) *clientDependencies {
|
||||
deps := &clientDependencies{}
|
||||
if cfg == nil {
|
||||
return deps
|
||||
}
|
||||
|
||||
if feesAddress := cfg.Fees.resolveAddress(); feesAddress != "" {
|
||||
dialCtx, cancel := context.WithTimeout(context.Background(), cfg.Fees.dialTimeout())
|
||||
conn, err := dialGRPC(dialCtx, cfg.Fees, feesAddress)
|
||||
cancel()
|
||||
if err != nil {
|
||||
i.logger.Warn("Failed to dial fee engine", zap.Error(err), zap.String("address", feesAddress))
|
||||
} else {
|
||||
deps.feesConn = conn
|
||||
deps.feesClient = feesv1.NewFeeEngineClient(conn)
|
||||
}
|
||||
}
|
||||
|
||||
if oracleAddress := cfg.Oracle.resolveAddress(); oracleAddress != "" {
|
||||
client, err := oracleclient.New(context.Background(), oracleclient.Config{
|
||||
Address: oracleAddress,
|
||||
DialTimeout: cfg.Oracle.dialTimeout(),
|
||||
CallTimeout: cfg.Oracle.callTimeout(),
|
||||
Insecure: cfg.Oracle.InsecureTransport,
|
||||
})
|
||||
if err != nil {
|
||||
i.logger.Warn("Failed to initialise oracle client", zap.Error(err), zap.String("address", oracleAddress))
|
||||
} else {
|
||||
deps.oracleClient = client
|
||||
}
|
||||
}
|
||||
|
||||
if gatewayAddress := cfg.Gateway.resolveAddress(); gatewayAddress != "" {
|
||||
client, err := chainclient.New(context.Background(), chainclient.Config{
|
||||
Address: gatewayAddress,
|
||||
DialTimeout: cfg.Gateway.dialTimeout(),
|
||||
CallTimeout: cfg.Gateway.callTimeout(),
|
||||
Insecure: cfg.Gateway.InsecureTransport,
|
||||
})
|
||||
if err != nil {
|
||||
i.logger.Warn("Failed to initialise chain gateway client", zap.Error(err), zap.String("address", gatewayAddress))
|
||||
} else {
|
||||
deps.gatewayClient = client
|
||||
}
|
||||
}
|
||||
|
||||
return deps
|
||||
}
|
||||
|
||||
func (i *Imp) closeDependencies() {
|
||||
if i.deps == nil {
|
||||
return
|
||||
}
|
||||
if i.deps.oracleClient != nil {
|
||||
_ = i.deps.oracleClient.Close()
|
||||
i.deps.oracleClient = nil
|
||||
}
|
||||
if i.deps.gatewayClient != nil {
|
||||
_ = i.deps.gatewayClient.Close()
|
||||
i.deps.gatewayClient = nil
|
||||
}
|
||||
if i.deps.feesConn != nil {
|
||||
_ = i.deps.feesConn.Close()
|
||||
i.deps.feesConn = nil
|
||||
}
|
||||
}
|
||||
|
||||
func dialGRPC(ctx context.Context, cfg clientConfig, address string) (*grpc.ClientConn, error) {
|
||||
address = strings.TrimSpace(address)
|
||||
if ctx == nil {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
}
|
||||
if cfg.InsecureTransport {
|
||||
return grpc.DialContext(
|
||||
ctx,
|
||||
address,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithBlock(),
|
||||
)
|
||||
}
|
||||
|
||||
return grpc.DialContext(
|
||||
ctx,
|
||||
address,
|
||||
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
|
||||
grpc.WithBlock(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user