separated quotation and payments

This commit is contained in:
Stephan D
2026-02-10 18:29:47 +01:00
parent 6745bc0f6f
commit 296cc7b86a
163 changed files with 13516 additions and 191 deletions

View File

@@ -0,0 +1,101 @@
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"`
Oracle clientConfig `yaml:"oracle"`
Gateway clientConfig `yaml:"gateway"`
QuoteRetentionHrs int `yaml:"quote_retention_hours"`
}
type clientConfig struct {
Address string `yaml:"address"`
AddressEnv string `yaml:"address_env"`
DialTimeoutSecs int `yaml:"dial_timeout_seconds"`
CallTimeoutSecs int `yaml:"call_timeout_seconds"`
InsecureTransport bool `yaml:"insecure"`
}
func (c clientConfig) resolveAddress() string {
if address := strings.TrimSpace(c.Address); address != "" {
return address
}
if env := strings.TrimSpace(c.AddressEnv); env != "" {
return strings.TrimSpace(os.Getenv(env))
}
return ""
}
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 (c *config) quoteRetention() time.Duration {
if c == nil || c.QuoteRetentionHrs <= 0 {
return 72 * time.Hour
}
return time.Duration(c.QuoteRetentionHrs) * time.Hour
}
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: ":50064",
EnableReflection: true,
EnableHealth: true,
}
} else {
if strings.TrimSpace(cfg.GRPC.Address) == "" {
cfg.GRPC.Address = ":50064"
}
if strings.TrimSpace(cfg.GRPC.Network) == "" {
cfg.GRPC.Network = "tcp"
}
}
if cfg.Metrics == nil {
cfg.Metrics = &grpcapp.MetricsConfig{Address: ":9414"}
} else if strings.TrimSpace(cfg.Metrics.Address) == "" {
cfg.Metrics.Address = ":9414"
}
return cfg, nil
}

View 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(),
)
}

View File

@@ -0,0 +1,45 @@
package serverimp
import (
"strings"
"github.com/tech/sendico/payments/quotation/internal/appversion"
"github.com/tech/sendico/pkg/discovery"
msg "github.com/tech/sendico/pkg/messaging"
"go.uber.org/zap"
)
const quotationDiscoverySender = "payment_quotation"
func (i *Imp) startDiscoveryAnnouncer(cfg *config, producer msg.Producer) {
if i == nil || cfg == nil || producer == nil || cfg.GRPC == nil {
return
}
invokeURI := strings.TrimSpace(cfg.GRPC.DiscoveryInvokeURI())
if invokeURI == "" {
i.logger.Warn("Skipping discovery announcement: missing advertise host/port in gRPC config")
return
}
announce := discovery.Announcement{
Service: "PAYMENTS_QUOTATION",
Operations: []string{"payment.quote", "payment.multiquote"},
InvokeURI: invokeURI,
Version: appversion.Create().Short(),
}
i.discoveryAnnouncer = discovery.NewAnnouncer(i.logger, producer, quotationDiscoverySender, announce)
i.discoveryAnnouncer.Start()
i.logger.Info("Discovery announcer started",
zap.String("service", announce.Service),
zap.String("invoke_uri", announce.InvokeURI))
}
func (i *Imp) stopDiscoveryAnnouncer() {
if i == nil || i.discoveryAnnouncer == nil {
return
}
i.discoveryAnnouncer.Stop()
i.discoveryAnnouncer = nil
}

View File

@@ -0,0 +1,18 @@
package serverimp
import (
"context"
"time"
)
func (i *Imp) shutdownApp() {
if i.app != nil {
timeout := 15 * time.Second
if i.config != nil && i.config.Runtime != nil {
timeout = i.config.Runtime.ShutdownTimeout()
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
i.app.Shutdown(ctx)
cancel()
}
}

View File

@@ -0,0 +1,70 @@
package serverimp
import (
quotesvc "github.com/tech/sendico/payments/quotation/internal/service/orchestrator"
"github.com/tech/sendico/payments/storage"
mongostorage "github.com/tech/sendico/payments/storage/mongo"
"github.com/tech/sendico/pkg/db"
msg "github.com/tech/sendico/pkg/messaging"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/server/grpcapp"
)
func Create(logger mlogger.Logger, file string, debug bool) (*Imp, error) {
return &Imp{
logger: logger.Named("server"),
file: file,
debug: debug,
}, nil
}
func (i *Imp) Shutdown() {
i.stopDiscoveryAnnouncer()
if i.service != nil {
i.service.Shutdown()
}
i.shutdownApp()
i.closeDependencies()
}
func (i *Imp) Start() error {
cfg, err := i.loadConfig()
if err != nil {
return err
}
i.config = cfg
i.deps = i.initDependencies(cfg)
quoteRetention := cfg.quoteRetention()
repoFactory := func(logger mlogger.Logger, conn *db.MongoConnection) (storage.Repository, error) {
return mongostorage.New(logger, conn, mongostorage.WithQuoteRetention(quoteRetention))
}
serviceFactory := func(logger mlogger.Logger, repo storage.Repository, producer msg.Producer) (grpcapp.Service, error) {
opts := []quotesvc.Option{}
if i.deps != nil {
if i.deps.feesClient != nil {
opts = append(opts, quotesvc.WithFeeEngine(i.deps.feesClient, cfg.Fees.callTimeout()))
}
if i.deps.oracleClient != nil {
opts = append(opts, quotesvc.WithOracleClient(i.deps.oracleClient))
}
if i.deps.gatewayClient != nil {
opts = append(opts, quotesvc.WithChainGatewayClient(i.deps.gatewayClient))
}
}
i.startDiscoveryAnnouncer(cfg, producer)
svc := quotesvc.NewQuotationService(logger, repo, opts...)
i.service = svc
return svc, nil
}
app, err := grpcapp.NewApp(i.logger, "payments_quotation", cfg.Config, i.debug, repoFactory, serviceFactory)
if err != nil {
return err
}
i.app = app
return i.app.Start()
}

View File

@@ -0,0 +1,37 @@
package serverimp
import (
oracleclient "github.com/tech/sendico/fx/oracle/client"
chainclient "github.com/tech/sendico/gateway/chain/client"
"github.com/tech/sendico/payments/storage"
"github.com/tech/sendico/pkg/discovery"
"github.com/tech/sendico/pkg/mlogger"
feesv1 "github.com/tech/sendico/pkg/proto/billing/fees/v1"
"github.com/tech/sendico/pkg/server/grpcapp"
"google.golang.org/grpc"
)
type quoteService interface {
grpcapp.Service
Shutdown()
}
type clientDependencies struct {
feesConn *grpc.ClientConn
feesClient feesv1.FeeEngineClient
oracleClient oracleclient.Client
gatewayClient chainclient.Client
}
type Imp struct {
logger mlogger.Logger
file string
debug bool
config *config
app *grpcapp.App[storage.Repository]
service quoteService
deps *clientDependencies
discoveryAnnouncer *discovery.Announcer
}