From 6babe25aeeb078e0d3fa9f9a5d19272b6cc8c482 Mon Sep 17 00:00:00 2001 From: pharr117 Date: Wed, 11 Oct 2023 22:33:47 -0400 Subject: [PATCH] Improve setup functions by removing common code into functions --- cmd/index.go | 20 ++------------------ cmd/query.go | 22 ++-------------------- cmd/root.go | 27 +++++++++++++++++++++++++++ cmd/update_denoms.go | 23 ++--------------------- cmd/update_epochs.go | 22 ++-------------------- config/client_config.go | 2 +- config/common_configs.go | 6 +++--- config/index_config.go | 2 +- config/query_config.go | 2 +- config/update_denoms_config.go | 2 +- config/update_epochs_config.go | 2 +- 11 files changed, 43 insertions(+), 87 deletions(-) diff --git a/cmd/index.go b/cmd/index.go index 8b64c97..edc9349 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -62,38 +62,22 @@ func setupIndex(cmd *cobra.Command, args []string) error { return err } - logLevel := indexer.cfg.Log.Level - logPath := indexer.cfg.Log.Path - prettyLogging := indexer.cfg.Log.Pretty - config.DoConfigureLogger(logPath, logLevel, prettyLogging) + setupLogger(indexer.cfg.Log.Level, indexer.cfg.Log.Path, indexer.cfg.Log.Pretty) // 0 is an invalid starting block, set it to 1 if indexer.cfg.Base.StartBlock == 0 { indexer.cfg.Base.StartBlock = 1 } - db, err := dbTypes.PostgresDbConnect(indexer.cfg.Database.Host, indexer.cfg.Database.Port, indexer.cfg.Database.Database, - indexer.cfg.Database.User, indexer.cfg.Database.Password, strings.ToLower(indexer.cfg.Database.LogLevel)) + db, err := connectToDBAndMigrate(indexer.cfg.Database) if err != nil { config.Log.Fatal("Could not establish connection to the database", err) } indexer.db = db - sqldb, _ := db.DB() - sqldb.SetMaxIdleConns(10) - sqldb.SetMaxOpenConns(100) - sqldb.SetConnMaxLifetime(time.Hour) - indexer.scheduler = gocron.NewScheduler(time.UTC) - // run database migrations at every runtime - err = dbTypes.MigrateModels(db) - if err != nil { - config.Log.Error("Error running DB migrations", err) - return err - } - // We should stop relying on the denom cache now that we are running this as a CLI tool only dbTypes.CacheDenoms(db) dbTypes.CacheIBCDenoms(db) diff --git a/cmd/query.go b/cmd/query.go index 8e2115e..32d819e 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "log" - "strings" "time" "github.com/DefiantLabs/cosmos-indexer/config" @@ -76,30 +75,13 @@ func setupQuery(cmd *cobra.Command, args []string) error { return err } - // Logger - logLevel := queryConfig.Log.Level - logPath := queryConfig.Log.Path - prettyLogging := queryConfig.Log.Pretty - config.DoConfigureLogger(logPath, logLevel, prettyLogging) + setupLogger(queryConfig.Log.Level, queryConfig.Log.Path, queryConfig.Log.Pretty) - db, err := dbTypes.PostgresDbConnect(queryConfig.Database.Host, queryConfig.Database.Port, queryConfig.Database.Database, - queryConfig.Database.User, queryConfig.Database.Password, strings.ToLower(queryConfig.Database.LogLevel)) + db, err := connectToDBAndMigrate(queryConfig.Database) if err != nil { config.Log.Fatal("Could not establish connection to the database", err) } - sqldb, _ := db.DB() - sqldb.SetMaxIdleConns(10) - sqldb.SetMaxOpenConns(100) - sqldb.SetConnMaxLifetime(time.Hour) - - // run database migrations at every runtime - err = dbTypes.MigrateModels(db) - if err != nil { - config.Log.Error("Error running DB migrations", err) - return err - } - queryDbConnection = db // We should stop relying on the denom cache now that we are running this as a CLI tool only diff --git a/cmd/root.go b/cmd/root.go index f791996..595df7f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,10 +5,14 @@ import ( "log" "os" "strings" + "time" + "github.com/DefiantLabs/cosmos-indexer/config" + "github.com/DefiantLabs/cosmos-indexer/db" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" + "gorm.io/gorm" ) var ( @@ -96,3 +100,26 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) { } }) } + +func setupLogger(logLevel string, logPath string, prettyLogging bool) { + config.DoConfigureLogger(logPath, logLevel, prettyLogging) +} + +func connectToDBAndMigrate(dbConfig config.Database) (*gorm.DB, error) { + database, err := db.PostgresDbConnect(dbConfig.Host, dbConfig.Port, dbConfig.Database, dbConfig.User, dbConfig.Password, strings.ToLower(dbConfig.LogLevel)) + if err != nil { + config.Log.Fatal("Could not establish connection to the database", err) + } + + sqldb, _ := database.DB() + sqldb.SetMaxIdleConns(10) + sqldb.SetMaxOpenConns(100) + sqldb.SetConnMaxLifetime(time.Hour) + + err = db.MigrateModels(database) + if err != nil { + config.Log.Error("Error running DB migrations", err) + } + + return database, nil +} diff --git a/cmd/update_denoms.go b/cmd/update_denoms.go index e705aaf..41897bb 100644 --- a/cmd/update_denoms.go +++ b/cmd/update_denoms.go @@ -2,11 +2,8 @@ package cmd import ( "os" - "strings" - "time" "github.com/DefiantLabs/cosmos-indexer/config" - dbTypes "github.com/DefiantLabs/cosmos-indexer/db" "github.com/DefiantLabs/cosmos-indexer/tasks" "github.com/spf13/cobra" "gorm.io/gorm" @@ -45,29 +42,13 @@ func setupUpdateDenoms(cmd *cobra.Command, args []string) error { return err } - // Logger - logLevel := updateDenomsConfig.Log.Level - logPath := updateDenomsConfig.Log.Path - prettyLogging := updateDenomsConfig.Log.Pretty - config.DoConfigureLogger(logPath, logLevel, prettyLogging) + setupLogger(updateDenomsConfig.Log.Level, updateDenomsConfig.Log.Path, updateDenomsConfig.Log.Pretty) - db, err := dbTypes.PostgresDbConnect(updateDenomsConfig.Database.Host, updateDenomsConfig.Database.Port, updateDenomsConfig.Database.Database, - updateDenomsConfig.Database.User, updateDenomsConfig.Database.Password, strings.ToLower(updateDenomsConfig.Database.LogLevel)) + db, err := connectToDBAndMigrate(updateDenomsConfig.Database) if err != nil { config.Log.Fatal("Could not establish connection to the database", err) } - sqldb, _ := db.DB() - sqldb.SetMaxIdleConns(10) - sqldb.SetMaxOpenConns(100) - sqldb.SetConnMaxLifetime(time.Hour) - - err = dbTypes.MigrateModels(db) - if err != nil { - config.Log.Error("Error running DB migrations", err) - return err - } - updateDenomsDbConnection = db return nil diff --git a/cmd/update_epochs.go b/cmd/update_epochs.go index 7f4532b..1a47ad5 100644 --- a/cmd/update_epochs.go +++ b/cmd/update_epochs.go @@ -1,7 +1,6 @@ package cmd import ( - "strings" "time" "github.com/DefiantLabs/cosmos-indexer/config" @@ -45,30 +44,13 @@ func setupUpdateEpochs(cmd *cobra.Command, args []string) error { return err } - // Logger - logLevel := updateEpochsConfig.Log.Level - logPath := updateEpochsConfig.Log.Path - prettyLogging := updateEpochsConfig.Log.Pretty - config.DoConfigureLogger(logPath, logLevel, prettyLogging) + setupLogger(updateEpochsConfig.Log.Level, updateEpochsConfig.Log.Path, updateEpochsConfig.Log.Pretty) - db, err := dbTypes.PostgresDbConnect(updateEpochsConfig.Database.Host, updateEpochsConfig.Database.Port, updateEpochsConfig.Database.Database, - updateEpochsConfig.Database.User, updateEpochsConfig.Database.Password, strings.ToLower(updateEpochsConfig.Database.LogLevel)) + db, err := connectToDBAndMigrate(updateEpochsConfig.Database) if err != nil { config.Log.Fatal("Could not establish connection to the database", err) } - sqldb, _ := db.DB() - sqldb.SetMaxIdleConns(10) - sqldb.SetMaxOpenConns(100) - sqldb.SetConnMaxLifetime(time.Hour) - - // run database migrations at every runtime - err = dbTypes.MigrateModels(db) - if err != nil { - config.Log.Error("Error running DB migrations", err) - return err - } - updateEpochsDbConnection = db return nil diff --git a/config/client_config.go b/config/client_config.go index 7bfaedf..c8b703f 100644 --- a/config/client_config.go +++ b/config/client_config.go @@ -13,7 +13,7 @@ import ( type ClientConfig struct { ConfigFileLocation string - Database database + Database Database Client client Log log } diff --git a/config/common_configs.go b/config/common_configs.go index 3deacd9..020f690 100644 --- a/config/common_configs.go +++ b/config/common_configs.go @@ -16,7 +16,7 @@ type log struct { Pretty bool } -type database struct { +type Database struct { Host string Port string Database string @@ -47,7 +47,7 @@ func SetupLogFlags(logConf *log, cmd *cobra.Command) { cmd.PersistentFlags().StringVar(&logConf.Path, "log.path", "", "log path (default is $HOME/.cosmos-indexer/logs.txt") } -func SetupDatabaseFlags(databaseConf *database, cmd *cobra.Command) { +func SetupDatabaseFlags(databaseConf *Database, cmd *cobra.Command) { cmd.PersistentFlags().StringVar(&databaseConf.Host, "database.host", "", "database host") cmd.PersistentFlags().StringVar(&databaseConf.Port, "database.port", "5432", "database port") cmd.PersistentFlags().StringVar(&databaseConf.Database, "database.database", "", "database name") @@ -67,7 +67,7 @@ func SetupThrottlingFlag(throttlingValue *float64, cmd *cobra.Command) { cmd.PersistentFlags().Float64Var(throttlingValue, "base.throttling", 0.5, "throttle delay") } -func validateDatabaseConf(dbConf database) error { +func validateDatabaseConf(dbConf Database) error { if util.StrNotSet(dbConf.Host) { return errors.New("database host must be set") } diff --git a/config/index_config.go b/config/index_config.go index 5884ed4..6606dca 100644 --- a/config/index_config.go +++ b/config/index_config.go @@ -9,7 +9,7 @@ import ( ) type IndexConfig struct { - Database database + Database Database ConfigFileLocation string Base indexBase Log log diff --git a/config/query_config.go b/config/query_config.go index bf10dc1..90ac02c 100644 --- a/config/query_config.go +++ b/config/query_config.go @@ -9,7 +9,7 @@ import ( ) type QueryConfig struct { - Database database + Database Database Log log Base queryBase } diff --git a/config/update_denoms_config.go b/config/update_denoms_config.go index 269b0b4..84aebd0 100644 --- a/config/update_denoms_config.go +++ b/config/update_denoms_config.go @@ -3,7 +3,7 @@ package config import "github.com/spf13/cobra" type UpdateDenomsConfig struct { - Database database + Database Database Lens lens Log log Base updateDenomsBase diff --git a/config/update_epochs_config.go b/config/update_epochs_config.go index 39abff7..64cf93e 100644 --- a/config/update_epochs_config.go +++ b/config/update_epochs_config.go @@ -7,7 +7,7 @@ import ( ) type UpdateEpochsConfig struct { - Database database + Database Database Lens lens Base updateEpochsBase Log log