From 03295580e3c0c4d5591c25d351ca4af01cbb9fb2 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 29 May 2024 19:54:39 +0300 Subject: [PATCH] 404 handler --- src/dto.rs | 5 +++++ src/server.rs | 9 +++++++-- src/task.rs | 3 +-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/dto.rs b/src/dto.rs index 1603bc1..f5ecfe3 100644 --- a/src/dto.rs +++ b/src/dto.rs @@ -35,3 +35,8 @@ pub struct ConvertURLRequest { pub url: String, pub callback_url: Option, } + +#[derive(Serialize)] +pub(crate) struct ErrorResponse { + pub(crate) error: String +} \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index 3ed0184..8e1670d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -15,7 +15,7 @@ use tower_http::trace::TraceLayer; use tracing::{debug, error}; use uuid::Uuid; -use crate::dto::{ConvertRequest, ConvertResponse, ConvertURLRequest}; +use crate::dto::{ConvertRequest, ConvertResponse, ConvertURLRequest, ErrorResponse}; use crate::task::{Task, TaskParams}; use crate::thread_pool::ThreadPool; @@ -73,7 +73,8 @@ impl Server { .route("/enqueue_url", post(enqueue_url)) .route("/get/:identifier", get(download_file)) .with_state(this) - .layer(TraceLayer::new_for_http()); + .layer(TraceLayer::new_for_http()) + .fallback(handler_not_found); tracing::info!("listening on {addr}"); let listener = TcpListener::bind(addr).await?; @@ -220,6 +221,10 @@ async fn download_file( Ok(([(http::header::CONTENT_TYPE, mime_type)], body)) } +async fn handler_not_found() -> impl IntoResponse { + (StatusCode::NOT_FOUND, Json::from(ErrorResponse { error: "not found".to_string() })) +} + fn error_response(msg: &str) -> (StatusCode, Json) { ( StatusCode::INTERNAL_SERVER_ERROR, diff --git a/src/task.rs b/src/task.rs index 5d9f597..2e1983b 100644 --- a/src/task.rs +++ b/src/task.rs @@ -4,11 +4,10 @@ use ffmpeg_next::channel_layout::ChannelLayout; use ffmpeg_next::{format, Dictionary}; use std::error::Error; use std::fs::File; -use std::io::{self, Write}; +use std::io::Write; use std::path::Path; use tracing::{debug, error}; use ureq::Error as UreqError; -use ureq::Response; #[derive(Clone)] pub struct Task {