2024-06-06 18:21:12 +00:00
|
|
|
mod account;
|
|
|
|
|
mod model;
|
|
|
|
|
|
2024-06-07 01:24:42 +00:00
|
|
|
use anyhow::Result;
|
2024-06-08 15:36:06 +00:00
|
|
|
use axum::{
|
|
|
|
|
body::Body,
|
|
|
|
|
response::{IntoResponse, Response}
|
|
|
|
|
};
|
|
|
|
|
use http::{StatusCode, Uri};
|
|
|
|
|
|
|
|
|
|
pub type ApiResult = Result<Response, AppError>;
|
2024-06-07 01:24:42 +00:00
|
|
|
|
|
|
|
|
pub async fn run() -> Result<()> {
|
|
|
|
|
|
2024-06-07 21:29:49 +00:00
|
|
|
let app = router();
|
2024-06-10 17:43:24 +00:00
|
|
|
let port = std::env::var("API_PORT")?;
|
|
|
|
|
let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}")).await.unwrap();
|
2024-06-07 21:29:49 +00:00
|
|
|
axum::serve(listener, app).await.unwrap();
|
|
|
|
|
|
|
|
|
|
|
2024-06-07 01:24:42 +00:00
|
|
|
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())
|
2024-06-08 15:36:06 +00:00
|
|
|
.fallback(not_found)
|
|
|
|
|
.nest("/predict", model::router())
|
2024-06-06 18:21:12 +00:00
|
|
|
}
|
2024-06-07 01:24:42 +00:00
|
|
|
|
2024-06-10 17:43:24 +00:00
|
|
|
pub struct AppError(anyhow::Error);
|
2024-06-07 14:30:16 +00:00
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-08 15:36:06 +00:00
|
|
|
|
|
|
|
|
async fn not_found(uri: Uri) -> ApiResult {
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from(format!("The route {uri} does not exist")))?)
|
|
|
|
|
}
|