42 lines
1.2 KiB
Rust
42 lines
1.2 KiB
Rust
mod account;
|
|
mod model;
|
|
|
|
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)
|
|
);
|
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
|
|
|
axum::serve(listener, app).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn router() -> axum::Router {
|
|
axum::Router::new()
|
|
.nest("/account", account::router())
|
|
.nest("/predict", model::router())
|
|
}
|
|
|
|
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?)
|
|
}
|