Skip to content

Commit

Permalink
Merge pull request crypto-com#662 from crypto-com/refactor/configuation
Browse files Browse the repository at this point in the history
Problem: Missing generic configuration
  • Loading branch information
davcrypto authored Jan 18, 2022
2 parents 2047b5e + 676f787 commit c42232b
Show file tree
Hide file tree
Showing 24 changed files with 1,031 additions and 596 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func initProjections(
customConfig *CustomConfig,
) (projections []projection_entity.Projection) {
// Skip if API_ONLY is on
if config.System.Mode == bootstrap.SYSTEM_MODE_API_ONLY {
if !config.IndexService.Enable {
return projections
}

Expand Down Expand Up @@ -458,7 +458,7 @@ func initCronJobs(
customConfig *CustomConfig,
) (crons []projection_entity.CronJob) {
// Skip if API_ONLY is on
if config.System.Mode == bootstrap.SYSTEM_MODE_API_ONLY {
if !config.IndexService.Enable {
return crons
}

Expand Down
3 changes: 0 additions & 3 deletions appinterface/eventhandler/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ func (handler *ProjectionHandler) HandleEvents(blockHeight int64, events []event
filteredEvents := make([]event.Event, 0)
for _, event := range events {
if !isListeningEvent(event, handler.projection.GetEventsToListen()) {
//eventLogger.WithFields(applogger.LogFields{
// "eventName": event.Name(),
//}).Debugf("skipping because event is not one of the listening events")
continue
}
filteredEvents = append(filteredEvents, event)
Expand Down
31 changes: 14 additions & 17 deletions bootstrap/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/crypto-com/chain-indexing/appinterface/rdb"
config "github.com/crypto-com/chain-indexing/bootstrap/config"
projection_entity "github.com/crypto-com/chain-indexing/entity/projection"
applogger "github.com/crypto-com/chain-indexing/external/logger"
"github.com/crypto-com/chain-indexing/infrastructure/metric/prometheus"
Expand All @@ -14,27 +15,27 @@ import (

type app struct {
logger applogger.Logger
config *Config
config *config.Config

rdbConn rdb.Conn
httpAPIServer *HTTPAPIServer
indexService *IndexService
}

func NewApp(logger applogger.Logger, config *Config) *app {
func NewApp(logger applogger.Logger, config *config.Config) *app {
rdbConn, err := SetupRDbConn(config, logger)
if err != nil {
logger.Panicf("error setting up RDb connection: %v", err)
}

if config.System.Mode != SYSTEM_MODE_API_ONLY {
if config.IndexService.Enable {
ref := ""
if config.GithubAPI.MigrationRepoRef != "" {
ref = "#" + config.GithubAPI.MigrationRepoRef
if config.IndexService.GithubAPI.MigrationRepoRef != "" {
ref = "#" + config.IndexService.GithubAPI.MigrationRepoRef
}

m, err := migrate.New(
fmt.Sprintf(MIGRATION_GITHUB_TARGET, config.GithubAPI.Username, config.GithubAPI.Token, ref),
fmt.Sprintf(MIGRATION_GITHUB_TARGET, config.IndexService.GithubAPI.Username, config.IndexService.GithubAPI.Token, ref),
migrationDBConnString(rdbConn),
)
if err != nil {
Expand Down Expand Up @@ -72,23 +73,19 @@ func (a *app) GetRDbConn() rdb.Conn {
}

func (a *app) InitHTTPAPIServer(registry RouteRegistry) {
a.httpAPIServer = NewHTTPAPIServer(a.logger, a.config)
a.httpAPIServer.RegisterRoutes(registry)
if a.config.HTTPService.Enable {
a.httpAPIServer = NewHTTPAPIServer(a.logger, a.config)
a.httpAPIServer.RegisterRoutes(registry)
}
}

func (a *app) InitIndexService(projections []projection_entity.Projection, cronJobs []projection_entity.CronJob) {
a.indexService = NewIndexService(a.logger, a.rdbConn, a.config, projections, cronJobs)
if a.config.IndexService.Enable {
a.indexService = NewIndexService(a.logger, a.rdbConn, a.config, projections, cronJobs)
}
}

func (a *app) Run() {
switch a.config.System.Mode {
case SYSTEM_MODE_EVENT_STORE,
SYSTEM_MODE_TENDERMINT_DIRECT,
SYSTEM_MODE_API_ONLY:
default:
a.logger.Panicf("unrecognized system mode: %s", a.config.System.Mode)
}

if a.httpAPIServer != nil {
go func() {
if runErr := a.httpAPIServer.Run(); runErr != nil {
Expand Down
177 changes: 0 additions & 177 deletions bootstrap/config.go

This file was deleted.

107 changes: 107 additions & 0 deletions bootstrap/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package config

const SYSTEM_MODE_EVENT_STORE = "EVENT_STORE"
const SYSTEM_MODE_TENDERMINT_DIRECT = "TENDERMINT_DIRECT"

type Config struct {
Blockchain Blockchain `yaml:"blockchain" toml:"blockchain" xml:"blockchain" json:"blockchain"`
IndexService IndexService `yaml:"index_service" toml:"index_service" xml:"index_service" json:"index_service"`
HTTPService HTTPService `yaml:"http_service" toml:"http_service" xml:"http_service" json:"http_service"`
TendermintApp TendermintApp `yaml:"tendermint_app" toml:"tendermint_app" xml:"tendermint_app" json:"tendermint_app"`
CosmosApp CosmosApp `yaml:"cosmos_app" toml:"cosmos_app" xml:"cosmos_app" json:"cosmos_app"`
Debug Debug `yaml:"debug" toml:"debug" xml:"debug" json:"debug"`
Postgres Postgres `yaml:"postgres" toml:"postgres" xml:"postgres" json:"postgres"`
Logger Logger `yaml:"logger" toml:"logger" xml:"logger" json:"logger"`
Prometheus Prometheus `yaml:"prometheus" toml:"prometheus" xml:"prometheus" json:"prometheus"`
}

type IndexService struct {
Enable bool `yaml:"enable" toml:"enable" xml:"enable" json:"enable,omitempty"`
Mode string `yaml:"mode" toml:"mode" xml:"mode" json:"mode,omitempty"`
WindowSize int `yaml:"window_size" toml:"window_size" xml:"window_size" json:"window_size,omitempty"`
Projection Projection `yaml:"projection" toml:"projection" xml:"projection" json:"projection"`
CronJob CronJob `yaml:"cron_job" toml:"cron_job" xml:"cron_job" json:"cron_job"`
CosmosVersionEnabledHeight CosmosVersionEnabledHeight `yaml:"cosmos_version_enabled_height" toml:"cosmos_version_enabled_height" xml:"cosmos_version_enabled_height" json:"cosmos_version_enabled_height"`
GithubAPI GithubAPI `yaml:"github_api" toml:"github_api" xml:"github_api" json:"github_api"`
}

type HTTPService struct {
Enable bool `yaml:"enable" toml:"enable" xml:"enable" json:"enable,omitempty"`
ListeningAddress string `yaml:"listening_address" toml:"listening_address" xml:"listening_address" json:"listening_address,omitempty"`
RoutePrefix string `yaml:"route_prefix" toml:"route_prefix" xml:"route_prefix" json:"route_prefix,omitempty"`
CorsAllowedOrigins []string `yaml:"cors_allowed_origins" toml:"cors_allowed_origins" xml:"cors_allowed_origins" json:"cors_allowed_origins,omitempty"`
CorsAllowedMethods []string `yaml:"cors_allowed_methods" toml:"cors_allowed_methods" xml:"cors_allowed_methods" json:"cors_allowed_methods,omitempty"`
CorsAllowedHeaders []string `yaml:"cors_allowed_headers" toml:"cors_allowed_headers" xml:"cors_allowed_headers" json:"cors_allowed_headers,omitempty"`
}

type Blockchain struct {
BondingDenom string `yaml:"bonding_denom" toml:"bonding_denom" xml:"bonding_denom" json:"bonding_denom,omitempty"`
AccountAddressPrefix string `yaml:"account_address_prefix" toml:"account_address_prefix" xml:"account_address_prefix" json:"account_address_prefix,omitempty"`
AccountPubKeyPrefix string `yaml:"account_pub_key_prefix" toml:"account_pub_key_prefix" xml:"account_pub_key_prefix" json:"account_pub_key_prefix,omitempty"`
ValidatorAddressPrefix string `yaml:"validator_address_prefix" toml:"validator_address_prefix" xml:"validator_address_prefix" json:"validator_address_prefix,omitempty"`
ValidatorPubKeyPrefix string `yaml:"validator_pub_key_prefix" toml:"validator_pub_key_prefix" xml:"validator_pub_key_prefix" json:"validator_pub_key_prefix,omitempty"`
ConNodeAddressPrefix string `yaml:"con_node_address_prefix" toml:"con_node_address_prefix" xml:"con_node_address_prefix" json:"con_node_address_prefix,omitempty"`
ConNodePubKeyPrefix string `yaml:"con_node_pub_key_prefix" toml:"con_node_pub_key_prefix" xml:"con_node_pub_key_prefix" json:"con_node_pub_key_prefix,omitempty"`
}

type Debug struct {
PprofEnable bool `yaml:"pprof_enable" toml:"pprof_enable" xml:"pprof_enable" json:"pprof_enable,omitempty"`
PprofListeningAddress string `yaml:"pprof_listening_address" toml:"pprof_listening_address" xml:"pprof_listening_address" json:"pprof_listening_address,omitempty"`
}

type TendermintApp struct {
HTTPRPCUrl string `yaml:"http_rpc_url" toml:"http_rpc_url" xml:"http_rpc_url" json:"http_rpc_url,omitempty"`
Insecure bool `yaml:"insecure" toml:"insecure" xml:"insecure" json:"insecure,omitempty"`
StrictGenesisParsing bool `yaml:"strict_genesis_parsing" toml:"strict_genesis_parsing" xml:"strict_genesis_parsing" json:"strict_genesis_parsing,omitempty"`
}

type CosmosApp struct {
HTTPRPCUrl string `yaml:"http_rpc_url" toml:"http_rpc_url" xml:"http_rpc_url" json:"http_rpc_url,omitempty"`
Insecure bool `yaml:"insecure" toml:"insecure" xml:"insecure" json:"insecure,omitempty"`
}

type Postgres struct {
SSL bool `yaml:"ssl" toml:"ssl" xml:"ssl" json:"ssl,omitempty"`
Host string `yaml:"host" toml:"host" xml:"host" json:"host,omitempty"`
Port int32 `yaml:"port" toml:"port" xml:"port" json:"port,omitempty"`
Username string `yaml:"username" toml:"username" xml:"username" json:"username,omitempty"`
Password string `yaml:"password" toml:"password" xml:"password" json:"password,omitempty"`
Name string `yaml:"name" toml:"name" xml:"name" json:"name,omitempty"`
Schema string `yaml:"schema" toml:"schema" xml:"schema" json:"schema,omitempty"`
PoolMaxConns int32 `yaml:"pool_max_conns" toml:"pool_max_conns" xml:"pool_max_conns" json:"pool_max_conns,omitempty"`
PoolMinConns int32 `yaml:"pool_min_conns" toml:"pool_min_conns" xml:"pool_min_conns" json:"pool_min_conns,omitempty"`
PoolMaxConnLifeTime string `yaml:"pool_max_conn_life_time" toml:"pool_max_conn_life_time" xml:"pool_max_conn_life_time" json:"pool_max_conn_life_time,omitempty"`
PoolMaxConnIdleTime string `yaml:"pool_max_conn_idle_time" toml:"pool_max_conn_idle_time" xml:"pool_max_conn_idle_time" json:"pool_max_conn_idle_time,omitempty"`
PoolHealthCheckInterval string `yaml:"pool_health_check_interval" toml:"pool_health_check_interval" xml:"pool_health_check_interval" json:"pool_health_check_interval,omitempty"`
}

type Logger struct {
Level string `yaml:"level" toml:"level" xml:"level" json:"level,omitempty"`
Color bool `yaml:"color" toml:"color" xml:"color" json:"color,omitempty"`
}

type Projection struct {
Enables []string `yaml:"enables" toml:"enables" xml:"enables" json:"enables,omitempty"`
ExtraConfigs map[string]interface{} `yaml:"extra_configs" toml:"extra_configs" xml:"extra_configs" json:"extra_configs,omitempty"`
}

type CronJob struct {
Enables []string `yaml:"enables" toml:"enables" xml:"enables" json:"enables,omitempty"`
ExtraConfigs map[string]interface{} `yaml:"extra_configs" toml:"extra_configs" xml:"extra_configs" json:"extra_configs,omitempty"`
}

type CosmosVersionEnabledHeight struct {
V0_42_7 uint64 `yaml:"v_0_42_7" toml:"v_0_42_7" xml:"v_0_42_7" json:"v_0_42_7,omitempty"`
}

type GithubAPI struct {
Username string `yaml:"username" toml:"username" xml:"username" json:"username,omitempty"`
Token string `yaml:"token" toml:"token" xml:"token" json:"token,omitempty"`
MigrationRepoRef string `yaml:"migration_repo_ref" toml:"migration_repo_ref" xml:"migration_repo_ref" json:"migration_repo_ref,omitempty"`
}

type Prometheus struct {
Enable bool `yaml:"enable" toml:"enable" xml:"enable" json:"enable,omitempty"`
ExportPath string `yaml:"export_path" toml:"export_path" xml:"export_path" json:"export_path,omitempty"`
Port string `yaml:"port" toml:"port" xml:"port" json:"port,omitempty"`
}
Loading

0 comments on commit c42232b

Please sign in to comment.