mod account; mod model; use account::db::{User, get_db_client}; use anyhow::Result; use axum::response::{IntoResponse, Response}; use axum_session::{SessionConfig, SessionLayer, SessionStore}; use axum_session_auth::{AuthConfig, AuthSessionLayer}; use axum_session_mongo::SessionMongoPool; use chrono::Utc; use http::StatusCode; use mongodb::{bson::oid::ObjectId, Client}; use rlg::{config::Config, macro_info_log}; pub async fn run() -> Result<()> { let id = "server"; init_logging(); macro_info_log!(&Utc::now().to_string(), id, "Server Initializing..."); let db = get_db_client().await?; let session_store = session(db.clone()).await?; let auth_config = AuthConfig::::default(); macro_info_log!(&Utc::now().to_string(), id, "Routes Initializing..."); let app = router() .layer(SessionLayer::new(session_store)) .layer(AuthSessionLayer:: ::new(Some(db)).with_config(auth_config) ); macro_info_log!(&Utc::now().to_string(), id, "Routes Initialized"); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await?; macro_info_log!(&Utc::now().to_string(), id, "Server Initialized"); Ok(()) } fn router() -> axum::Router { axum::Router::new() .nest("/account", account::router()) //.nest("/predict", model::router()) } async fn session(db: Client) -> Result> { let session_config = SessionConfig::default() .with_table_name("sessions"); Ok(SessionStore:: ::new(Some(db.clone().into()), session_config).await?) } struct AppError(anyhow::Error); impl IntoResponse for AppError { fn into_response(self) -> Response { ( StatusCode::INTERNAL_SERVER_ERROR, format!("Something went wrong: {}", self.0), ) .into_response() } } impl From for AppError where E: Into, { fn from(err: E) -> Self { Self(err.into()) } } fn init_logging() { std::env::set_var("LOG_FILE_PATH", "$HOME/.dermy/server.log"); let config = Config::load(); }