move out the api check code

This commit is contained in:
blek 2023-12-09 22:57:41 +10:00 committed by blek! Git
parent ea15e25fe2
commit e0577a66ae
2 changed files with 22 additions and 11 deletions

View File

@ -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<Json>> {
if ! state.config.api.enabled {
return Err(
warp::reply::with_status(
json(&ErrorMessage::new(Error::APIDisabled)),
StatusCode::SERVICE_UNAVAILABLE
)
)
}
Ok(())
}
pub mod get_all; pub mod get_all;

View File

@ -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 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<Box<dyn Reply>, Rejection> { pub async fn get_all(state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
if ! state.config.api.enabled { if let Err(res) = check_api_enabled(&state) {
return Ok( return Ok(Box::new(res));
Box::new(
warp::reply::with_status(
json(&ErrorMessage::new(Error::APIDisabled)),
StatusCode::SERVICE_UNAVAILABLE
)
)
)
} }
Ok( Ok(