Skip to content

Commit

Permalink
Refactor DB config
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaranov committed Sep 16, 2022
1 parent f0df0b9 commit 9fb791e
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 104 deletions.
12 changes: 3 additions & 9 deletions apps/block_scout_web/config/runtime/test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Config

alias EthereumJSONRPC.Variant

config :explorer, Explorer.ExchangeRates, enabled: false, store: :none

config :explorer, Explorer.KnownTokens, enabled: false, store: :none
Expand All @@ -13,15 +15,7 @@ config :ueberauth, Ueberauth,
logout_url: "example.com/logout",
logout_return_to_url: "example.com/return"

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
variant = Variant.get()

Code.require_file("#{variant}.exs", "#{__DIR__}/../../../explorer/config/test")
Code.require_file("#{variant}.exs", "#{__DIR__}/../../../indexer/config/test")
12 changes: 3 additions & 9 deletions apps/ethereum_jsonrpc/config/runtime/test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import Config

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
alias EthereumJSONRPC.Variant

variant = Variant.get()

Code.require_file("#{variant}.exs", "#{__DIR__}/../../../explorer/config/test")
Code.require_file("#{variant}.exs", "#{__DIR__}/../../../indexer/config/test")
11 changes: 11 additions & 0 deletions apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/variant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,15 @@ defmodule EthereumJSONRPC.Variant do
],
EthereumJSONRPC.json_rpc_named_arguments()
) :: {:ok, [raw_trace_params]} | {:error, reason :: term} | :ignore

def get do
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"nethermind"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
end
end
12 changes: 3 additions & 9 deletions apps/explorer/config/runtime/test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Config

alias EthereumJSONRPC.Variant

config :explorer, Explorer.ExchangeRates, enabled: false, store: :ets, fetch_btc_value: true

config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: false
Expand All @@ -24,15 +26,7 @@ config :explorer, Explorer.Tracer, disabled?: false
config :explorer,
realtime_events_sender: Explorer.Chain.Events.SimpleSender

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
variant = Variant.get()

Code.require_file("#{variant}.exs", "#{__DIR__}/../../../explorer/config/test")
Code.require_file("#{variant}.exs", "#{__DIR__}/../../../indexer/config/test")
13 changes: 13 additions & 0 deletions apps/explorer/lib/explorer/repo/config_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ defmodule Explorer.Repo.ConfigHelper do
|> Keyword.merge(extract_parameters(url))
end

def get_db_pool_size(default_pool_size), do: String.to_integer(System.get_env("POOL_SIZE", default_pool_size))

def get_account_db_url, do: System.get_env("ACCOUNT_DATABASE_URL") || System.get_env("DATABASE_URL")

def get_account_db_pool_size(default_pool_size),
do: String.to_integer(System.get_env("ACCOUNT_POOL_SIZE", default_pool_size))

def get_api_db_url, do: System.get_env("DATABASE_READ_ONLY_API_URL") || System.get_env("DATABASE_URL")

def get_api_db_pool_size(default_pool_size), do: String.to_integer(System.get_env("POOL_SIZE_API", default_pool_size))

def ssl_enabled?, do: String.equivalent?(System.get_env("ECTO_USE_SSL") || "true", "true")

defp extract_parameters(empty) when empty == nil or empty == "", do: []

# sobelow_skip ["DOS.StringToAtom"]
Expand Down
12 changes: 3 additions & 9 deletions apps/indexer/config/runtime/test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import Config

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
alias EthereumJSONRPC.Variant

variant = Variant.get()

Code.require_file("#{variant}.exs", "#{__DIR__}/../../../explorer/config/test")
Code.require_file("#{variant}.exs", "#{__DIR__}/../../../indexer/config/test")
38 changes: 11 additions & 27 deletions config/runtime/dev.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Config

alias EthereumJSONRPC.Variant
alias Explorer.Repo.ConfigHelper

######################
### BlockScout Web ###
######################
Expand Down Expand Up @@ -41,15 +44,10 @@ config :block_scout_web, BlockScoutWeb.Endpoint,
database = if System.get_env("DATABASE_URL"), do: nil, else: "explorer_dev"
hostname = if System.get_env("DATABASE_URL"), do: nil, else: "localhost"

database_api_url =
if System.get_env("DATABASE_READ_ONLY_API_URL"),
do: System.get_env("DATABASE_READ_ONLY_API_URL"),
else: System.get_env("DATABASE_URL")

pool_size =
if System.get_env("DATABASE_READ_ONLY_API_URL"),
do: String.to_integer(System.get_env("POOL_SIZE", "30")),
else: String.to_integer(System.get_env("POOL_SIZE", "40"))
do: ConfigHelper.get_db_pool_size("30"),
else: ConfigHelper.get_db_pool_size("40")

# Configure your database
config :explorer, Explorer.Repo,
Expand All @@ -61,18 +59,12 @@ config :explorer, Explorer.Repo,
database_api = if System.get_env("DATABASE_READ_ONLY_API_URL"), do: nil, else: database
hostname_api = if System.get_env("DATABASE_READ_ONLY_API_URL"), do: nil, else: hostname

pool_size_api = String.to_integer(System.get_env("POOL_SIZE_API", "10"))

# Configure API database
config :explorer, Explorer.Repo.Replica1,
database: database_api,
hostname: hostname_api,
url: database_api_url,
pool_size: pool_size_api

database_account_url = System.get_env("ACCOUNT_DATABASE_URL") || System.get_env("DATABASE_URL")

pool_size_account = String.to_integer(System.get_env("ACCOUNT_POOL_SIZE", "10"))
url: ConfigHelper.get_api_db_url(),
pool_size: ConfigHelper.get_api_db_pool_size("10")

database_account = if System.get_env("ACCOUNT_DATABASE_URL"), do: nil, else: database
hostname_account = if System.get_env("ACCOUNT_DATABASE_URL"), do: nil, else: hostname
Expand All @@ -81,18 +73,10 @@ hostname_account = if System.get_env("ACCOUNT_DATABASE_URL"), do: nil, else: hos
config :explorer, Explorer.Repo.Account,
database: database_account,
hostname: hostname_account,
url: database_account_url,
pool_size: pool_size_account

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"ganache"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
url: ConfigHelper.get_account_db_url(),
pool_size: ConfigHelper.get_account_db_pool_size("10")

variant = Variant.get()

Code.require_file("#{variant}.exs", "apps/explorer/config/dev")

Expand Down
45 changes: 14 additions & 31 deletions config/runtime/prod.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Config

alias EthereumJSONRPC.Variant
alias Explorer.Repo.ConfigHelper

######################
### BlockScout Web ###
######################
Expand All @@ -24,53 +27,33 @@ config :block_scout_web, BlockScoutWeb.Endpoint,

pool_size =
if System.get_env("DATABASE_READ_ONLY_API_URL"),
do: String.to_integer(System.get_env("POOL_SIZE", "50")),
else: String.to_integer(System.get_env("POOL_SIZE", "40"))
do: ConfigHelper.get_db_pool_size("50"),
else: ConfigHelper.get_db_pool_size("40")

# Configures the database
config :explorer, Explorer.Repo,
url: System.get_env("DATABASE_URL"),
pool_size: pool_size,
ssl: String.equivalent?(System.get_env("ECTO_USE_SSL") || "true", "true")

database_api_url =
if System.get_env("DATABASE_READ_ONLY_API_URL"),
do: System.get_env("DATABASE_READ_ONLY_API_URL"),
else: System.get_env("DATABASE_URL")
ssl: ConfigHelper.ssl_enabled?()

pool_size_api =
if System.get_env("DATABASE_READ_ONLY_API_URL"),
do: String.to_integer(System.get_env("POOL_SIZE_API", "50")),
else: String.to_integer(System.get_env("POOL_SIZE_API", "10"))
do: ConfigHelper.get_api_db_pool_size("50"),
else: ConfigHelper.get_api_db_pool_size("10")

# Configures API the database
config :explorer, Explorer.Repo.Replica1,
url: database_api_url,
url: ConfigHelper.get_api_db_url(),
pool_size: pool_size_api,
ssl: String.equivalent?(System.get_env("ECTO_USE_SSL") || "true", "true")

database_account_url =
if System.get_env("ACCOUNT_DATABASE_URL"),
do: System.get_env("ACCOUNT_DATABASE_URL"),
else: System.get_env("DATABASE_URL")

pool_size_account = String.to_integer(System.get_env("ACCOUNT_POOL_SIZE", "50"))
ssl: ConfigHelper.ssl_enabled?()

# Configures Account database
config :explorer, Explorer.Repo.Account,
url: database_account_url,
pool_size: pool_size_account,
ssl: String.equivalent?(System.get_env("ECTO_USE_SSL") || "true", "true")
url: ConfigHelper.get_account_db_url(),
pool_size: ConfigHelper.get_account_db_pool_size("50"),
ssl: ConfigHelper.ssl_enabled?()

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
variant = Variant.get()

Code.require_file("#{variant}.exs", "apps/explorer/config/prod")

Expand Down
12 changes: 3 additions & 9 deletions config/runtime/test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Config

alias EthereumJSONRPC.Variant

######################
### BlockScout Web ###
######################
Expand All @@ -12,15 +14,7 @@ import Config
### Explorer ###
################

variant =
if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do
"parity"
else
System.get_env("ETHEREUM_JSONRPC_VARIANT")
|> String.split(".")
|> List.last()
|> String.downcase()
end
variant = Variant.get()

Code.require_file("#{variant}.exs", "apps/explorer/config/test")

Expand Down
2 changes: 1 addition & 1 deletion docker-compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Also, the repo contains built-in configs for different clients without need to b

- Erigon: `docker-compose -f docker-compose-no-build-erigon.yml up -d`
- Geth: `docker-compose -f docker-compose-no-build-geth.yml up -d`
- Nethermind, OpenEthereum: `docker-compose -f docker-compose-no-build-open-ethereum-nethermind up -d`
- Nethermind, OpenEthereum: `docker-compose -f docker-compose-no-build-nethermind up -d`
- Ganache: `docker-compose -f docker-compose-no-build-ganache.yml up -d`
- HardHat network: `docker-compose -f docker-compose-no-build-hardhat-network.yml up -d`
- Running only explorer without DB: `docker-compose -f docker-compose-no-build-no-db-container.yml up -d`. In this case, one container is created - for the explorer itself. And it assumes that the DB credentials are provided through `DATABASE_URL` environment variable.
Expand Down

0 comments on commit 9fb791e

Please sign in to comment.