diff --git a/packages/backend/Cargo.lock b/packages/backend/Cargo.lock index 821851f11e..fbc3f212d5 100644 --- a/packages/backend/Cargo.lock +++ b/packages/backend/Cargo.lock @@ -15,6 +15,7 @@ dependencies = [ "config", "lazy_static", "logging", + "macros", "queue", "server", ] @@ -63,6 +64,13 @@ dependencies = [ "config", ] +[[package]] +name = "macros" +version = "0.1.0" +dependencies = [ + "lazy_static", +] + [[package]] name = "once_cell" version = "1.17.1" diff --git a/packages/backend/Cargo.toml b/packages/backend/Cargo.toml index bca8257e36..1d2e91c2e8 100644 --- a/packages/backend/Cargo.toml +++ b/packages/backend/Cargo.toml @@ -21,6 +21,7 @@ server = { path = "crates/server" } logging = { path = "crates/logging" } queue = { path = "crates/queue" } config = { path = "crates/config" } +macros = { path = "crates/macros" } lazy_static = "1.4.0" [dev-dependencies] diff --git a/packages/backend/crates/macros/Cargo.toml b/packages/backend/crates/macros/Cargo.toml new file mode 100644 index 0000000000..791e48996c --- /dev/null +++ b/packages/backend/crates/macros/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "macros" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lazy_static = "1.4.0" diff --git a/packages/backend/crates/macros/src/environment.rs b/packages/backend/crates/macros/src/environment.rs new file mode 100644 index 0000000000..47de90d823 --- /dev/null +++ b/packages/backend/crates/macros/src/environment.rs @@ -0,0 +1,54 @@ +#![macro_use] + +use std::env; + +use lazy_static::lazy_static; + +#[derive(PartialEq)] +pub enum EnvType { + Release, + Debug, + Test, +} + +lazy_static! { + pub static ref NODE_ENV: EnvType = init_env_type(); +} + +#[macro_export] +macro_rules! node_env { + () => { + *macros::environment::NODE_ENV + }; +} + +#[macro_export] +macro_rules! is_debug { + () => { + macros::node_env!() == macros::environment::EnvType::Debug + }; +} + +#[macro_export] +macro_rules! is_release { + () => { + macros::node_env!() == macros::environment::EnvType::Release + }; +} + +#[macro_export] +macro_rules! is_test { + () => { + macros::node_env!() == macros::environment::EnvType::Test + }; +} + +fn init_env_type() -> EnvType { + use EnvType::*; + match env::var("NODE_ENV") { + Ok(s) if s == *"production" => Release, + Ok(s) if s == *"development" => Debug, + Ok(s) if s == *"test" => Test, + _ => Debug, + } +} diff --git a/packages/backend/crates/macros/src/lib.rs b/packages/backend/crates/macros/src/lib.rs new file mode 100644 index 0000000000..529a61f102 --- /dev/null +++ b/packages/backend/crates/macros/src/lib.rs @@ -0,0 +1,3 @@ +pub mod environment; + +//pub use environment::*; diff --git a/packages/backend/src/main.rs b/packages/backend/src/main.rs index bc5198c3f7..b6268f3d21 100644 --- a/packages/backend/src/main.rs +++ b/packages/backend/src/main.rs @@ -3,7 +3,8 @@ use std::{ path::{Path, PathBuf}, }; -extern crate config; +#[macro_use] +extern crate macros; fn main() -> Result<(), Box> { env::set_var(