feat: add dotenvy for environment variable management and improve error handling in Env struct
This commit is contained in:
Generated
+7
@@ -1091,6 +1091,12 @@ dependencies = [
|
||||
"urlencoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenvy"
|
||||
version = "0.15.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
|
||||
|
||||
[[package]]
|
||||
name = "double-ended-peekable"
|
||||
version = "0.1.0"
|
||||
@@ -2090,6 +2096,7 @@ dependencies = [
|
||||
"argon2",
|
||||
"axum",
|
||||
"chrono",
|
||||
"dotenvy",
|
||||
"imphnen-entities",
|
||||
"jsonwebtoken",
|
||||
"lettre",
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ fancy-regex = "0.14.0"
|
||||
futures = "0.3.31"
|
||||
tower = "0.5.2"
|
||||
env_logger = "0.11.8"
|
||||
|
||||
dotenvy = "0.15.7"
|
||||
[profile.release]
|
||||
lto = "fat"
|
||||
codegen-units = 1
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use imphnen_cms::v1::landing::events::events_schema::EventsSchema;
|
||||
use imphnen_utils::{get_iso_date, Env};
|
||||
use std::error::Error;
|
||||
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, sql::Thing, Surreal};
|
||||
|
||||
use surrealdb::engine::any;
|
||||
use surrealdb::{opt::auth::Root, sql::Thing};
|
||||
use imphnen_libs::enviroment::load_env;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
load_env();
|
||||
let env = Env::new();
|
||||
let db = Surreal::new::<Ws>(env.surrealdb_url).await?;
|
||||
let db = any::connect(&env.surrealdb_url).await?;
|
||||
db.signin(Root {
|
||||
username: &env.surrealdb_username,
|
||||
password: &env.surrealdb_password,
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
use imphnen_iam::PermissionsEnum;
|
||||
use imphnen_libs::enviroment::load_env;
|
||||
use imphnen_utils::{get_iso_date, Env};
|
||||
use serde_json::json;
|
||||
use std::error::Error;
|
||||
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, Surreal};
|
||||
use surrealdb::engine::any;
|
||||
use surrealdb::opt::auth::Root;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
load_env();
|
||||
|
||||
let env = Env::new();
|
||||
let db = Surreal::new::<Ws>(env.surrealdb_url).await?;
|
||||
let db = any::connect(&env.surrealdb_url).await?;
|
||||
db.signin(Root {
|
||||
username: &env.surrealdb_username,
|
||||
password: &env.surrealdb_password,
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use imphnen_utils::{get_iso_date, Env};
|
||||
use serde_json::json;
|
||||
use std::error::Error;
|
||||
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, Surreal};
|
||||
|
||||
use surrealdb::opt::auth::Root;
|
||||
use imphnen_libs::enviroment::load_env;
|
||||
use surrealdb::engine::any;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
load_env();
|
||||
let env = Env::new();
|
||||
let db = Surreal::new::<Ws>(env.surrealdb_url).await?;
|
||||
let db = any::connect(&env.surrealdb_url).await?;
|
||||
db.signin(Root {
|
||||
username: &env.surrealdb_username,
|
||||
password: &env.surrealdb_password,
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use imphnen_iam::{get_iso_date, make_thing, Env, PermissionsEnum};
|
||||
use std::error::Error;
|
||||
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, Surreal};
|
||||
|
||||
use surrealdb::opt::auth::Root;
|
||||
use surrealdb::engine::any;
|
||||
use imphnen_libs::enviroment::load_env;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
load_env();
|
||||
let env = Env::new();
|
||||
let db = Surreal::new::<Ws>(env.surrealdb_url).await?;
|
||||
let db = any::connect(&env.surrealdb_url).await?;
|
||||
db.signin(Root {
|
||||
username: &env.surrealdb_username,
|
||||
password: &env.surrealdb_password,
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use imphnen_iam::UsersSchema;
|
||||
use imphnen_libs::enviroment::load_env;
|
||||
use imphnen_utils::{get_iso_date, hash_password, Env};
|
||||
use std::error::Error;
|
||||
use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, sql::Thing, Surreal};
|
||||
|
||||
use surrealdb::{opt::auth::Root, sql::Thing};
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
load_env();
|
||||
let env = Env::new();
|
||||
let db = Surreal::new::<Ws>(env.surrealdb_url).await?;
|
||||
use surrealdb::engine::any;
|
||||
let db = any::connect(&env.surrealdb_url).await?;
|
||||
db.signin(Root {
|
||||
username: &env.surrealdb_username,
|
||||
password: &env.surrealdb_password,
|
||||
|
||||
@@ -14,3 +14,5 @@ lettre.workspace = true
|
||||
chrono.workspace = true
|
||||
surrealdb.workspace = true
|
||||
jsonwebtoken.workspace = true
|
||||
dotenvy.workspace = true
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
use std::env;
|
||||
|
||||
pub fn load_env() {
|
||||
dotenvy::dotenv().ok();
|
||||
}
|
||||
|
||||
pub struct Env {
|
||||
pub port: u16,
|
||||
pub access_token_secret: String,
|
||||
@@ -23,45 +27,122 @@ pub struct Env {
|
||||
}
|
||||
|
||||
impl Env {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
port: env::var("PORT")
|
||||
.unwrap_or_else(|_| "3000".to_string())
|
||||
.unwrap_or_else(|_| {
|
||||
println!("INFO: PORT is not set, using default '3000'.");
|
||||
"3000".to_string()
|
||||
})
|
||||
.parse()
|
||||
.unwrap_or(3000),
|
||||
access_token_secret: env::var("ACCESS_TOKEN_SECRET")
|
||||
.unwrap_or_else(|_| "default_access_secret".to_string()),
|
||||
refresh_token_secret: env::var("REFRESH_TOKEN_SECRET")
|
||||
.unwrap_or_else(|_| "default_refresh_secret".to_string()),
|
||||
surrealdb_url: env::var("SURREALDB_URL")
|
||||
.unwrap_or_else(|_| "http://localhost:8000".to_string()),
|
||||
surrealdb_username: env::var("SURREALDB_USERNAME")
|
||||
.unwrap_or_else(|_| "root".to_string()),
|
||||
surrealdb_password: env::var("SURREALDB_PASSWORD")
|
||||
.unwrap_or_else(|_| "password".to_string()),
|
||||
surrealdb_namespace: env::var("SURREALDB_NAMESPACE")
|
||||
.unwrap_or_else(|_| "namespace".to_string()),
|
||||
surrealdb_dbname: env::var("SURREALDB_DBNAME")
|
||||
.unwrap_or_else(|_| "database".to_string()),
|
||||
smtp_email: env::var("SMTP_EMAIL")
|
||||
.unwrap_or_else(|_| "no-reply@example.com".to_string()),
|
||||
smtp_password: env::var("SMTP_PASSWORD")
|
||||
.unwrap_or_else(|_| "default_smtp_password".to_string()),
|
||||
smtp_name: env::var("SMTP_NAME").unwrap_or_else(|_| "MyApp SMTP".to_string()),
|
||||
smtp_host: env::var("SMTP_HOST")
|
||||
.unwrap_or_else(|_| "smtp.gmail.com".to_string()),
|
||||
redisdb_url: env::var("REDISDB_URL")
|
||||
.unwrap_or_else(|_| "localhost".to_string()),
|
||||
fe_url: env::var("FE_URL").unwrap_or_else(|_| "http://localhost".to_string()),
|
||||
rust_env: env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()),
|
||||
minio_endpoint: env::var("MINIO_ENDPOINT")
|
||||
.unwrap_or_else(|_| "http://localhost:9000".to_string()),
|
||||
minio_bucket_name: env::var("MINIO_BUCKET_NAME")
|
||||
.unwrap_or_else(|_| "default_bucket".to_string()),
|
||||
minio_access_key: env::var("MINIO_ACCESS_KEY")
|
||||
.unwrap_or_else(|_| "minio_access".to_string()),
|
||||
minio_secret_key: env::var("MINIO_SECRET_KEY")
|
||||
.unwrap_or_else(|_| "minio_secret".to_string()),
|
||||
|
||||
rust_env: env::var("RUST_ENV").unwrap_or_else(|_| {
|
||||
println!("INFO: RUST_ENV is not set, using default 'development'.");
|
||||
"development".to_string()
|
||||
}),
|
||||
|
||||
access_token_secret: env::var("ACCESS_TOKEN_SECRET").unwrap_or_else(|_| {
|
||||
println!("WARNING: ACCESS_TOKEN_SECRET is not set, using fallback!");
|
||||
"default_access_secret".to_string()
|
||||
}),
|
||||
|
||||
refresh_token_secret: env::var("REFRESH_TOKEN_SECRET").unwrap_or_else(|_| {
|
||||
println!("WARNING: REFRESH_TOKEN_SECRET is not set, using fallback!");
|
||||
"default_refresh_secret".to_string()
|
||||
}),
|
||||
|
||||
surrealdb_url: env::var("SURREALDB_URL").unwrap_or_else(|_| {
|
||||
println!(
|
||||
"WARNING: SURREALDB_URL is not set, using fallback 'http://localhost:8000'!"
|
||||
);
|
||||
"http://localhost:8000".to_string()
|
||||
}),
|
||||
|
||||
surrealdb_username: env::var("SURREALDB_USERNAME").unwrap_or_else(|_| {
|
||||
println!("WARNING: SURREALDB_USERNAME is not set, using fallback 'root'!");
|
||||
"root".to_string()
|
||||
}),
|
||||
|
||||
surrealdb_password: env::var("SURREALDB_PASSWORD").unwrap_or_else(|_| {
|
||||
println!("WARNING: SURREALDB_PASSWORD is not set, using fallback!");
|
||||
"password".to_string()
|
||||
}),
|
||||
|
||||
surrealdb_namespace: env::var("SURREALDB_NAMESPACE").unwrap_or_else(|_| {
|
||||
println!(
|
||||
"WARNING: SURREALDB_NAMESPACE is not set, using fallback 'namespace'!"
|
||||
);
|
||||
"namespace".to_string()
|
||||
}),
|
||||
|
||||
surrealdb_dbname: env::var("SURREALDB_DBNAME").unwrap_or_else(|_| {
|
||||
println!("WARNING: SURREALDB_DBNAME is not set, using fallback 'database'!");
|
||||
"database".to_string()
|
||||
}),
|
||||
|
||||
smtp_email: env::var("SMTP_EMAIL").unwrap_or_else(|_| {
|
||||
println!(
|
||||
"WARNING: SMTP_EMAIL is not set, using fallback 'no-reply@example.com'!"
|
||||
);
|
||||
"no-reply@example.com".to_string()
|
||||
}),
|
||||
|
||||
smtp_password: env::var("SMTP_PASSWORD").unwrap_or_else(|_| {
|
||||
println!("WARNING: SMTP_PASSWORD is not set, using fallback!");
|
||||
"default_smtp_password".to_string()
|
||||
}),
|
||||
|
||||
smtp_name: env::var("SMTP_NAME").unwrap_or_else(|_| {
|
||||
println!("WARNING: SMTP_NAME is not set, using fallback 'MyApp SMTP'!");
|
||||
"MyApp SMTP".to_string()
|
||||
}),
|
||||
|
||||
smtp_host: env::var("SMTP_HOST").unwrap_or_else(|_| {
|
||||
println!("WARNING: SMTP_HOST is not set, using fallback 'smtp.gmail.com'!");
|
||||
"smtp.gmail.com".to_string()
|
||||
}),
|
||||
|
||||
redisdb_url: env::var("REDISDB_URL").unwrap_or_else(|_| {
|
||||
println!("WARNING: REDISDB_URL is not set, using fallback 'localhost'!");
|
||||
"localhost".to_string()
|
||||
}),
|
||||
|
||||
fe_url: env::var("FE_URL").unwrap_or_else(|_| {
|
||||
println!("WARNING: FE_URL is not set, using fallback 'http://localhost'!");
|
||||
"http://localhost".to_string()
|
||||
}),
|
||||
|
||||
minio_endpoint: env::var("MINIO_ENDPOINT").unwrap_or_else(|_| {
|
||||
println!(
|
||||
"WARNING: MINIO_ENDPOINT is not set, using fallback 'http://localhost:9000'!"
|
||||
);
|
||||
"http://localhost:9000".to_string()
|
||||
}),
|
||||
|
||||
minio_bucket_name: env::var("MINIO_BUCKET_NAME").unwrap_or_else(|_| {
|
||||
println!(
|
||||
"WARNING: MINIO_BUCKET_NAME is not set, using fallback 'default_bucket'!"
|
||||
);
|
||||
"default_bucket".to_string()
|
||||
}),
|
||||
|
||||
minio_access_key: env::var("MINIO_ACCESS_KEY").unwrap_or_else(|_| {
|
||||
println!("WARNING: MINIO_ACCESS_KEY is not set, using fallback!");
|
||||
"minio_access".to_string()
|
||||
}),
|
||||
|
||||
minio_secret_key: env::var("MINIO_SECRET_KEY").unwrap_or_else(|_| {
|
||||
println!("WARNING: MINIO_SECRET_KEY is not set, using fallback!");
|
||||
"minio_secret".to_string()
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Env {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user