2025-09-26 17:35:30 +07:00
|
|
|
//! 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.
|
|
|
|
|
|
2025-10-28 14:04:41 +07:00
|
|
|
pub mod validated_json;
|
|
|
|
|
|
2025-09-26 15:05:45 +07:00
|
|
|
use crate::{surrealdb_init_mem, surrealdb_init_ws, SurrealMemClient, SurrealWsClient};
|
2025-05-20 10:45:01 +07:00
|
|
|
use axum::{Router, serve};
|
2025-02-07 22:26:32 +07:00
|
|
|
use std::{future::Future, net::SocketAddr};
|
|
|
|
|
use tokio::net::TcpListener;
|
2025-09-26 23:15:33 +07:00
|
|
|
use crate::environment::ENV;
|
2025-02-07 22:26:32 +07:00
|
|
|
|
2025-10-28 14:04:41 +07:00
|
|
|
pub use validated_json::ValidatedJson;
|
|
|
|
|
|
2025-09-26 17:35:30 +07:00
|
|
|
/// 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;
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2025-02-07 22:26:32 +07:00
|
|
|
pub async fn axum_init<F, Fut>(router_fn: F)
|
|
|
|
|
where
|
2025-09-26 17:35:30 +07:00
|
|
|
F: FnOnce(SurrealWsClient, SurrealMemClient) -> Fut,
|
|
|
|
|
Fut: Future<Output = Router>,
|
2025-02-07 22:26:32 +07:00
|
|
|
{
|
2025-09-26 17:35:30 +07:00
|
|
|
let env = &ENV;
|
2025-05-20 10:45:01 +07:00
|
|
|
|
2025-09-26 17:35:30 +07:00
|
|
|
// Initialize SurrealDB connections
|
|
|
|
|
log::info!("Initializing SurrealDB connections...");
|
|
|
|
|
let surrealdb_ws = surrealdb_init_ws()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to initialize SurrealDB WebSocket connection");
|
2025-05-20 10:45:01 +07:00
|
|
|
|
2025-09-26 17:35:30 +07:00
|
|
|
let surrealdb_mem = surrealdb_init_mem()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to initialize SurrealDB in-memory connection");
|
2025-05-20 10:45:01 +07:00
|
|
|
|
2025-09-26 17:35:30 +07:00
|
|
|
log::info!("SurrealDB connections established successfully");
|
2025-05-20 10:45:01 +07:00
|
|
|
|
2025-09-26 17:35:30 +07:00
|
|
|
// Build the router
|
|
|
|
|
let router = router_fn(surrealdb_ws, surrealdb_mem).await;
|
2025-05-20 10:45:01 +07:00
|
|
|
|
2025-09-26 17:35:30 +07:00
|
|
|
// 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);
|
|
|
|
|
}
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|