summaryrefslogtreecommitdiff
path: root/src/types/response.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/types/response.rs')
-rw-r--r--src/types/response.rs9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/types/response.rs b/src/types/response.rs
index 72c1334..de572b2 100644
--- a/src/types/response.rs
+++ b/src/types/response.rs
@@ -1,6 +1,7 @@
use axum::{response::{IntoResponse, Response}, http::{StatusCode, Request, HeaderValue}, body::Body, headers::HeaderName};
use tower::ServiceExt;
use tower_http::services::ServeFile;
+use tracing::instrument;
#[derive(Debug)]
pub enum ResponseCode {
@@ -15,6 +16,7 @@ pub enum ResponseCode {
}
impl ResponseCode {
+
pub fn code(self) -> StatusCode {
match self {
Self::Success => StatusCode::OK,
@@ -28,10 +30,12 @@ impl ResponseCode {
}
}
+ #[instrument()]
pub fn text(self, msg: &str) -> Response {
(self.code(), msg.to_owned()).into_response()
}
+ #[instrument()]
pub fn json(self, json: &str) -> Response {
let mut res = (self.code(), json.to_owned()).into_response();
res.headers_mut().insert(
@@ -40,6 +44,7 @@ impl ResponseCode {
res
}
+ #[instrument()]
pub fn html(self, json: &str) -> Response {
let mut res = (self.code(), json.to_owned()).into_response();
res.headers_mut().insert(
@@ -48,6 +53,7 @@ impl ResponseCode {
res
}
+ #[instrument()]
pub async fn file(self, path: &str) -> Result<Response> {
if path.chars().position(|c| c == '.' ).is_none() {
return Err(ResponseCode::BadRequest.text("Folders cannot be served"));
@@ -55,7 +61,8 @@ impl ResponseCode {
let path = format!("public{}", path);
let svc = ServeFile::new(path);
let Ok(mut res) = svc.oneshot(Request::new(Body::empty())).await else {
- return Err(ResponseCode::InternalServerError.text("Error wile fetching file"));
+ tracing::error!("Error while fetching file");
+ return Err(ResponseCode::InternalServerError.text("Error while fetching file"));
};
if res.status() != StatusCode::OK {
return Err(ResponseCode::NotFound.text("File not found"));