add initial endpoints
This commit is contained in:
parent
97d089c284
commit
3025f1380c
19 changed files with 300 additions and 9 deletions
|
@ -6,5 +6,10 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.4"
|
||||
eyre = "0.6.11"
|
||||
serde = { version = "1.0.195", features = ["derive"] }
|
||||
serde_json = "1.0.111"
|
||||
sqlx = "0.7.3"
|
||||
tracing = "0.1.40"
|
||||
uuid = { version = "1.6.1", features = ["v7"] }
|
||||
|
|
3
foxchat/src/http/mod.rs
Normal file
3
foxchat/src/http/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod response;
|
||||
|
||||
pub use response::{ApiError, ErrorCode};
|
64
foxchat/src/http/response.rs
Normal file
64
foxchat/src/http/response.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use eyre::Report;
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use tracing::error;
|
||||
|
||||
pub struct ApiError {
|
||||
status: StatusCode,
|
||||
code: ErrorCode,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError {
|
||||
fn into_response(self) -> Response {
|
||||
(
|
||||
self.status,
|
||||
Json(json!({
|
||||
"status": self.status.as_u16(),
|
||||
"code": self.code,
|
||||
"message": self.message,
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum ErrorCode {
|
||||
InternalServerError,
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for ApiError {
|
||||
fn from(err: sqlx::Error) -> Self {
|
||||
ApiError {
|
||||
status: StatusCode::INTERNAL_SERVER_ERROR,
|
||||
code: ErrorCode::InternalServerError,
|
||||
message: if cfg!(debug_assertions) {
|
||||
format!("Database error: {}", err)
|
||||
} else {
|
||||
"Database error".into()
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Report> for ApiError {
|
||||
fn from(err: Report) -> Self {
|
||||
error!("Error in handler: {}", err);
|
||||
ApiError {
|
||||
status: StatusCode::INTERNAL_SERVER_ERROR,
|
||||
code: ErrorCode::InternalServerError,
|
||||
message: if cfg!(debug_assertions) {
|
||||
format!("Internal server error: {}", err)
|
||||
} else {
|
||||
"Internal server error".into()
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod s2s;
|
||||
pub mod http;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue