Initial commit with basic axum and sqlx API

This commit is contained in:
2023-05-07 17:41:45 -04:00
commit c2c0f7a28d
13 changed files with 2355 additions and 0 deletions

26
src/error.rs Normal file
View File

@@ -0,0 +1,26 @@
use axum::{http::StatusCode, response::{IntoResponse, Response}};
// Make our own error that wraps `anyhow::Error`.
pub struct AppError(anyhow::Error);
// Tell axum how to convert `AppError` into a response.
impl IntoResponse for AppError {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", self.0),
)
.into_response()
}
}
// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
// `Result<_, AppError>`. That way you don't need to do that manually.
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}