Refactor environment module: Rename enviroment to environment and consolidate environment configuration management

- Updated all references from `enviroment` to `environment` across the codebase.
- Removed the old `enviroment` module and replaced it with a new `environment` module that includes centralized configuration management.
- Enhanced OTP generation to include secure hashing and expiration handling.
- Improved CSRF token generation and validation with better error handling.
- Cleaned up logging statements in various modules for clarity and consistency.
- Updated response formatting to include versioning from Cargo.toml.
- Removed unused mock test module from utils.
This commit is contained in:
MythEclipse
2025-09-26 23:15:33 +07:00
parent c12da948aa
commit 5859af5294
32 changed files with 164 additions and 141 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ use crate::{surrealdb_init_mem, surrealdb_init_ws, SurrealMemClient, SurrealWsCl
use axum::{Router, serve};
use std::{future::Future, net::SocketAddr};
use tokio::net::TcpListener;
use crate::enviroment::ENV;
use crate::environment::ENV;
/// Initialize and start the Axum server with SurrealDB connections.
///
+1 -1
View File
@@ -4,7 +4,7 @@
//! for authentication purposes, including access tokens, refresh tokens,
//! and password reset tokens.
use crate::enviroment::ENV;
use crate::environment::ENV;
use axum::http::StatusCode;
use chrono::{Duration, TimeDelta, Utc};
use jsonwebtoken::{
+3 -3
View File
@@ -3,7 +3,7 @@
//! This module provides functionality for sending emails through SMTP
//! with proper error handling and logging.
use crate::enviroment::ENV;
use crate::environment::ENV;
use lettre::message::Mailbox;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
@@ -88,7 +88,7 @@ fn build_email_message(
to: &str,
subject: &str,
body: &str,
env: &crate::enviroment::Env,
env: &crate::environment::Env,
) -> Result<Message, Box<dyn Error>> {
let sender_name = env.smtp_name.replace("-", " "); // Normalize sender name
@@ -107,7 +107,7 @@ fn build_email_message(
///
/// # Returns
/// Configured SMTP transport or error
fn create_smtp_transport(env: &crate::enviroment::Env) -> Result<SmtpTransport, Box<dyn Error>> {
fn create_smtp_transport(env: &crate::environment::Env) -> Result<SmtpTransport, Box<dyn Error>> {
let credentials = Credentials::new(
env.smtp_email.clone(),
env.smtp_password.replace("-", " "), // Normalize password
+19 -2
View File
@@ -1,8 +1,25 @@
/*!
# imphnen-libs
A collection of utility libraries and services for the imphnen project, providing integrations
with various external services and common functionality.
This crate includes modules for:
- Password hashing with Argon2 (`argon`)
- Axum web framework utilities (`axum`)
- Environment configuration (`environment`)
- JWT token handling (`jsonwebtoken`)
- Email sending with Lettre (`lettre`)
- MinIO object storage client (`minio`)
- Service abstractions (`services`)
- SurrealDB database client (`surrealdb`)
*/
use std::sync::Arc;
pub mod argon;
pub mod axum;
pub mod enviroment;
pub mod environment;
pub mod jsonwebtoken;
pub mod lettre;
pub mod minio;
@@ -11,7 +28,7 @@ pub mod surrealdb;
pub use argon::{hash_password, verify_password};
pub use axum::axum_init;
pub use enviroment::{ENV, Env};
pub use environment::{ENV, Env};
pub use imphnen_entities::{
MessageResponseDto,
MetaRequestDto,
+1 -30
View File
@@ -4,7 +4,7 @@ use chrono::Utc;
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
use uuid::Uuid;
use crate::enviroment::ENV;
use crate::environment::ENV;
@@ -108,12 +108,6 @@ impl MinioService {
let url = format!("https://{}/{}/{}", host, self.bucket_name, object_name);
// Debug logging
log::debug!("MinIO Endpoint config: {}", self.endpoint);
log::debug!("MinIO Region config: {}", self.region);
log::debug!("Upload URL: {}", url);
log::debug!("Object name: {}", object_name);
log::debug!("File hash: {}", short_hash);
let now = Utc::now();
let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
@@ -135,7 +129,6 @@ impl MinioService {
canonical_uri, canonical_headers, signed_headers, payload_hash
);
log::debug!("Canonical request:\n{}", canonical_request);
let scope = format!("{}/{}/s3/aws4_request", date_stamp, self.region);
let string_to_sign = format!(
@@ -153,7 +146,6 @@ impl MinioService {
mac.update(string_to_sign.as_bytes());
let signature = hex::encode(mac.finalize().into_bytes());
log::debug!("Generated signature: {}", signature);
let auth_header = format!(
"AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
@@ -211,13 +203,6 @@ impl MinioService {
let url = format!("https://{}/{}/{}", host, self.bucket_name, object_name);
// Debug logging
log::debug!("MinIO Endpoint config: {}", self.endpoint);
log::debug!("MinIO Region config: {}", self.region);
log::debug!("MinIO Access Key: {}", self.access_key);
log::debug!("MinIO Bucket: {}", self.bucket_name);
log::debug!("Extracted host: {}", host);
log::debug!("Final URL: {}", url);
let now = Utc::now();
let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string();
@@ -242,14 +227,6 @@ impl MinioService {
canonical_uri, canonical_headers, signed_headers, payload_hash
);
// Debug logging
log::debug!("URL: {}", url);
log::debug!("Host: {}", host);
log::debug!("Bucket: {}", self.bucket_name);
log::debug!("Object: {}", object_name);
log::debug!("Canonical URI: {}", canonical_uri);
log::debug!("Payload hash: {}", payload_hash);
log::debug!("Canonical Request:\n{}", canonical_request);
let scope = format!("{}/{}/s3/aws4_request", date_stamp, self.region);
let string_to_sign = format!(
@@ -259,15 +236,12 @@ impl MinioService {
hex::encode(Sha256::digest(canonical_request.as_bytes()))
);
log::debug!("Scope: {}", scope);
log::debug!("String to sign:\n{}", string_to_sign);
let signing_key = self.get_signature_key(&date_stamp)?;
let mut mac = Hmac::<Sha256>::new_from_slice(&signing_key)?;
mac.update(string_to_sign.as_bytes());
let signature = hex::encode(mac.finalize().into_bytes());
log::debug!("Generated signature: {}", signature);
let auth_header = format!(
"AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
@@ -475,15 +449,12 @@ impl MinioService {
// Debug logging for signature calculation
log::debug!("Region: {}", self.region);
log::debug!("Scope: {}", scope);
log::debug!("String to sign:\n{}", string_to_sign);
let signing_key = self.get_signature_key(&date_stamp)?;
let mut mac = Hmac::<Sha256>::new_from_slice(&signing_key)?;
mac.update(string_to_sign.as_bytes());
let signature = hex::encode(mac.finalize().into_bytes());
log::debug!("Final signature: {}", signature);
let auth_header = format!(
"AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
+1 -1
View File
@@ -3,7 +3,7 @@
//! This module provides utilities for initializing SurrealDB connections
//! for both WebSocket and in-memory databases, along with resource definitions.
use crate::enviroment::ENV;
use crate::environment::ENV;
use surrealdb::engine::any;
use surrealdb::engine::local::{Db, Mem};
use surrealdb::opt::auth::Root;