diff --git a/docker-compose.yml b/docker-compose.yml index c74a9b6..c4c3161 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,7 +28,8 @@ services: - hdrop - backend environment: - - PORT=80 + - HDROP_PORT=80 + - PROMETHEUS_PORT=3001 - STORAGE_PROVIDER=s3 - S3_ACCESS_KEY_ID=dev - S3_SECRET_ACCESS_KEY=dev diff --git a/hdrop-server/.env.example b/hdrop-server/.env.example index 1546b5f..aae6eda 100644 --- a/hdrop-server/.env.example +++ b/hdrop-server/.env.example @@ -1,4 +1,5 @@ -PORT=8080 +HDROP_PORT=8080 +PROMETHEUS_PORT=3001 S3_REGION=example-region-1 S3_ENDPOINT=https://foo.s3.example.org @@ -7,4 +8,4 @@ S3_SECRET_ACCESS_KEY= S3_BUCKET_NAME=hdrop S3_PUBLIC_URL=https://storage.example.org -CORS_ORIGIN=* \ No newline at end of file +CORS_ORIGIN=* diff --git a/hdrop-server/src/server/hdrop_server.rs b/hdrop-server/src/server/hdrop_server.rs index 506f2b4..78e2d31 100644 --- a/hdrop-server/src/server/hdrop_server.rs +++ b/hdrop-server/src/server/hdrop_server.rs @@ -178,7 +178,7 @@ impl Server { ); // Server configuration - let addr = SocketAddr::from(([0; 4], env::port().unwrap_or(8080))); + let addr = SocketAddr::from(([0; 4], env::hdrop_port().unwrap_or(8080))); tracing::info!("Starting server on {addr}"); diff --git a/hdrop-server/src/server/prometheus_metrics_server.rs b/hdrop-server/src/server/prometheus_metrics_server.rs index 15affa1..8997d8f 100644 --- a/hdrop-server/src/server/prometheus_metrics_server.rs +++ b/hdrop-server/src/server/prometheus_metrics_server.rs @@ -1,7 +1,7 @@ use std::{future::ready, net::SocketAddr}; use axum::{routing::get, Router}; -use hdrop_shared::metrics::names; +use hdrop_shared::{env, metrics::names}; use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle}; use tokio::net::TcpListener; @@ -52,7 +52,7 @@ impl PrometheusMetricsServer { /// Run the metrics server. pub async fn run(self) { let app = self.metrics_router(); - let addr = SocketAddr::from(([0; 4], 3001)); + let addr = SocketAddr::from(([0; 4], env::prometheus_port().unwrap_or(3001))); // Bind the listener to the address let listener = match TcpListener::bind(&addr).await { diff --git a/hdrop-shared/src/env.rs b/hdrop-shared/src/env.rs index 31a0ef6..165a8fb 100644 --- a/hdrop-shared/src/env.rs +++ b/hdrop-shared/src/env.rs @@ -43,29 +43,46 @@ macro_rules! env_get { }; } -// Server -env_get!(port => u16); -env_get!(cors_origin); -env_get!(single_file_limit_mb => usize); -env_get!(storage_provider); +macro_rules! map_env { + ($($name:ident => $type:ty),* $(,)?) => { + const ENV_VARS_BACKEND: &[&str] = &[$(stringify!($name),)*]; + $( + env_get!($name => $type); + )* + }; +} +// Generate the environment variables metadata and fetch functions +map_env!( +// Server +hdrop_port => u16, +prometheus_port => u16, +cors_origin => String, +single_file_limit_mb => usize, +storage_provider => String, // Database -env_get!(database_url); - +database_url => String, // Cache -env_get!(cache_strategy); -env_get!(cache_memory_limit_mb => usize); -env_get!(cache_disk_limit_mb => usize); -env_get!(cache_dir => PathBuf); - +cache_strategy => String, +cache_memory_limit_mb => usize, +cache_disk_limit_mb => usize, +cache_dir => PathBuf, // S3 Provider -env_get!(s3_region); -env_get!(s3_endpoint); -env_get!(s3_access_key_id); -env_get!(s3_secret_access_key); -env_get!(s3_bucket_name); -env_get!(s3_public_url); - +s3_region => String, +s3_endpoint => String, +s3_access_key_id => String, +s3_secret_access_key => String, +s3_bucket_name => String, +s3_public_url => String, // Local Provider -env_get!(local_storage_dir => PathBuf); -env_get!(local_storage_limit_mb => usize); +local_storage_dir => PathBuf, +local_storage_limit_mb => usize, +); + +/// Get a list of all environment variables used in the hdrop backend. +pub fn get_env_vars() -> Vec { + ENV_VARS_BACKEND + .iter() + .map(|&var| var.to_uppercase()) + .collect() +} diff --git a/start_api.sh b/start_api.sh index df93c94..d4e81c3 100755 --- a/start_api.sh +++ b/start_api.sh @@ -13,7 +13,8 @@ popd cargo build --release -p hdrop-server export RUST_BACKTRACE=1 -export PORT=8080 +export HDROP_PORT=8080 +export PROMETHEUS_PORT=3001 export CORS_ORIGIN="*" export S3_ACCESS_KEY_ID="dev" export S3_BUCKET_NAME="hdrop"