From 56615e2274cb8e116f9dead0811acce6338bae7d Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Mon, 29 Jan 2024 02:26:43 +0800 Subject: [PATCH] chore: run with different env file (#275) --- .github/workflows/integration_test.yml | 2 +- .gitignore | 2 ++ Cargo.toml | 3 ++ build/run_local_server.sh | 2 +- libs/client-api-test-util/Cargo.toml | 3 -- libs/client-api-test-util/src/client.rs | 4 +-- libs/wasm-test/Cargo.toml | 3 -- src/config/config.rs | 40 +++++++++++++++---------- src/main.rs | 36 ++++++++++++++++++---- 9 files changed, 63 insertions(+), 32 deletions(-) diff --git a/.github/workflows/integration_test.yml b/.github/workflows/integration_test.yml index 2efd75fb..223b5ef4 100644 --- a/.github/workflows/integration_test.yml +++ b/.github/workflows/integration_test.yml @@ -65,5 +65,5 @@ jobs: working-directory: ./libs/wasm-test run: | cargo install wasm-pack - wasm-pack test --headless --firefox --features="wasm_test" + wasm-pack test --headless --firefox diff --git a/.gitignore b/.gitignore index 9e804ce3..d701c4e4 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ flake.nix flake.lock .envrc .direnv/ + +**/.env.* \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index dc982a96..3db9679a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -169,6 +169,9 @@ collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" } collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" } +[features] +custom_env= [] + # Comment the above and uncomment the below to use local version of collab by cloning the repo and placing it in libs folder #collab = { path = "libs/AppFlowy-Collab/collab" } #collab-entity = { path = "libs/AppFlowy-Collab/collab-define" } diff --git a/build/run_local_server.sh b/build/run_local_server.sh index f327366b..1d628919 100755 --- a/build/run_local_server.sh +++ b/build/run_local_server.sh @@ -61,7 +61,7 @@ MAX_RESTARTS=5 RESTARTS=0 # Start the server and restart it on failure while [ "$RESTARTS" -lt "$MAX_RESTARTS" ]; do - RUST_LOG=trace RUST_BACKTRACE=full cargo run & + RUST_LOG=trace RUST_BACKTRACE=full cargo run --features="custom_env" & PID=$! wait $PID || { RESTARTS=$((RESTARTS+1)) diff --git a/libs/client-api-test-util/Cargo.toml b/libs/client-api-test-util/Cargo.toml index 7d2fd4f8..957b8e38 100644 --- a/libs/client-api-test-util/Cargo.toml +++ b/libs/client-api-test-util/Cargo.toml @@ -33,6 +33,3 @@ gotrue.workspace = true [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3", features = ["console"] } - -[features] -wasm_test = [] \ No newline at end of file diff --git a/libs/client-api-test-util/src/client.rs b/libs/client-api-test-util/src/client.rs index 057ddae8..3f7b52f1 100644 --- a/libs/client-api-test-util/src/client.rs +++ b/libs/client-api-test-util/src/client.rs @@ -5,7 +5,7 @@ use std::borrow::Cow; use std::env; use tracing::warn; -#[cfg(not(feature = "wasm_test"))] +#[cfg(not(target_arch = "wasm32"))] lazy_static! { pub static ref LOCALHOST_URL: Cow<'static, str> = get_env_var("LOCALHOST_URL", "http://localhost:8000"); @@ -16,7 +16,7 @@ lazy_static! { } // The env vars are not available in wasm32-unknown-unknown -#[cfg(feature = "wasm_test")] +#[cfg(target_arch = "wasm32")] lazy_static! { pub static ref LOCALHOST_URL: Cow<'static, str> = Cow::Owned("http://localhost".to_string()); pub static ref LOCALHOST_WS: Cow<'static, str> = Cow::Owned("ws://localhost/ws".to_string()); diff --git a/libs/wasm-test/Cargo.toml b/libs/wasm-test/Cargo.toml index ac8ed270..c1e39c33 100644 --- a/libs/wasm-test/Cargo.toml +++ b/libs/wasm-test/Cargo.toml @@ -13,6 +13,3 @@ client-api-test-util = { path = "../client-api-test-util" } client-api = { path = "../client-api" } tokio = { version = "1", features = ["sync", "macros"] } wasm-bindgen-futures = "0.4" - -[features] -wasm_test = ["client-api-test-util/wasm_test"] \ No newline at end of file diff --git a/src/config/config.rs b/src/config/config.rs index e89432ad..27e430b0 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use secrecy::Secret; use serde::Deserialize; use sqlx::postgres::{PgConnectOptions, PgSslMode}; @@ -82,14 +83,20 @@ impl DatabaseSetting { // Default values favor local development. pub fn get_configuration() -> Result { let config = Config { - app_env: get_env_var("APPFLOWY_ENVIRONMENT", "local").parse()?, + app_env: get_env_var("APPFLOWY_ENVIRONMENT", "local") + .parse() + .context("fail to get APPFLOWY_ENVIRONMENT")?, db_settings: DatabaseSetting { pg_conn_opts: PgConnectOptions::from_str(&get_env_var( "APPFLOWY_DATABASE_URL", "postgres://postgres:password@localhost:5432/postgres", ))?, - require_ssl: get_env_var("APPFLOWY_DATABASE_REQUIRE_SSL", "false").parse()?, - max_connections: get_env_var("APPFLOWY_DATABASE_MAX_CONNECTIONS", "20").parse()?, + require_ssl: get_env_var("APPFLOWY_DATABASE_REQUIRE_SSL", "false") + .parse() + .context("fail to get APPFLOWY_DATABASE_REQUIRE_SSL")?, + max_connections: get_env_var("APPFLOWY_DATABASE_MAX_CONNECTIONS", "20") + .parse() + .context("fail to get APPFLOWY_DATABASE_MAX_CONNECTIONS")?, database_name: get_env_var("APPFLOWY_DATABASE_NAME", "postgres"), }, gotrue: GoTrueSetting { @@ -102,7 +109,9 @@ pub fn get_configuration() -> Result { application: ApplicationSetting { port: get_env_var("APPFLOWY_APPLICATION_PORT", "8000").parse()?, host: get_env_var("APPFLOWY_APPLICATION_HOST", "0.0.0.0"), - use_tls: get_env_var("APPFLOWY_APPLICATION_USE_TLS", "false").parse()?, + use_tls: get_env_var("APPFLOWY_APPLICATION_USE_TLS", "false") + .parse() + .context("fail to get APPFLOWY_APPLICATION_USE_TLS")?, server_key: get_env_var("APPFLOWY_APPLICATION_SERVER_KEY", "server_key").into(), }, websocket: WebsocketSetting { @@ -111,7 +120,9 @@ pub fn get_configuration() -> Result { }, redis_uri: get_env_var("APPFLOWY_REDIS_URI", "redis://localhost:6379").into(), s3: S3Setting { - use_minio: get_env_var("APPFLOWY_S3_USE_MINIO", "true").parse()?, + use_minio: get_env_var("APPFLOWY_S3_USE_MINIO", "true") + .parse() + .context("fail to get APPFLOWY_S3_USE_MINIO")?, minio_url: get_env_var("APPFLOWY_S3_MINIO_URL", "http://localhost:9000"), access_key: get_env_var("APPFLOWY_S3_ACCESS_KEY", "minioadmin"), secret_key: get_env_var("APPFLOWY_S3_SECRET_KEY", "minioadmin").into(), @@ -126,17 +137,14 @@ pub fn get_configuration() -> Result { } fn get_env_var(key: &str, default: &str) -> String { - match std::env::var(key) { - Ok(value) => value, - Err(e) => { - tracing::warn!( - "failed to read environment variable: {}, using default value: {}", - e, - default - ); - default.to_owned() - }, - } + std::env::var(key).unwrap_or_else(|e| { + tracing::warn!( + "failed to read environment variable: {}, using default value: {}", + e, + default + ); + default.to_owned() + }) } /// The possible runtime environment for our application. diff --git a/src/main.rs b/src/main.rs index 490b884d..1dde1649 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,12 @@ use appflowy_cloud::application::{init_state, Application}; use appflowy_cloud::config::config::get_configuration; use appflowy_cloud::telemetry::init_subscriber; +use tracing::info; #[actix_web::main] async fn main() -> anyhow::Result<()> { - // load from .env - dotenvy::dotenv().ok(); - let level = std::env::var("RUST_LOG").unwrap_or("info".to_string()); println!("AppFlowy Cloud with RUST_LOG={}", level); - let mut filters = vec![]; filters.push(format!("actix_web={}", level)); filters.push(format!("collab={}", level)); @@ -19,12 +16,39 @@ async fn main() -> anyhow::Result<()> { filters.push(format!("realtime={}", level)); filters.push(format!("database={}", level)); filters.push(format!("storage={}", level)); - let conf = get_configuration().map_err(|e| anyhow::anyhow!("Failed to read configuration: {}", e))?; - init_subscriber(&conf.app_env, filters); + // If current build is debug and the feature "custom_env" is not enabled, load from .env + // otherwise, load from .env.without_nginx. + if cfg!(debug_assertions) { + #[cfg(not(feature = "custom_env"))] + { + info!("custom_env is disable, load from .env"); + dotenvy::dotenv().ok(); + } + + #[cfg(feature = "custom_env")] + { + match dotenvy::from_filename(".env.without_nginx") { + Ok(_) => { + info!("custom_env is enabled, load from .env.without_nginx"); + }, + Err(err) => { + tracing::error!( + "Failed to load .env.without_nginx: {}, fallback to .env file", + err + ); + dotenvy::dotenv().ok(); + }, + } + } + } else { + // In release, always load from .env + dotenvy::dotenv().ok(); + } + let state = init_state(&conf) .await .map_err(|e| anyhow::anyhow!("Failed to initialize application state: {}", e))?;