Skip to content

Commit

Permalink
fix(services): allow passing db name and password to MODE (#1107)
Browse files Browse the repository at this point in the history
* allow passing db name and password to mode

* remove comment
  • Loading branch information
alvrs committed Jul 5, 2023
1 parent d1c278c commit 3fb16d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/services/cmd/mode/main.go
Expand Up @@ -30,10 +30,12 @@ var (
chainRpcsHttp = flag.String("chain-rpcs-http", "", "comma separated list of chain rpcs (http)")
chainRpcsWs = flag.String("chain-rpcs-ws", "", "comma separated list of chain rpcs (ws)")
// Database flags.
dbName = flag.String("db-name", "", "database name")
dbHost = flag.String("db-host", "", "database host")
dbPort = flag.Uint64("db-port", 5433, "database port")
dbWipe = flag.Bool("db-wipe", false, "database wipe on launch")
dbName = flag.String("db-name", "", "database name")
dbHost = flag.String("db-host", "", "database host")
dbPort = flag.Uint64("db-port", 5433, "database port")
dbUser = flag.String("db-user", "", "database user")
dbPassword = flag.String("db-password", "", "database password")
dbWipe = flag.Bool("db-wipe", false, "database wipe on launch")
// Sync flags.
syncEnabled = flag.Bool("sync-enabled", false, "enable syncing")
syncStartBlock = flag.Uint64("sync-start-block", 0, "start block for syncing")
Expand Down Expand Up @@ -73,6 +75,8 @@ func main() {
*chainRpcsWs,
*dbName,
*dbHost,
*dbUser,
*dbPassword,
*dbPort,
*dbWipe,
*syncEnabled,
Expand Down
12 changes: 8 additions & 4 deletions packages/services/pkg/mode/config/config.go
Expand Up @@ -28,6 +28,8 @@ func FromFile(configFile string) (*Config, error) {
// FromFlags parses the configuration from command-line flags.
func FromFlags(
chainNames, chainIds, chainRPCsHTTP, chainRPCsWS, dbName, dbHost string,
dbUser string,
dbPassword string,
dbPort uint64,
dbWipe bool,
syncEnabled bool,
Expand All @@ -37,10 +39,12 @@ func FromFlags(
config := &Config{
Chains: make([]ChainConfig, 0),
DB: DBConfig{
Name: dbName,
Host: dbHost,
Port: dbPort,
Wipe: dbWipe,
Name: dbName,
Host: dbHost,
User: dbUser,
Password: dbPassword,
Port: dbPort,
Wipe: dbWipe,
},
Sync: SyncConfig{
Enabled: syncEnabled,
Expand Down
10 changes: 6 additions & 4 deletions packages/services/pkg/mode/config/types.go
Expand Up @@ -15,10 +15,12 @@ type ChainConfig struct {

// DBConfig defines the configuration for a database.
type DBConfig struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port uint64 `yaml:"port"`
Wipe bool `yaml:"wipe"`
Name string `yaml:"name"`
Host string `yaml:"host"`
User string `yaml:"user"`
Password string `yaml:"password"`
Port uint64 `yaml:"port"`
Wipe bool `yaml:"wipe"`
}

// SyncConfig defines the configuration for the synchronization process.
Expand Down
7 changes: 6 additions & 1 deletion packages/services/pkg/mode/db/core.go
Expand Up @@ -32,7 +32,12 @@ func createGormDsn(config *config.DBConfig) string {

// Creates a base DSN for use with SQLX and GORM.
func createBaseDsn(config *config.DBConfig) string {
return fmt.Sprintf("host=%s port=%d dbname=%s sslmode=disable", config.Host, config.Port, config.Name)
baseDsn := fmt.Sprintf("host=%s port=%d dbname=%s sslmode=disable", config.Host, config.Port, config.Name)
if config.User != "" && config.Password != "" {
baseDsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
config.Host, config.Port, config.User, config.Password, config.Name)
}
return baseDsn
}

// connectViaSQLX creates a connection to a PostgreSQL database using the specified DSN via SQLX.
Expand Down

0 comments on commit 3fb16d7

Please sign in to comment.