chore: run with different env file (#275)

This commit is contained in:
Nathan.fooo 2024-01-29 02:26:43 +08:00 committed by GitHub
parent 3a5a3f3e20
commit 56615e2274
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 63 additions and 32 deletions

View File

@ -65,5 +65,5 @@ jobs:
working-directory: ./libs/wasm-test working-directory: ./libs/wasm-test
run: | run: |
cargo install wasm-pack cargo install wasm-pack
wasm-pack test --headless --firefox --features="wasm_test" wasm-pack test --headless --firefox

2
.gitignore vendored
View File

@ -17,3 +17,5 @@ flake.nix
flake.lock flake.lock
.envrc .envrc
.direnv/ .direnv/
**/.env.*

View File

@ -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-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
collab-document = { 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 # 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 = { path = "libs/AppFlowy-Collab/collab" }
#collab-entity = { path = "libs/AppFlowy-Collab/collab-define" } #collab-entity = { path = "libs/AppFlowy-Collab/collab-define" }

View File

@ -61,7 +61,7 @@ MAX_RESTARTS=5
RESTARTS=0 RESTARTS=0
# Start the server and restart it on failure # Start the server and restart it on failure
while [ "$RESTARTS" -lt "$MAX_RESTARTS" ]; do 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=$! PID=$!
wait $PID || { wait $PID || {
RESTARTS=$((RESTARTS+1)) RESTARTS=$((RESTARTS+1))

View File

@ -33,6 +33,3 @@ gotrue.workspace = true
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features = ["console"] } web-sys = { version = "0.3", features = ["console"] }
[features]
wasm_test = []

View File

@ -5,7 +5,7 @@ use std::borrow::Cow;
use std::env; use std::env;
use tracing::warn; use tracing::warn;
#[cfg(not(feature = "wasm_test"))] #[cfg(not(target_arch = "wasm32"))]
lazy_static! { lazy_static! {
pub static ref LOCALHOST_URL: Cow<'static, str> = pub static ref LOCALHOST_URL: Cow<'static, str> =
get_env_var("LOCALHOST_URL", "http://localhost:8000"); get_env_var("LOCALHOST_URL", "http://localhost:8000");
@ -16,7 +16,7 @@ lazy_static! {
} }
// The env vars are not available in wasm32-unknown-unknown // The env vars are not available in wasm32-unknown-unknown
#[cfg(feature = "wasm_test")] #[cfg(target_arch = "wasm32")]
lazy_static! { lazy_static! {
pub static ref LOCALHOST_URL: Cow<'static, str> = Cow::Owned("http://localhost".to_string()); 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()); pub static ref LOCALHOST_WS: Cow<'static, str> = Cow::Owned("ws://localhost/ws".to_string());

View File

@ -13,6 +13,3 @@ client-api-test-util = { path = "../client-api-test-util" }
client-api = { path = "../client-api" } client-api = { path = "../client-api" }
tokio = { version = "1", features = ["sync", "macros"] } tokio = { version = "1", features = ["sync", "macros"] }
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"
[features]
wasm_test = ["client-api-test-util/wasm_test"]

View File

@ -1,3 +1,4 @@
use anyhow::Context;
use secrecy::Secret; use secrecy::Secret;
use serde::Deserialize; use serde::Deserialize;
use sqlx::postgres::{PgConnectOptions, PgSslMode}; use sqlx::postgres::{PgConnectOptions, PgSslMode};
@ -82,14 +83,20 @@ impl DatabaseSetting {
// Default values favor local development. // Default values favor local development.
pub fn get_configuration() -> Result<Config, anyhow::Error> { pub fn get_configuration() -> Result<Config, anyhow::Error> {
let config = Config { 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 { db_settings: DatabaseSetting {
pg_conn_opts: PgConnectOptions::from_str(&get_env_var( pg_conn_opts: PgConnectOptions::from_str(&get_env_var(
"APPFLOWY_DATABASE_URL", "APPFLOWY_DATABASE_URL",
"postgres://postgres:password@localhost:5432/postgres", "postgres://postgres:password@localhost:5432/postgres",
))?, ))?,
require_ssl: get_env_var("APPFLOWY_DATABASE_REQUIRE_SSL", "false").parse()?, require_ssl: get_env_var("APPFLOWY_DATABASE_REQUIRE_SSL", "false")
max_connections: get_env_var("APPFLOWY_DATABASE_MAX_CONNECTIONS", "20").parse()?, .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"), database_name: get_env_var("APPFLOWY_DATABASE_NAME", "postgres"),
}, },
gotrue: GoTrueSetting { gotrue: GoTrueSetting {
@ -102,7 +109,9 @@ pub fn get_configuration() -> Result<Config, anyhow::Error> {
application: ApplicationSetting { application: ApplicationSetting {
port: get_env_var("APPFLOWY_APPLICATION_PORT", "8000").parse()?, port: get_env_var("APPFLOWY_APPLICATION_PORT", "8000").parse()?,
host: get_env_var("APPFLOWY_APPLICATION_HOST", "0.0.0.0"), 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(), server_key: get_env_var("APPFLOWY_APPLICATION_SERVER_KEY", "server_key").into(),
}, },
websocket: WebsocketSetting { websocket: WebsocketSetting {
@ -111,7 +120,9 @@ pub fn get_configuration() -> Result<Config, anyhow::Error> {
}, },
redis_uri: get_env_var("APPFLOWY_REDIS_URI", "redis://localhost:6379").into(), redis_uri: get_env_var("APPFLOWY_REDIS_URI", "redis://localhost:6379").into(),
s3: S3Setting { 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"), minio_url: get_env_var("APPFLOWY_S3_MINIO_URL", "http://localhost:9000"),
access_key: get_env_var("APPFLOWY_S3_ACCESS_KEY", "minioadmin"), access_key: get_env_var("APPFLOWY_S3_ACCESS_KEY", "minioadmin"),
secret_key: get_env_var("APPFLOWY_S3_SECRET_KEY", "minioadmin").into(), secret_key: get_env_var("APPFLOWY_S3_SECRET_KEY", "minioadmin").into(),
@ -126,17 +137,14 @@ pub fn get_configuration() -> Result<Config, anyhow::Error> {
} }
fn get_env_var(key: &str, default: &str) -> String { fn get_env_var(key: &str, default: &str) -> String {
match std::env::var(key) { std::env::var(key).unwrap_or_else(|e| {
Ok(value) => value, tracing::warn!(
Err(e) => { "failed to read environment variable: {}, using default value: {}",
tracing::warn!( e,
"failed to read environment variable: {}, using default value: {}", default
e, );
default default.to_owned()
); })
default.to_owned()
},
}
} }
/// The possible runtime environment for our application. /// The possible runtime environment for our application.

View File

@ -1,15 +1,12 @@
use appflowy_cloud::application::{init_state, Application}; use appflowy_cloud::application::{init_state, Application};
use appflowy_cloud::config::config::get_configuration; use appflowy_cloud::config::config::get_configuration;
use appflowy_cloud::telemetry::init_subscriber; use appflowy_cloud::telemetry::init_subscriber;
use tracing::info;
#[actix_web::main] #[actix_web::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
// load from .env
dotenvy::dotenv().ok();
let level = std::env::var("RUST_LOG").unwrap_or("info".to_string()); let level = std::env::var("RUST_LOG").unwrap_or("info".to_string());
println!("AppFlowy Cloud with RUST_LOG={}", level); println!("AppFlowy Cloud with RUST_LOG={}", level);
let mut filters = vec![]; let mut filters = vec![];
filters.push(format!("actix_web={}", level)); filters.push(format!("actix_web={}", level));
filters.push(format!("collab={}", level)); filters.push(format!("collab={}", level));
@ -19,12 +16,39 @@ async fn main() -> anyhow::Result<()> {
filters.push(format!("realtime={}", level)); filters.push(format!("realtime={}", level));
filters.push(format!("database={}", level)); filters.push(format!("database={}", level));
filters.push(format!("storage={}", level)); filters.push(format!("storage={}", level));
let conf = let conf =
get_configuration().map_err(|e| anyhow::anyhow!("Failed to read configuration: {}", e))?; get_configuration().map_err(|e| anyhow::anyhow!("Failed to read configuration: {}", e))?;
init_subscriber(&conf.app_env, filters); 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) let state = init_state(&conf)
.await .await
.map_err(|e| anyhow::anyhow!("Failed to initialize application state: {}", e))?; .map_err(|e| anyhow::anyhow!("Failed to initialize application state: {}", e))?;