move out the api check code
This commit is contained in:
parent
aae05764f5
commit
3c3ca2b87a
|
@ -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;
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in New Issue