Refactor and enhance SurrealDB integration and resource management
- Updated `lib.rs` to selectively expose specific entities and services for better clarity. - Improved SurrealDB client initialization with detailed logging in `surrealdb/mod.rs`. - Enhanced resource definitions in `resource.rs` with additional utility methods for better resource management. - Refactored user data retrieval logic in `auth_middleware/mod.rs` for improved readability and efficiency. - Cleaned up middleware exports in `lib.rs` for clearer API surface. - Added detailed comments and documentation throughout the SurrealDB module for better maintainability. - Updated tests to ensure compatibility with new changes and improved structure. - Introduced new permissions module structure in `imphnen-utils` for future enhancements.
This commit is contained in:
@@ -1,27 +1,80 @@
|
||||
//! Axum server initialization utilities.
|
||||
//!
|
||||
//! This module provides utilities for initializing and running an Axum web server
|
||||
//! with SurrealDB connections for both WebSocket and in-memory databases.
|
||||
|
||||
use crate::{surrealdb_init_mem, surrealdb_init_ws, SurrealMemClient, SurrealWsClient};
|
||||
use axum::{Router, serve};
|
||||
use std::{future::Future, net::SocketAddr};
|
||||
use tokio::net::TcpListener;
|
||||
use crate::enviroment::ENV;
|
||||
|
||||
/// Initialize and start the Axum server with SurrealDB connections.
|
||||
///
|
||||
/// This function sets up both WebSocket and in-memory SurrealDB connections,
|
||||
/// builds the router using the provided function, and starts the server.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `router_fn` - A function that takes SurrealDB clients and returns a Router
|
||||
///
|
||||
/// # Panics
|
||||
/// This function will panic if:
|
||||
/// - SurrealDB initialization fails
|
||||
/// - TCP listener binding fails
|
||||
///
|
||||
/// # Example
|
||||
/// ```no_run
|
||||
/// use axum::Router;
|
||||
/// use imphnen_libs::{axum_init, SurrealWsClient, SurrealMemClient};
|
||||
///
|
||||
/// async fn create_router(ws: SurrealWsClient, mem: SurrealMemClient) -> Router {
|
||||
/// Router::new()
|
||||
/// // Add your routes here
|
||||
/// }
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// axum_init(create_router).await;
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn axum_init<F, Fut>(router_fn: F)
|
||||
where
|
||||
F: FnOnce(SurrealWsClient, SurrealMemClient) -> Fut,
|
||||
Fut: Future<Output = Router>,
|
||||
F: FnOnce(SurrealWsClient, SurrealMemClient) -> Fut,
|
||||
Fut: Future<Output = Router>,
|
||||
{
|
||||
let env = &ENV;
|
||||
let env = &ENV;
|
||||
|
||||
let surrealdb_ws = surrealdb_init_ws().await.expect("Failed surrealdb ws");
|
||||
// Initialize SurrealDB connections
|
||||
log::info!("Initializing SurrealDB connections...");
|
||||
let surrealdb_ws = surrealdb_init_ws()
|
||||
.await
|
||||
.expect("Failed to initialize SurrealDB WebSocket connection");
|
||||
|
||||
let surrealdb_mem = surrealdb_init_mem().await.expect("Failed surrealdb mem");
|
||||
let surrealdb_mem = surrealdb_init_mem()
|
||||
.await
|
||||
.expect("Failed to initialize SurrealDB in-memory connection");
|
||||
|
||||
let router = router_fn(surrealdb_ws, surrealdb_mem).await;
|
||||
log::info!("SurrealDB connections established successfully");
|
||||
|
||||
let port = env.port;
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
||||
let listener = TcpListener::bind(&addr).await.unwrap();
|
||||
// Build the router
|
||||
let router = router_fn(surrealdb_ws, surrealdb_mem).await;
|
||||
|
||||
if let Err(err) = serve(listener, router).await {
|
||||
log::error!("Server failed to start: {}", err);
|
||||
}
|
||||
// Start the server
|
||||
let port = env.port;
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
||||
log::info!("Starting server on {}", addr);
|
||||
|
||||
let listener = TcpListener::bind(&addr)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to bind to address {}: {}", addr, e);
|
||||
panic!("Server binding failed: {}", e);
|
||||
});
|
||||
|
||||
log::info!("Server listening on {}", addr);
|
||||
|
||||
if let Err(err) = serve(listener, router).await {
|
||||
log::error!("Server encountered an error: {}", err);
|
||||
panic!("Server failed: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user