From 3c3ca2b87ac9d03a6fc72b59d2fbcb1b2c57331f Mon Sep 17 00:00:00 2001 From: blek Date: Sat, 9 Dec 2023 22:57:41 +1000 Subject: [PATCH] move out the api check code --- filed/src/web/api/files.rs | 18 ++++++++++++++++++ filed/src/web/api/files/get_all.rs | 15 ++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/filed/src/web/api/files.rs b/filed/src/web/api/files.rs index 2cc462a..d2d3873 100644 --- a/filed/src/web/api/files.rs +++ b/filed/src/web/api/files.rs @@ -1 +1,19 @@ +use warp::{reply::{WithStatus, Json, json}, http::StatusCode}; + +use crate::web::state::SharedState; + +use super::types::{ErrorMessage, Error}; + +fn check_api_enabled(state: &SharedState) -> Result<(), WithStatus> { + if ! state.config.api.enabled { + return Err( + warp::reply::with_status( + json(&ErrorMessage::new(Error::APIDisabled)), + StatusCode::SERVICE_UNAVAILABLE + ) + ) + } + Ok(()) +} + pub mod get_all; \ No newline at end of file diff --git a/filed/src/web/api/files/get_all.rs b/filed/src/web/api/files/get_all.rs index 8af8395..212762a 100644 --- a/filed/src/web/api/files/get_all.rs +++ b/filed/src/web/api/files/get_all.rs @@ -1,19 +1,12 @@ -use warp::{reply::{Reply, json}, reject::Rejection, Filter, http::StatusCode}; +use warp::{reply::{Reply, json}, reject::Rejection, Filter}; use crate::web::{state::SharedState, rejection::HttpReject}; -use super::super::types::{ErrorMessage, Error}; +use super::check_api_enabled; pub async fn get_all(state: SharedState) -> Result, Rejection> { - if ! state.config.api.enabled { - return Ok( - Box::new( - warp::reply::with_status( - json(&ErrorMessage::new(Error::APIDisabled)), - StatusCode::SERVICE_UNAVAILABLE - ) - ) - ) + if let Err(res) = check_api_enabled(&state) { + return Ok(Box::new(res)); } Ok(