dermy-api/src/lib.rs

42 lines
1.2 KiB
Rust
Raw Normal View History

2024-06-06 18:21:12 +00:00
mod account;
mod model;
2024-06-07 01:24:42 +00:00
use account::db::User;
use anyhow::Result;
use axum_session::{SessionConfig, SessionLayer, SessionStore};
use axum_session_auth::{AuthConfig, AuthSessionLayer};
use axum_session_mongo::SessionMongoPool;
use mongodb::{bson::oid::ObjectId, Client};
pub async fn run() -> Result<()> {
let db = Client::with_uri_str("mongodb://localhost:27017").await?;
let session_store = session(db.clone()).await?;
let auth_config = AuthConfig::<ObjectId>::default();
let app = router()
.layer(SessionLayer::new(session_store))
.layer(AuthSessionLayer::<User, ObjectId, SessionMongoPool, Client>
::new(Some(db)).with_config(auth_config)
);
2024-06-06 18:25:03 +00:00
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
2024-06-07 01:24:42 +00:00
axum::serve(listener, app).await?;
Ok(())
2024-06-06 18:25:03 +00:00
}
fn router() -> axum::Router {
2024-06-06 18:21:12 +00:00
axum::Router::new()
.nest("/account", account::router())
.nest("/predict", model::router())
}
2024-06-07 01:24:42 +00:00
async fn session(db: Client) -> Result<SessionStore<SessionMongoPool>> {
let session_config = SessionConfig::default()
.with_table_name("sessions");
Ok(SessionStore::<SessionMongoPool>
::new(Some(db.clone().into()), session_config).await?)
}