Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config rpc enable flags, fixed logger setup #61

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions cmd/camino_messenger_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,29 @@ var rootCmd = &cobra.Command{
}

func rootFunc(cmd *cobra.Command, _ []string) error {
configReader := config.NewConfigReader(cmd.Flags())
configReaderLogger, err := zap.NewProduction()
if err != nil {
return fmt.Errorf("failed to create config-reader logger: %w", err)
}

sugaredConfigReaderLogger := configReaderLogger.Sugar()
defer func() { _ = sugaredConfigReaderLogger.Sync() }()

configReader, err := config.NewConfigReader(cmd.Flags(), sugaredConfigReaderLogger)
if err != nil {
return fmt.Errorf("failed to create config reader: %w", err)
}

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

cfg, err := configReader.ReadConfig()
if err != nil {
return fmt.Errorf("failed to read config: %w", err)
}

_ = sugaredConfigReaderLogger.Sync()

var err error
var zapLogger *zap.Logger
if configReader.IsDevelopmentMode() {
zapLogger, err = zap.NewDevelopment()
Expand All @@ -46,15 +66,6 @@ func rootFunc(cmd *cobra.Command, _ []string) error {

logger.Infof("App version: %s (git: %s)", Version, GitCommit)

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

cfg, err := configReader.ReadConfig(logger)
if err != nil {
logger.Error(err)
return err
}

app, err := app.NewApp(ctx, cfg, logger)
if err != nil {
logger.Error(err)
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type TracingConfig struct {
}

type PartnerPluginConfig struct {
Enabled bool
HostURL url.URL
Unencrypted bool
CACertFile string
Expand All @@ -70,6 +71,7 @@ type DBConfig struct {
}

type RPCServerConfig struct {
Enabled bool `mapstructure:"enabled"`
Port uint64 `mapstructure:"port"`
Unencrypted bool `mapstructure:"unencrypted"`
ServerCertFile string `mapstructure:"cert_file"`
Expand Down Expand Up @@ -115,6 +117,7 @@ type UnparsedTracingConfig struct {
}

type UnparsedPartnerPluginConfig struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Unencrypted bool `mapstructure:"unencrypted"`
CACertFile string `mapstructure:"ca_file"`
Expand All @@ -137,6 +140,7 @@ func (cfg *Config) unparse() *UnparsedConfig {
KeyFile: cfg.Tracing.KeyFile,
},
PartnerPlugin: UnparsedPartnerPluginConfig{
Enabled: cfg.PartnerPlugin.Enabled,
Host: cfg.PartnerPlugin.HostURL.String(),
Unencrypted: cfg.PartnerPlugin.Unencrypted,
CACertFile: cfg.PartnerPlugin.CACertFile,
Expand Down
16 changes: 8 additions & 8 deletions config/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ var (

type Reader interface {
IsDevelopmentMode() bool
ReadConfig(logger *zap.SugaredLogger) (*Config, error)
ReadConfig() (*Config, error)
}

// Returns a new config reader.
func NewConfigReader(flags *pflag.FlagSet) Reader {
func NewConfigReader(flags *pflag.FlagSet, logger *zap.SugaredLogger) (Reader, error) {
return &reader{
viper: viper.New(),
flags: flags,
}
viper: viper.New(),
flags: flags,
logger: logger,
}, nil
}

type reader struct {
Expand All @@ -47,9 +48,7 @@ func (cr *reader) IsDevelopmentMode() bool {
return cr.viper.GetBool(flagKeyDeveloperMode)
}

func (cr *reader) ReadConfig(logger *zap.SugaredLogger) (*Config, error) {
cr.logger = logger

func (cr *reader) ReadConfig() (*Config, error) {
cr.viper.SetEnvPrefix(envPrefix)
cr.viper.AutomaticEnv()
cr.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
Expand Down Expand Up @@ -131,6 +130,7 @@ func (cr *reader) parseConfig(cfg *UnparsedConfig) (*Config, error) {
KeyFile: cfg.Tracing.KeyFile,
},
PartnerPlugin: PartnerPluginConfig{
Enabled: cfg.PartnerPlugin.Enabled,
HostURL: *partnerPluginHost,
Unencrypted: cfg.PartnerPlugin.Unencrypted,
CACertFile: cfg.PartnerPlugin.CACertFile,
Expand Down
5 changes: 3 additions & 2 deletions config/config_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ func TestReadConfig(t *testing.T) {

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
cr := NewConfigReader(tt.flags)
cr, err := NewConfigReader(tt.flags, zap.NewNop().Sugar())
require.NoError(t, err)
tt.prepare(t, cr.(*reader))

config, err := cr.ReadConfig(zap.NewNop().Sugar())
config, err := cr.ReadConfig()
require.ErrorIs(t, err, tt.expectedErr)

require.NoError(t, unsetEnvFromMap(envPrefix, rawMap))
Expand Down
2 changes: 2 additions & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ func Flags() *pflag.FlagSet {
flags.String("tracing.key_file", "", "The tracing key file.")

// Partner plugin config flags
flags.Bool("partner_plugin.enabled", false, "Enable or disable the partner plugin rpc client. It must be enabled if bot's cm account supports at least one service.")
flags.String("partner_plugin.host", "localhost:50051", "partner plugin RPC server host.")
flags.Bool("partner_plugin.unencrypted", false, "Whether the RPC client should initiate an unencrypted connection with the server.")
flags.String("partner_plugin.ca_file", "", "The partner plugin RPC server CA certificate file.")

// RPC server config flags
flags.Bool("rpc_server.enabled", false, "Enable or disable RPC server. It must be enabled if bot is expecting to receive RPC requests (e.g. its distributor bot).")
flags.Uint64("rpc_server.port", 9090, "The RPC server port.")
flags.Bool("rpc_server.unencrypted", false, "Whether the RPC server should be unencrypted.")
flags.String("rpc_server.cert_file", "", "The server certificate file.")
Expand Down
2 changes: 2 additions & 0 deletions config/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ network_fee_recipient_bot_address: 0xff6BAC3d972680515cbB59fCB6Db6deB13Eb0E91
network_fee_recipient_cm_account: 0xF6bA5c68A505559c170dC7a30448Ed64D8b9Bc3B
partner_plugin:
ca_file: ca-cert.pem
enabled: true
host: localhost:50051
unencrypted: true
response_timeout: 10000
rpc_server:
cert_file: server-cert.pem
enabled: true
key_file: server-key.pem
port: 9090
unencrypted: true
Expand Down
6 changes: 6 additions & 0 deletions examples/config/camino-messenger-bot-distributor-camino.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ matrix:

### Partner Plugin (NOT USED FOR DISTRIBUTOR BOT)
# partner_plugin:
# # Enable or disable the partner plugin rpc client. It must be enabled if bot's cm account supports at least one service.
# enabled: false

# # Partner Plugin hostname and port, should be reachable from this machine.
# # Bot tries to connect to this host and port to relay messages that it receives from
# # the distributors through Matrix Server
Expand All @@ -86,6 +89,9 @@ matrix:

### RPC server
rpc_server:
# Enable or disable RPC server. It must be enabled if bot is expecting to receive RPC requests (e.g. its distributor bot).
enabled: true

# Listen on this port for incoming RPC requests
port: 9090

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ matrix:

### Partner Plugin (NOT USED FOR DISTRIBUTOR BOT)
# partner_plugin:
# # Enable or disable the partner plugin rpc client. It must be enabled if bot's cm account supports at least one service.
# enabled: false

# # Partner Plugin hostname and port, should be reachable from this machine.
# # Bot tries to connect to this host and port to relay messages that it receives from
# # the distributors through Matrix Server
Expand All @@ -86,6 +89,9 @@ matrix:

### RPC server
rpc_server:
# Enable or disable RPC server. It must be enabled if bot is expecting to receive RPC requests (e.g. its distributor bot).
enabled: true

# Listen on this port for incoming RPC requests
port: 9090

Expand Down
22 changes: 14 additions & 8 deletions examples/config/camino-messenger-bot-supplier-camino.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ matrix:

### Partner Plugin
partner_plugin:
# Enable or disable the partner plugin rpc client. It must be enabled if bot's cm account supports at least one service.
enabled: true

# Partner Plugin hostname and port, should be reachable from this machine.
# Bot tries to connect to this host and port to relay messages that it receives from
# the distributors through Matrix Server
Expand All @@ -84,16 +87,19 @@ partner_plugin:



### RPC server
rpc_server:
# Listen on this port for incoming RPC requests
port: 9090
### RPC server (NOT USED FOR SUPPLIER BOT IN THIS VERSION)
# rpc_server:
# # Enable or disable RPC server. It must be enabled if bot is expecting to receive RPC requests (e.g. its distributor bot).
# enabled: false

# TLS configuration
unencrypted: true
# # Listen on this port for incoming RPC requests
# port: 9090

# # TLS configuration
# unencrypted: true

cert_file: server-cert.pem
key_file: server-key.pem
# cert_file: server-cert.pem
# key_file: server-key.pem



Expand Down
22 changes: 14 additions & 8 deletions examples/config/camino-messenger-bot-supplier-columbus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ matrix:

### Partner Plugin
partner_plugin:
# Enable or disable the partner plugin rpc client. It must be enabled if bot's cm account supports at least one service.
enabled: true

# Partner Plugin hostname and port, should be reachable from this machine.
# Bot tries to connect to this host and port to relay messages that it receives from
# the distributors through Matrix Server
Expand All @@ -84,16 +87,19 @@ partner_plugin:



### RPC server
rpc_server:
# Listen on this port for incoming RPC requests
port: 9090
### RPC server (NOT USED FOR SUPPLIER BOT IN THIS VERSION)
# rpc_server:
# # Enable or disable RPC server. It must be enabled if bot is expecting to receive RPC requests (e.g. its distributor bot).
# enabled: false

# TLS configuration
unencrypted: true
# # Listen on this port for incoming RPC requests
# port: 9090

cert_file: server-cert.pem
key_file: server-key.pem
# # TLS configuration
# unencrypted: true

# cert_file: server-cert.pem
# key_file: server-key.pem



Expand Down
49 changes: 24 additions & 25 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ func NewApp(ctx context.Context, cfg *config.Config, logger *zap.SugaredLogger)
}

// partner-plugin rpc client
var rpcClient *client.RPCClient
if cfg.PartnerPlugin.HostURL.String() != "" {
rpcClient, err = client.NewClient(cfg.PartnerPlugin, logger)
if err != nil {
logger.Errorf("Failed to create rpc client: %v", err)
return nil, err
}
rpcClient, err := client.NewClient(cfg.PartnerPlugin, logger)
if err != nil {
logger.Errorf("Failed to create rpc client: %v", err)
return nil, err
}

// register supported services, check if they actually supported by bot
serviceRegistry, hasSupportedServices, err := messaging.NewServiceRegistry(
serviceRegistry, err := messaging.NewServiceRegistry(
cfg.CMAccountAddress,
evmClient,
logger,
Expand All @@ -84,10 +81,6 @@ func NewApp(ctx context.Context, cfg *config.Config, logger *zap.SugaredLogger)
return nil, err
}

if !hasSupportedServices && rpcClient != nil {
logger.Warn("Bot doesn't support any services, but has partner plugin rpc client configured")
}

// messaging components
responseHandler, err := messaging.NewResponseHandler(
cfg.BotKey,
Expand Down Expand Up @@ -207,10 +200,12 @@ func (a *App) Run(ctx context.Context) error {

// run

g.Go(func() error {
a.logger.Info("Starting gRPC server...")
return a.rpcServer.Start()
})
if a.rpcServer != nil { // rpcServer will be nil, if its disabled in config
g.Go(func() error {
a.logger.Info("Starting gRPC server...")
return a.rpcServer.Start()
})
}

g.Go(func() error {
a.logger.Info("Starting start-up cash-in status check...")
Expand Down Expand Up @@ -252,16 +247,20 @@ func (a *App) Run(ctx context.Context) error {
// stop
// <-gCtx.Done() means that all "run" goroutines are finished

g.Go(func() error {
<-ctx.Done()
return a.rpcClient.Shutdown()
})
if a.rpcClient != nil { // rpcClient will be nil, if its disabled in partner plugin config section
g.Go(func() error {
<-ctx.Done()
return a.rpcClient.Shutdown()
})
}

g.Go(func() error {
<-ctx.Done()
a.rpcServer.Stop()
return nil
})
if a.rpcServer != nil { // rpcServer will be nil, if its disabled in config
g.Go(func() error {
<-ctx.Done()
a.rpcServer.Stop()
return nil
})
}

g.Go(func() error {
<-ctx.Done()
Expand Down
Loading