Treasury bot + ledger fix
This commit is contained in:
@@ -3,6 +3,7 @@ package serverimp
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/tgsettle/internal/service/gateway"
|
||||
@@ -28,11 +29,17 @@ type Imp struct {
|
||||
config *config
|
||||
app *grpcapp.App[storage.Repository]
|
||||
service *gateway.Service
|
||||
|
||||
discoveryWatcher *discovery.RegistryWatcher
|
||||
discoveryReg *discovery.Registry
|
||||
}
|
||||
|
||||
type config struct {
|
||||
*grpcapp.Config `yaml:",inline"`
|
||||
Gateway gatewayConfig `yaml:"gateway"`
|
||||
Gateway gatewayConfig `yaml:"gateway"`
|
||||
Treasury treasuryConfig `yaml:"treasury"`
|
||||
Ledger ledgerConfig `yaml:"ledger"` // deprecated: use treasury.ledger
|
||||
Telegram telegramConfig `yaml:"telegram"` // deprecated: use treasury.telegram
|
||||
}
|
||||
|
||||
type gatewayConfig struct {
|
||||
@@ -43,6 +50,33 @@ type gatewayConfig struct {
|
||||
SuccessReaction string `yaml:"success_reaction"`
|
||||
}
|
||||
|
||||
type telegramConfig struct {
|
||||
AllowedChats []string `yaml:"allowed_chats"`
|
||||
Users []telegramUserConfig `yaml:"users"`
|
||||
}
|
||||
|
||||
type telegramUserConfig struct {
|
||||
TelegramUserID string `yaml:"telegram_user_id"`
|
||||
LedgerAccount string `yaml:"ledger_account"`
|
||||
}
|
||||
|
||||
type treasuryConfig struct {
|
||||
ExecutionDelay time.Duration `yaml:"execution_delay"`
|
||||
PollInterval time.Duration `yaml:"poll_interval"`
|
||||
Telegram telegramConfig `yaml:"telegram"`
|
||||
Ledger ledgerConfig `yaml:"ledger"`
|
||||
Limits treasuryLimitsConfig `yaml:"limits"`
|
||||
}
|
||||
|
||||
type treasuryLimitsConfig struct {
|
||||
MaxAmountPerOperation string `yaml:"max_amount_per_operation"`
|
||||
MaxDailyAmount string `yaml:"max_daily_amount"`
|
||||
}
|
||||
|
||||
type ledgerConfig struct {
|
||||
Timeout time.Duration `yaml:"timeout"`
|
||||
}
|
||||
|
||||
func Create(logger mlogger.Logger, file string, debug bool) (*Imp, error) {
|
||||
return &Imp{
|
||||
logger: logger.Named("server"),
|
||||
@@ -62,6 +96,9 @@ func (i *Imp) Shutdown() {
|
||||
if i.service != nil {
|
||||
i.service.Shutdown()
|
||||
}
|
||||
if i.discoveryWatcher != nil {
|
||||
i.discoveryWatcher.Stop()
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
i.app.Shutdown(ctx)
|
||||
@@ -81,6 +118,19 @@ func (i *Imp) Start() error {
|
||||
i.logger.Warn("Failed to create messaging broker", zap.Error(err))
|
||||
}
|
||||
}
|
||||
if broker != nil {
|
||||
registry := discovery.NewRegistry()
|
||||
watcher, watcherErr := discovery.NewRegistryWatcher(i.logger, broker, registry)
|
||||
if watcherErr != nil {
|
||||
i.logger.Warn("Failed to initialise discovery registry watcher", zap.Error(watcherErr))
|
||||
} else if startErr := watcher.Start(); startErr != nil {
|
||||
i.logger.Warn("Failed to start discovery registry watcher", zap.Error(startErr))
|
||||
} else {
|
||||
i.discoveryWatcher = watcher
|
||||
i.discoveryReg = registry
|
||||
i.logger.Info("Discovery registry watcher started")
|
||||
}
|
||||
}
|
||||
|
||||
repoFactory := func(logger mlogger.Logger, conn *db.MongoConnection) (storage.Repository, error) {
|
||||
return gatewaymongo.New(logger, conn)
|
||||
@@ -95,6 +145,8 @@ func (i *Imp) Start() error {
|
||||
if cfg.Messaging != nil {
|
||||
msgSettings = cfg.Messaging.Settings
|
||||
}
|
||||
treasuryTelegram := treasuryTelegramConfig(cfg, i.logger)
|
||||
treasuryLedger := treasuryLedgerConfig(cfg, i.logger)
|
||||
gwCfg := gateway.Config{
|
||||
Rail: cfg.Gateway.Rail,
|
||||
TargetChatIDEnv: cfg.Gateway.TargetChatIDEnv,
|
||||
@@ -103,6 +155,22 @@ func (i *Imp) Start() error {
|
||||
SuccessReaction: cfg.Gateway.SuccessReaction,
|
||||
InvokeURI: invokeURI,
|
||||
MessagingSettings: msgSettings,
|
||||
DiscoveryRegistry: i.discoveryReg,
|
||||
Treasury: gateway.TreasuryConfig{
|
||||
ExecutionDelay: cfg.Treasury.ExecutionDelay,
|
||||
PollInterval: cfg.Treasury.PollInterval,
|
||||
Telegram: gateway.TelegramConfig{
|
||||
AllowedChats: treasuryTelegram.AllowedChats,
|
||||
Users: telegramUsers(treasuryTelegram.Users),
|
||||
},
|
||||
Ledger: gateway.LedgerConfig{
|
||||
Timeout: treasuryLedger.Timeout,
|
||||
},
|
||||
Limits: gateway.TreasuryLimitsConfig{
|
||||
MaxAmountPerOperation: cfg.Treasury.Limits.MaxAmountPerOperation,
|
||||
MaxDailyAmount: cfg.Treasury.Limits.MaxDailyAmount,
|
||||
},
|
||||
},
|
||||
}
|
||||
svc := gateway.NewService(logger, repo, producer, broker, gwCfg)
|
||||
i.service = svc
|
||||
@@ -142,6 +210,15 @@ func (i *Imp) loadConfig() (*config, error) {
|
||||
if cfg.Metrics == nil {
|
||||
cfg.Metrics = &grpcapp.MetricsConfig{Address: ":9406"}
|
||||
}
|
||||
if cfg.Treasury.ExecutionDelay <= 0 {
|
||||
cfg.Treasury.ExecutionDelay = 30 * time.Second
|
||||
}
|
||||
if cfg.Treasury.PollInterval <= 0 {
|
||||
cfg.Treasury.PollInterval = 30 * time.Second
|
||||
}
|
||||
if cfg.Treasury.Ledger.Timeout <= 0 {
|
||||
cfg.Treasury.Ledger.Timeout = 5 * time.Second
|
||||
}
|
||||
cfg.Gateway.Rail = discovery.NormalizeRail(cfg.Gateway.Rail)
|
||||
if cfg.Gateway.Rail == "" {
|
||||
return nil, merrors.InvalidArgument("gateway rail is required", "gateway.rail")
|
||||
@@ -151,3 +228,46 @@ func (i *Imp) loadConfig() (*config, error) {
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func telegramUsers(input []telegramUserConfig) []gateway.TelegramUserBinding {
|
||||
result := make([]gateway.TelegramUserBinding, 0, len(input))
|
||||
for _, next := range input {
|
||||
result = append(result, gateway.TelegramUserBinding{
|
||||
TelegramUserID: strings.TrimSpace(next.TelegramUserID),
|
||||
LedgerAccount: strings.TrimSpace(next.LedgerAccount),
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func treasuryTelegramConfig(cfg *config, logger mlogger.Logger) telegramConfig {
|
||||
if cfg == nil {
|
||||
return telegramConfig{}
|
||||
}
|
||||
if len(cfg.Treasury.Telegram.Users) > 0 || len(cfg.Treasury.Telegram.AllowedChats) > 0 {
|
||||
return cfg.Treasury.Telegram
|
||||
}
|
||||
if len(cfg.Telegram.Users) > 0 || len(cfg.Telegram.AllowedChats) > 0 {
|
||||
if logger != nil {
|
||||
logger.Warn("Deprecated config path used: telegram.*; move these settings to treasury.telegram.*")
|
||||
}
|
||||
return cfg.Telegram
|
||||
}
|
||||
return cfg.Treasury.Telegram
|
||||
}
|
||||
|
||||
func treasuryLedgerConfig(cfg *config, logger mlogger.Logger) ledgerConfig {
|
||||
if cfg == nil {
|
||||
return ledgerConfig{}
|
||||
}
|
||||
if cfg.Treasury.Ledger.Timeout > 0 {
|
||||
return cfg.Treasury.Ledger
|
||||
}
|
||||
if cfg.Ledger.Timeout > 0 {
|
||||
if logger != nil {
|
||||
logger.Warn("Deprecated config path used: ledger.*; move these settings to treasury.ledger.*")
|
||||
}
|
||||
return cfg.Ledger
|
||||
}
|
||||
return cfg.Treasury.Ledger
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user