2023-10-20 12:18:44 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use actix_web::http::header::{CACHE_CONTROL, HeaderValue};
|
2023-10-23 09:58:36 +02:00
|
|
|
use actix_web::{HttpRequest, HttpResponse, web};
|
2023-10-25 09:57:44 +02:00
|
|
|
use crate::api::api_model::{AppState, UserApiRequest};
|
|
|
|
|
use crate::model::api_proxy::{UserCredentials};
|
2023-10-23 09:58:36 +02:00
|
|
|
use crate::model::config::ConfigTarget;
|
2023-10-20 12:18:44 +02:00
|
|
|
|
2023-10-23 09:58:36 +02:00
|
|
|
pub(crate) async fn serve_file(file_path: &PathBuf, req: &HttpRequest) -> HttpResponse {
|
2023-10-20 12:18:44 +02:00
|
|
|
if file_path.exists() {
|
|
|
|
|
let file = actix_files::NamedFile::open_async(file_path).await.unwrap()
|
|
|
|
|
.set_content_type(mime::APPLICATION_JSON)
|
|
|
|
|
.disable_content_disposition();
|
|
|
|
|
let mut result = file.into_response(req);
|
|
|
|
|
let headers = result.headers_mut();
|
|
|
|
|
headers.insert(CACHE_CONTROL, HeaderValue::from_bytes("no-cache".as_bytes()).unwrap());
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
HttpResponse::NoContent().finish()
|
|
|
|
|
}
|
2023-10-23 09:58:36 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-25 09:57:44 +02:00
|
|
|
pub(crate) fn get_user_target<'a>(api_req: &'a web::Query<UserApiRequest>, app_state: &'a web::Data<AppState>) -> Option<(&'a UserCredentials, &'a ConfigTarget)> {
|
2023-10-25 21:07:38 +02:00
|
|
|
let username = api_req.username.as_str().trim();
|
|
|
|
|
let password = api_req.password.as_str().trim();
|
2023-10-23 09:58:36 +02:00
|
|
|
if !username.is_empty() && !password.is_empty() {
|
|
|
|
|
app_state.config.get_target_for_user(username, password)
|
|
|
|
|
} else {
|
2023-10-25 21:07:38 +02:00
|
|
|
let token = api_req.token.as_str().trim();
|
2023-10-23 09:58:36 +02:00
|
|
|
if !token.is_empty() {
|
|
|
|
|
app_state.config.get_target_for_user_by_token(token)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-20 12:18:44 +02:00
|
|
|
}
|