Skip to content

Commit 26c085b

Browse files
authored
Add ability to configure carp via env var (#139)
1 parent 633b51d commit 26c085b

File tree

11 files changed

+50
-65
lines changed

11 files changed

+50
-65
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deployment/config/indexer/cardano_node_docker.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ sink:
1414
type: cardano
1515
db:
1616
type: postgres
17-
host: postgres # name of postgres container
18-
port: 5432 # internal postgres port
19-
user: carp
20-
password: "1234"
21-
db: carp_mainnet
17+
database_url: postgresql://carp:1234@postgres:5432/carp_mainnet
2218
network: mainnet # preview / preprod / testnet
2319

2420
start_block:

deployment/config/indexer/oura_docker.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ sink:
77
type: cardano
88
db:
99
type: postgres
10-
host: postgres # name of postgres container
11-
port: 5432 # internal postgres port
12-
user: carp
13-
password: "1234"
14-
db: carp_mainnet
10+
database_url: postgresql://carp:1234@postgres:5432/carp_mainnet
1511
network: mainnet # preview / preprod / testnet
1612

1713
start_block:

docs/docs/indexer/docker.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NETWORK=mainnet
4343
4444
# can be cardano_node_docker.yml or oura_docker.yml or
4545
# any custom filename located in deployment/config/indexer folder
46+
# alternative to CARP_CONFIG env variable
4647
CONFIG_FILE=cardano_node_docker.yml
4748
4849
CARP_VERSION=2.2.1
@@ -65,7 +66,7 @@ DATABASE_URL=postgresql://${PGUSER}:${PGPASSWORD}@${POSTGRES_HOST}:${POSTGRES_PO
6566
PGURI=$DATABASE_URL
6667
```
6768

68-
Please set `NETWORK`, `CONFIG_FILE` and postgres variables carefully. `CONFIG_FILE` should be located in `deployment/config/indexer` folder.
69+
Please set `NETWORK`, `CONFIG_FILE` and postgres variables carefully. `CONFIG_FILE` should be located in `deployment/config/indexer` folder. You can use `CARP_CONFIG` env variable instead of `CONFIG_FILE` if you want to use just env variables for configuration.
6970

7071
You can also add non-mandatory variables that are useful for backuper:
7172

@@ -83,7 +84,7 @@ AWS_SECRET_ACCESS_KEY=<Secret for given account>
8384

8485
## Carp configuration
8586

86-
We mentioned config files above, they should be stored in `deployment/config/indexer` folder, and you should set `CONFIG_FILE` env variable with the config name.
87+
We mentioned config files above, they should be stored in `deployment/config/indexer` folder, and you should set `CONFIG_FILE` env variable with the config name. Alternatively you can set `CARP_CONFIG` env variable and not use the configuration file
8788

8889
The difference from non-docker configuration is in the networking mainly.
8990

docs/docs/indexer/run.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ To run carp you need to configure carp itself, set up / configure cardano-node (
88

99
## Configuration & concepts
1010

11-
Carp itself uses special file for configuration, the examples are stored [there](https://github.com/dcSpark/carp/blob/main/indexer/configs/). Let's dive into configuration a little further:
11+
Carp itself uses special file for configuration, the examples are stored [there](https://github.com/dcSpark/carp/blob/main/indexer/configs/). Besides, carp's config can be set up through `CARP_CONFIG` env variable in json format. Let's dive into configuration a little further:
12+
13+
File format:
1214
```yaml
1315
source:
1416
type: oura
@@ -19,16 +21,17 @@ sink:
1921
type: cardano
2022
db:
2123
type: postgres
22-
host: localhost
23-
port: 5432
24-
user: carp
25-
password: "1234"
26-
db: carp_mainnet
24+
database_url: postgresql://carp:1234@localhost:5432/carp_mainnet
2725
network: mainnet # preview / preprod / testnet
2826

2927
start_block:
3028
```
3129
30+
Json format:
31+
```json
32+
{"source":{"type":"oura","socket":"relays-new.cardano-mainnet.iohk.io:3001","bearer":"Tcp"},"sink":{"type":"cardano","db":{"type": "postgres","database_url":"postgresql://carp:1234@localhost:5432/carp_mainnet"},"network":"mainnet"},"start_block":null}
33+
```
34+
3235
As you might see there are several key sections: source and sink. For sink there's only one option at the moment: `cardano` sink. For source there are two options with different configurations: `oura` and `cardano_net`.
3336

3437
### Sink configuration

indexer/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ async-trait = { version = "0.1.64" }
2424
base64 = { version = "0.21.0" }
2525
cardano-multiplatform-lib = { git = "https://github.com/dcSpark/cardano-multiplatform-lib", branch = "metadata-and-addr" }
2626
clap = { version = "3.1", features = ["derive"] }
27-
ctrlc = {version = "3.2.4", features = ["termination"] }
27+
ctrlc = { version = "3.2.4", features = ["termination"] }
28+
dotenv = { version = "0.15.0" }
2829
hex = { version = "0.4.3" }
2930
oura = { version = "1.8.0" }
30-
pallas = {version = "0.17.0" }
31+
pallas = { version = "0.17.0" }
3132
serde = { version = "1.0.152", features = ["derive", "rc"] }
3233
serde_json = { version = "1.0.92" }
3334
serde_yaml = { version = "0.9.17" }

indexer/configs/cardano_node.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ sink:
1212
type: cardano
1313
db:
1414
type: postgres
15-
host: localhost
16-
port: 5432
17-
user: carp
18-
password: "1234"
19-
db: carp_mainnet
15+
database_url: postgresql://carp:1234@localhost:5432/carp_mainnet
2016
network: mainnet # preview / preprod / testnet
2117

2218
start_block:

indexer/configs/default.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ sink:
77
type: cardano
88
db:
99
type: postgres
10-
host: localhost
11-
port: 5432
12-
user: carp
13-
password: "1234"
14-
db: carp_mainnet
10+
database_url: postgresql://carp:1234@localhost:5432/carp_mainnet
1511
network: mainnet # preview / preprod / testnet
1612

1713
start_block:

indexer/configs/oura.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ sink:
77
type: cardano
88
db:
99
type: postgres
10-
host: localhost
11-
port: 5432
12-
user: carp
13-
password: "1234"
14-
db: carp_mainnet
10+
database_url: postgresql://carp:1234@localhost:5432/carp_mainnet
1511
network: mainnet # preview / preprod / testnet
1612

1713
start_block:

indexer/src/main.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,14 @@ pub struct Cli {
3333

3434
/// path to config file
3535
#[clap(long, value_parser)]
36-
config_path: PathBuf,
36+
config_path: Option<PathBuf>,
3737
}
3838

3939
#[derive(Debug, Clone, Deserialize)]
4040
#[serde(tag = "type", rename_all = "snake_case")]
4141
#[serde(deny_unknown_fields)]
4242
pub enum DbConfig {
43-
Postgres {
44-
host: String,
45-
port: u64,
46-
user: String,
47-
password: String,
48-
db: String,
49-
},
43+
Postgres { database_url: String },
5044
}
5145

5246
#[derive(Debug, Clone, Deserialize)]
@@ -114,19 +108,31 @@ async fn main() -> anyhow::Result<()> {
114108
tracing::info!("Execution plan {}", plan);
115109
let exec_plan = Arc::new(ExecutionPlan::load_from_file(&plan)?);
116110

117-
tracing::info!("Config file {:?}", config_path);
118-
let file = File::open(&config_path).with_context(|| {
119-
format!(
120-
"Cannot open config file {path}",
121-
path = config_path.display()
122-
)
123-
})?;
124-
let config: Config = serde_yaml::from_reader(file).with_context(|| {
125-
format!(
126-
"Cannot parse config file {path}",
127-
path = config_path.display()
128-
)
129-
})?;
111+
let config = if let Some(config_path) = config_path {
112+
tracing::info!("Config file {:?}", config_path);
113+
let file = File::open(&config_path).with_context(|| {
114+
format!(
115+
"Cannot open config file {path}",
116+
path = config_path.display()
117+
)
118+
})?;
119+
let config: Config = serde_yaml::from_reader(file).with_context(|| {
120+
format!(
121+
"Cannot parse config file {path}",
122+
path = config_path.display()
123+
)
124+
})?;
125+
config
126+
} else {
127+
dotenv::dotenv().ok();
128+
129+
let carp_config = std::env::var("CARP_CONFIG")
130+
.expect("env CARP_CONFIG not found and --config-path not specified");
131+
let config: Config = serde_json::from_str(&carp_config)
132+
.with_context(|| format!("Cannot parse config string {carp_config}"))?;
133+
134+
config
135+
};
130136

131137
let (network, mut sink) = match config.sink {
132138
SinkConfig::Cardano { ref network, .. } => (

0 commit comments

Comments
 (0)