Skip to content

Commit

Permalink
Improve setup functions by removing common code into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pharr117 committed Oct 12, 2023
1 parent 8cf49d3 commit 6babe25
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 87 deletions.
20 changes: 2 additions & 18 deletions cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 2 additions & 20 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"log"
"strings"
"time"

"github.com/DefiantLabs/cosmos-indexer/config"
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
23 changes: 2 additions & 21 deletions cmd/update_denoms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
22 changes: 2 additions & 20 deletions cmd/update_epochs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"strings"
"time"

"github.com/DefiantLabs/cosmos-indexer/config"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type ClientConfig struct {
ConfigFileLocation string
Database database
Database Database
Client client
Log log
}
Expand Down
6 changes: 3 additions & 3 deletions config/common_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type log struct {
Pretty bool
}

type database struct {
type Database struct {
Host string
Port string
Database string
Expand Down Expand Up @@ -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")
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion config/index_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type IndexConfig struct {
Database database
Database Database
ConfigFileLocation string
Base indexBase
Log log
Expand Down
2 changes: 1 addition & 1 deletion config/query_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type QueryConfig struct {
Database database
Database Database
Log log
Base queryBase
}
Expand Down
2 changes: 1 addition & 1 deletion config/update_denoms_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import "github.com/spf13/cobra"

type UpdateDenomsConfig struct {
Database database
Database Database
Lens lens
Log log
Base updateDenomsBase
Expand Down
2 changes: 1 addition & 1 deletion config/update_epochs_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type UpdateEpochsConfig struct {
Database database
Database Database
Lens lens
Base updateEpochsBase
Log log
Expand Down

0 comments on commit 6babe25

Please sign in to comment.