respect the config.api.METHOD config values

This commit is contained in:
blek 2023-12-09 23:02:23 +10:00 committed by blek! Git
parent e0577a66ae
commit a7ce3bbc21
3 changed files with 16 additions and 3 deletions

View File

@ -16,4 +16,11 @@ fn check_api_enabled(state: &SharedState) -> Result<(), WithStatus<Json>> {
Ok(()) Ok(())
} }
fn function_disabled_err() -> WithStatus<Json> {
warp::reply::with_status(
json(&ErrorMessage::new(Error::APIFunctionDisabled)),
StatusCode::SERVICE_UNAVAILABLE
)
}
pub mod get_all; pub mod get_all;

View File

@ -2,11 +2,15 @@ use warp::{reply::{Reply, json}, reject::Rejection, Filter};
use crate::web::{state::SharedState, rejection::HttpReject}; use crate::web::{state::SharedState, rejection::HttpReject};
use super::check_api_enabled; use super::{check_api_enabled, function_disabled_err};
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 let Err(res) = check_api_enabled(&state) { if let Err(res) = check_api_enabled(&state) {
return Ok(Box::new(res)); return Ok(Box::new(res))
}
if ! state.config.api.get_all {
return Ok(Box::new(function_disabled_err()))
} }
Ok( Ok(

View File

@ -3,6 +3,7 @@ use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Error { pub enum Error {
APIDisabled, APIDisabled,
APIFunctionDisabled
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -15,7 +16,8 @@ impl ErrorMessage {
pub fn new(error: Error) -> ErrorMessage { pub fn new(error: Error) -> ErrorMessage {
ErrorMessage { ErrorMessage {
details: match error { details: match error {
Error::APIDisabled => Some("API is disabled by the administrator. Please contact them for further details".into()) Error::APIDisabled => Some("API is disabled by the administrator. Please contact them for further details".into()),
Error::APIFunctionDisabled => Some("This API function is disabled by the administrator. Please contact them for further details.".into())
}, },
error, error,
} }