65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
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 http::StatusCode;
|
|
use mongodb::{bson::oid::ObjectId, Client};
|
|
|
|
pub async fn run() -> Result<()> {
|
|
let db = get_db_client().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?)
|
|
}
|
|
|
|
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<E> From<E> for AppError
|
|
where
|
|
E: Into<anyhow::Error>,
|
|
{
|
|
fn from(err: E) -> Self {
|
|
Self(err.into())
|
|
}
|
|
}
|