Skip to content

Commit

Permalink
Provide option to point to different .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
limemloh committed Jan 29, 2025
1 parent 0153cd2 commit 4563dab
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
12 changes: 12 additions & 0 deletions backend-rust/.env.mainnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gRPC interface of the node.
CCDSCAN_INDEXER_GRPC_ENDPOINTS=https://grpc.mainnet.concordium.software
# CCDSCAN_INDEXER_GRPC_ENDPOINTS=http://localhost:20000

# Database connection used by the ccdscan-indexer.
CCDSCAN_INDEXER_DATABASE_URL=postgres://postgres:secret@localhost/ccdscan_mainnet

# Database connection used by the ccdscan-api.
CCDSCAN_API_DATABASE_URL=${CCDSCAN_INDEXER_DATABASE_URL}

# Run database migrations at startup.
CCDSCAN_INDEXER_MIGRATE=true
12 changes: 12 additions & 0 deletions backend-rust/.env.stagenet
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gRPC interface of the node.
CCDSCAN_INDEXER_GRPC_ENDPOINTS=https://grpc.stagenet.concordium.com
# CCDSCAN_INDEXER_GRPC_ENDPOINTS=http://localhost:20500

# Database connection used by the ccdscan-indexer.
CCDSCAN_INDEXER_DATABASE_URL=postgres://postgres:secret@localhost/ccdscan_stagenet

# Database connection used by the ccdscan-api.
CCDSCAN_API_DATABASE_URL=${CCDSCAN_INDEXER_DATABASE_URL}

# Run database migrations at startup.
CCDSCAN_INDEXER_MIGRATE=true
12 changes: 12 additions & 0 deletions backend-rust/.env.testnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gRPC interface of the node.
CCDSCAN_INDEXER_GRPC_ENDPOINTS=https://grpc.testnet.concordium.com
# CCDSCAN_INDEXER_GRPC_ENDPOINTS=http://localhost:20001

# Database connection used by the ccdscan-indexer.
CCDSCAN_INDEXER_DATABASE_URL=postgres://postgres:secret@localhost/ccdscan_testnet

# Database connection used by the ccdscan-api.
CCDSCAN_API_DATABASE_URL=${CCDSCAN_INDEXER_DATABASE_URL}

# Run database migrations at startup.
CCDSCAN_INDEXER_MIGRATE=true
22 changes: 21 additions & 1 deletion backend-rust/src/bin/ccdscan-api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,31 @@ struct Cli {
/// Non-zero exit code is returned when incompatible.
#[arg(long, env = "CCDSCAN_API_CHECK_DATABASE_COMPATIBILITY_ONLY")]
check_database_compatibility_only: bool,
/// Provide file to load environment variables from, instead of the default
/// `.env`.
// This is only part of this struct in order to generate help information.
// This argument is actually handled before hand using `DotenvCli`.
#[arg(long)]
dotenv: Option<PathBuf>,
}

/// CLI argument parser first used for parsing only the --dotenv option.
/// Allowing loading the provided file before parsing the remaining arguments
/// and producing errors
#[derive(Parser)]
#[command(ignore_errors = true, disable_help_flag = true, disable_version_flag = true)]
struct DotenvCli {
#[arg(long)]
dotenv: Option<PathBuf>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = dotenvy::dotenv();
if let Some(dotenv) = DotenvCli::parse().dotenv {
dotenvy::from_filename(dotenv)?;
} else {
let _ = dotenvy::dotenv();
}
let cli = Cli::parse();
tracing_subscriber::fmt().with_max_level(cli.log_level).init();
let pool = PgPoolOptions::new()
Expand Down
26 changes: 23 additions & 3 deletions backend-rust/src/bin/ccdscan-indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use prometheus_client::{
};
use serde_json::json;
use sqlx::postgres::PgPoolOptions;
use std::net::SocketAddr;
use std::{net::SocketAddr, path::PathBuf};
use tokio::net::TcpListener;
use tokio_util::sync::CancellationToken;
use tracing::{error, info};
Expand Down Expand Up @@ -42,7 +42,7 @@ struct Cli {
/// Address to listen for monitoring related requests
#[arg(long, env = "CCDSCAN_INDEXER_MONITORING_ADDRESS", default_value = "127.0.0.1:8001")]
monitoring_listen: SocketAddr,
#[command(flatten, next_help_heading = "Performance tuning")]
#[command(flatten)]
indexer_config: IndexerServiceConfig,
/// The maximum log level. Possible values are: `trace`, `debug`, `info`,
/// `warn`, and `error`.
Expand All @@ -56,11 +56,31 @@ struct Cli {
/// migrations with elevated privileges.
#[arg(long, env = "CCDSCAN_INDEXER_MIGRATE_ONLY")]
migrate_only: bool,
/// Provide file to load environment variables from, instead of the default
/// `.env`.
// This is only part of this struct in order to generate help information.
// This argument is actually handled before hand using `DotenvCli`.
#[arg(long)]
dotenv: Option<PathBuf>,
}

/// CLI argument parser first used for parsing only the --dotenv option.
/// Allowing loading the provided file before parsing the remaining arguments
/// and producing errors
#[derive(Parser)]
#[command(ignore_errors = true, disable_help_flag = true, disable_version_flag = true)]
struct DotenvCli {
#[arg(long)]
dotenv: Option<PathBuf>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = dotenvy::dotenv();
if let Some(dotenv) = DotenvCli::parse().dotenv {
dotenvy::from_filename(dotenv)?;
} else {
let _ = dotenvy::dotenv();
}
let cli = Cli::parse();
tracing_subscriber::fmt().with_max_level(cli.log_level).init();
let pool = PgPoolOptions::new()
Expand Down

0 comments on commit 4563dab

Please sign in to comment.