mod account; mod model; use anyhow::Result; use axum::{ body::Body, response::{IntoResponse, Response} }; use http::{StatusCode, Uri}; pub type ApiResult = Result; pub async fn run() -> Result<()> { let app = router(); let port = std::env::var("API_PORT")?; let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}")).await.unwrap(); axum::serve(listener, app).await.unwrap(); Ok(()) } fn router() -> axum::Router { axum::Router::new() .nest("/account", account::router()) .fallback(not_found) .nest("/predict", model::router()) } pub 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()) } } async fn not_found(uri: Uri) -> ApiResult { Ok(Response::builder() .status(StatusCode::NOT_FOUND) .body(Body::from(format!("The route {uri} does not exist")))?) }