Krys4lide/crates/backend/src/lib.rs

38 lines
897 B
Rust
Raw Normal View History

2024-09-23 18:55:31 +02:00
use anyhow::Error as AnyError;
2024-09-23 18:02:07 +02:00
use axum::http::{StatusCode, Uri};
2024-09-23 18:55:31 +02:00
use axum::response::{IntoResponse, Response};
2024-09-23 18:02:07 +02:00
use axum::{routing::get, Router};
pub fn get_router() -> Router {
Router::new()
.route("/", get(|| async { "Hello, world!" }))
.fallback(fallback)
}
async fn fallback(uri: Uri) -> (StatusCode, String) {
(StatusCode::NOT_FOUND, format!("No route for {uri}"))
}
2024-09-23 18:55:31 +02:00
struct AppError(AnyError);
// To automatically convert `AppError` into a response
impl IntoResponse for AppError {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal Server Error: {}", self.0),
)
.into_response()
}
}
// To automatically convert `AnyError` into `AppError`
impl<E> From<E> for AppError
where
E: Into<AnyError>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}