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
run: |
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
.envrc
.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-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" }

View File

@ -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))

View File

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

View File

@ -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());

View File

@ -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"]

View File

@ -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<Config, anyhow::Error> {
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<Config, anyhow::Error> {
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<Config, anyhow::Error> {
},
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<Config, anyhow::Error> {
}
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.

View File

@ -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))?;