Skip to content

Commit

Permalink
Break down ENABLE apis (#276)
Browse files Browse the repository at this point in the history
This allows the caller to select which of the 3 services (replication,
sync, payer) to run.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **New Features**
- Introduced new environment variables to enhance configuration options:
`XMTPD_PAYER_ENABLE`, `XMTPD_REPLICATION_ENABLE`, and
`XMTPD_SYNC_ENABLE`.
- Added new configuration options for replication and synchronization,
improving control over server functionalities.

- **Bug Fixes**
- Improved error handling and readability in command-line option
parsing.

- **Documentation**
  - Enhanced logging for better observability during server startup.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
mkysel authored Nov 5, 2024
1 parent fae3c1b commit 210951f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 41 deletions.
14 changes: 8 additions & 6 deletions cmd/replication/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ var Commit string = "unknown"
var options config.ServerOptions

func main() {
if _, err := flags.Parse(&options); err != nil {
if err, ok := err.(*flags.Error); !ok || err.Type != flags.ErrHelp {
fatal("Could not parse options: %s", err)
}
return
}
_, err := flags.Parse(&options)

if options.Version {
fmt.Printf("Version: %s\n", Commit)
return
}

if err != nil {
if err, ok := err.(*flags.Error); !ok || err.Type != flags.ErrHelp {
fatal("Could not parse options: %s", err)
}
return
}

logger, _, err := utils.BuildLogger(options.Log)
if err != nil {
fatal("Could not build logger: %s", err)
Expand Down
4 changes: 4 additions & 0 deletions dev/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ set -eu

. dev/local.env

export XMTPD_PAYER_ENABLE=true
export XMTPD_REPLICATION_ENABLE=true
export XMTPD_SYNC_ENABLE=true

go run -ldflags="-X main.Commit=$(git rev-parse HEAD)" cmd/replication/main.go "$@"
4 changes: 4 additions & 0 deletions dev/run-2
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ export XMTPD_SIGNER_PRIVATE_KEY=$ANVIL_ACC_2_PRIVATE_KEY
export XMTPD_PAYER_PRIVATE_KEY=$XMTPD_SIGNER_PRIVATE_KEY
export XMTPD_DB_WRITER_CONNECTION_STRING="postgres://postgres:xmtp@localhost:8766/postgres?sslmode=disable"

export XMTPD_PAYER_ENABLE=true
export XMTPD_REPLICATION_ENABLE=true
export XMTPD_SYNC_ENABLE=true

go run -ldflags="-X main.Commit=$(git rev-parse HEAD)" cmd/replication/main.go -p 5051 "$@"
20 changes: 15 additions & 5 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ type MetricsOptions struct {

type PayerOptions struct {
PrivateKey string `long:"private-key" env:"XMTPD_PAYER_PRIVATE_KEY" description:"Private key used to sign blockchain transactions" required:"true"`
EnableAPI bool `long:"enable-api" env:"XMTPD_PAYER_ENABLE_API" description:"Enable the payer API"`
Enable bool `long:"enable" env:"XMTPD_PAYER_ENABLE" description:"Enable the payer API"`
}

type ReplicationOptions struct {
Enable bool `long:"enable" env:"XMTPD_REPLICATION_ENABLE" description:"Enable the replication API"`
}

type SyncOptions struct {
Enable bool `long:"enable" env:"XMTPD_SYNC_ENABLE" description:"Enable the sync server"`
}

type MlsValidationOptions struct {
Expand Down Expand Up @@ -90,14 +98,16 @@ type RegisterNodeOptions struct {

type ServerOptions struct {
API ApiOptions `group:"API Options" namespace:"api"`
DB DbOptions `group:"Database Options" namespace:"db"`
Contracts ContractsOptions `group:"Contracts Options" namespace:"contracts"`
DB DbOptions `group:"Database Options" namespace:"db"`
Log LogOptions `group:"Log Options" namespace:"log"`
Metrics MetricsOptions `group:"Metrics Options" namespace:"metrics"`
MlsValidation MlsValidationOptions `group:"MLS Validation Options" namespace:"mls-validation"`
Payer PayerOptions `group:"Payer Options" namespace:"payer"`
Reflection ReflectionOptions `group:"Reflection Options" namespace:"reflection"`
Tracing TracingOptions `group:"DD APM Tracing Options" namespace:"tracing"`
MlsValidation MlsValidationOptions `group:"MLS Validation Options" namespace:"mls-validation"`
Log LogOptions `group:"Log Options" namespace:"log"`
Replication ReplicationOptions `group:"Replication Options" namespace:"replication"`
Signer SignerOptions `group:"Signer Options" namespace:"signer"`
Sync SyncOptions `group:"Sync Options" namespace:"sync"`
Tracing TracingOptions `group:"DD APM Tracing Options" namespace:"tracing"`
Version bool ` short:"v" long:"version" description:"Output binary version and exit"`
}
73 changes: 43 additions & 30 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,23 @@ func NewReplicationServer(
}

serviceRegistrationFunc := func(grpcServer *grpc.Server) error {
replicationService, err := message.NewReplicationApiService(
ctx,
log,
s.registrant,
writerDB,
)
if err != nil {
return err

if options.Replication.Enable {
replicationService, err := message.NewReplicationApiService(
ctx,
log,
s.registrant,
writerDB,
)
if err != nil {
return err
}
message_api.RegisterReplicationApiServer(grpcServer, replicationService)

log.Info("Replication service enabled")
}
message_api.RegisterReplicationApiServer(grpcServer, replicationService)

if options.Payer.EnableAPI {
if options.Payer.Enable {
payerPrivateKey, err := utils.ParseEcdsaPrivateKey(options.Payer.PrivateKey)
if err != nil {
return err
Expand All @@ -139,35 +144,43 @@ func NewReplicationServer(
return err
}
payer_api.RegisterPayerApiServer(grpcServer, payerService)

log.Info("Payer service enabled")
}

return nil
}

// TODO(rich): Add configuration to specify whether to run API/sync server
s.apiServer, err = api.NewAPIServer(
s.ctx,
log,
listenAddress,
options.Reflection.Enable,
serviceRegistrationFunc,
)
if err != nil {
return nil, err
if options.Payer.Enable || options.Replication.Enable {
s.apiServer, err = api.NewAPIServer(
s.ctx,
log,
listenAddress,
options.Reflection.Enable,
serviceRegistrationFunc,
)
if err != nil {
return nil, err
}

log.Info("API server started", zap.Int("port", options.API.Port))
}

s.syncServer, err = sync.NewSyncServer(
s.ctx,
log,
s.nodeRegistry,
s.registrant,
s.writerDB,
)
if err != nil {
return nil, err
if options.Sync.Enable {
s.syncServer, err = sync.NewSyncServer(
s.ctx,
log,
s.nodeRegistry,
s.registrant,
s.writerDB,
)
if err != nil {
return nil, err
}

log.Info("Sync server started")
}

log.Info("Replication server started", zap.Int("port", options.API.Port))
return s, nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func NewTestServer(
API: config.ApiOptions{
Port: port,
},
Sync: config.SyncOptions{
Enable: true,
},
Replication: config.ReplicationOptions{
Enable: true,
},
//TODO(mkysel): this is not fully mocked yet
//Payer: config.PayerOptions{
// Enable: true,
//},
}, registry, db, messagePublisher, fmt.Sprintf("localhost:%d", port))
require.NoError(t, err)

Expand Down

0 comments on commit 210951f

Please sign in to comment.