2025-07-18 17:52:58 +03:00
|
|
|
use actix_web::{web, HttpResponse, Result};
|
|
|
|
use log::info;
|
2025-07-18 18:06:26 +03:00
|
|
|
use rust_embed::RustEmbed;
|
2025-07-18 17:52:58 +03:00
|
|
|
use serde_json::json;
|
|
|
|
use tokio_postgres::Client;
|
|
|
|
|
|
|
|
use crate::server::{get_keys_from_db, Flows};
|
|
|
|
|
2025-07-18 18:06:26 +03:00
|
|
|
#[derive(RustEmbed)]
|
|
|
|
#[folder = "static/"]
|
|
|
|
struct StaticAssets;
|
|
|
|
|
2025-07-18 17:52:58 +03:00
|
|
|
// API endpoint to get list of available flows
|
|
|
|
pub async fn get_flows_api(allowed_flows: web::Data<Vec<String>>) -> Result<HttpResponse> {
|
|
|
|
info!("API request for available flows");
|
|
|
|
Ok(HttpResponse::Ok().json(&**allowed_flows))
|
|
|
|
}
|
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
// API endpoint to deprecate a specific key by server name
|
2025-07-18 17:52:58 +03:00
|
|
|
pub async fn delete_key_by_server(
|
|
|
|
flows: web::Data<Flows>,
|
2025-07-18 18:06:26 +03:00
|
|
|
path: web::Path<(String, String)>,
|
2025-07-18 17:52:58 +03:00
|
|
|
db_client: web::Data<std::sync::Arc<Client>>,
|
|
|
|
allowed_flows: web::Data<Vec<String>>,
|
|
|
|
) -> Result<HttpResponse> {
|
2025-07-18 18:06:26 +03:00
|
|
|
let (flow_id_str, server_name) = path.into_inner();
|
2025-07-18 17:52:58 +03:00
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
info!("API request to deprecate key for server '{}' in flow '{}'", server_name, flow_id_str);
|
2025-07-18 17:52:58 +03:00
|
|
|
|
|
|
|
if !allowed_flows.contains(&flow_id_str) {
|
|
|
|
return Ok(HttpResponse::Forbidden().json(json!({
|
|
|
|
"error": "Flow ID not allowed"
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
// Deprecate in database
|
|
|
|
match crate::db::deprecate_key_by_server(&db_client, &server_name, &flow_id_str).await {
|
|
|
|
Ok(deprecated_count) => {
|
|
|
|
if deprecated_count > 0 {
|
|
|
|
info!("Deprecated {} key(s) for server '{}' in flow '{}'", deprecated_count, server_name, flow_id_str);
|
2025-07-18 17:52:58 +03:00
|
|
|
|
|
|
|
// Refresh the in-memory flows
|
|
|
|
let updated_flows = match get_keys_from_db(&db_client).await {
|
|
|
|
Ok(flows) => flows,
|
|
|
|
Err(e) => {
|
|
|
|
return Ok(HttpResponse::InternalServerError().json(json!({
|
|
|
|
"error": format!("Failed to refresh flows: {}", e)
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut flows_guard = flows.lock().unwrap();
|
|
|
|
*flows_guard = updated_flows;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(json!({
|
2025-07-18 18:35:04 +03:00
|
|
|
"message": format!("Successfully deprecated {} key(s) for server '{}'", deprecated_count, server_name),
|
|
|
|
"deprecated_count": deprecated_count
|
2025-07-18 17:52:58 +03:00
|
|
|
})))
|
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::NotFound().json(json!({
|
|
|
|
"error": format!("No keys found for server '{}'", server_name)
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Ok(HttpResponse::InternalServerError().json(json!({
|
2025-07-18 18:35:04 +03:00
|
|
|
"error": format!("Failed to deprecate key: {}", e)
|
2025-07-18 17:52:58 +03:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
// API endpoint to restore a deprecated key
|
|
|
|
pub async fn restore_key_by_server(
|
|
|
|
flows: web::Data<Flows>,
|
|
|
|
path: web::Path<(String, String)>,
|
|
|
|
db_client: web::Data<std::sync::Arc<Client>>,
|
|
|
|
allowed_flows: web::Data<Vec<String>>,
|
|
|
|
) -> Result<HttpResponse> {
|
|
|
|
let (flow_id_str, server_name) = path.into_inner();
|
2025-07-18 17:52:58 +03:00
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
info!("API request to restore key for server '{}' in flow '{}'", server_name, flow_id_str);
|
|
|
|
|
|
|
|
if !allowed_flows.contains(&flow_id_str) {
|
|
|
|
return Ok(HttpResponse::Forbidden().json(json!({
|
|
|
|
"error": "Flow ID not allowed"
|
|
|
|
})));
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
// Restore in database
|
|
|
|
match crate::db::restore_key_by_server(&db_client, &server_name, &flow_id_str).await {
|
|
|
|
Ok(restored_count) => {
|
|
|
|
if restored_count > 0 {
|
|
|
|
info!("Restored {} key(s) for server '{}' in flow '{}'", restored_count, server_name, flow_id_str);
|
|
|
|
|
|
|
|
// Refresh the in-memory flows
|
|
|
|
let updated_flows = match get_keys_from_db(&db_client).await {
|
|
|
|
Ok(flows) => flows,
|
|
|
|
Err(e) => {
|
|
|
|
return Ok(HttpResponse::InternalServerError().json(json!({
|
|
|
|
"error": format!("Failed to refresh flows: {}", e)
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut flows_guard = flows.lock().unwrap();
|
|
|
|
*flows_guard = updated_flows;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(json!({
|
|
|
|
"message": format!("Successfully restored {} key(s) for server '{}'", restored_count, server_name),
|
|
|
|
"restored_count": restored_count
|
|
|
|
})))
|
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::NotFound().json(json!({
|
|
|
|
"error": format!("No deprecated keys found for server '{}'", server_name)
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Ok(HttpResponse::InternalServerError().json(json!({
|
|
|
|
"error": format!("Failed to restore key: {}", e)
|
|
|
|
})))
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
|
|
|
}
|
2025-07-18 18:35:04 +03:00
|
|
|
}
|
2025-07-18 17:52:58 +03:00
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
// API endpoint to permanently delete a key
|
|
|
|
pub async fn permanently_delete_key_by_server(
|
|
|
|
flows: web::Data<Flows>,
|
|
|
|
path: web::Path<(String, String)>,
|
|
|
|
db_client: web::Data<std::sync::Arc<Client>>,
|
|
|
|
allowed_flows: web::Data<Vec<String>>,
|
|
|
|
) -> Result<HttpResponse> {
|
|
|
|
let (flow_id_str, server_name) = path.into_inner();
|
|
|
|
|
|
|
|
info!("API request to permanently delete key for server '{}' in flow '{}'", server_name, flow_id_str);
|
|
|
|
|
|
|
|
if !allowed_flows.contains(&flow_id_str) {
|
|
|
|
return Ok(HttpResponse::Forbidden().json(json!({
|
|
|
|
"error": "Flow ID not allowed"
|
|
|
|
})));
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
// Permanently delete from database
|
|
|
|
match crate::db::permanently_delete_key_by_server(&db_client, &server_name, &flow_id_str).await {
|
|
|
|
Ok(deleted_count) => {
|
|
|
|
if deleted_count > 0 {
|
|
|
|
info!("Permanently deleted {} key(s) for server '{}' in flow '{}'", deleted_count, server_name, flow_id_str);
|
|
|
|
|
|
|
|
// Refresh the in-memory flows
|
|
|
|
let updated_flows = match get_keys_from_db(&db_client).await {
|
|
|
|
Ok(flows) => flows,
|
|
|
|
Err(e) => {
|
|
|
|
return Ok(HttpResponse::InternalServerError().json(json!({
|
|
|
|
"error": format!("Failed to refresh flows: {}", e)
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut flows_guard = flows.lock().unwrap();
|
|
|
|
*flows_guard = updated_flows;
|
2025-07-18 17:52:58 +03:00
|
|
|
|
2025-07-18 18:35:04 +03:00
|
|
|
Ok(HttpResponse::Ok().json(json!({
|
|
|
|
"message": format!("Successfully deleted {} key(s) for server '{}'", deleted_count, server_name),
|
|
|
|
"deleted_count": deleted_count
|
|
|
|
})))
|
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::NotFound().json(json!({
|
|
|
|
"error": format!("No keys found for server '{}'", server_name)
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Ok(HttpResponse::InternalServerError().json(json!({
|
|
|
|
"error": format!("Failed to delete key: {}", e)
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
|
|
|
|
2025-07-18 18:06:26 +03:00
|
|
|
// Serve static files from embedded assets
|
2025-07-18 17:52:58 +03:00
|
|
|
pub async fn serve_static_file(path: web::Path<String>) -> Result<HttpResponse> {
|
|
|
|
let file_path = path.into_inner();
|
|
|
|
|
2025-07-18 18:06:26 +03:00
|
|
|
match StaticAssets::get(&file_path) {
|
|
|
|
Some(content) => {
|
|
|
|
let content_type = match std::path::Path::new(&file_path)
|
|
|
|
.extension()
|
|
|
|
.and_then(|s| s.to_str())
|
|
|
|
{
|
2025-07-18 17:52:58 +03:00
|
|
|
Some("html") => "text/html; charset=utf-8",
|
|
|
|
Some("css") => "text/css; charset=utf-8",
|
|
|
|
Some("js") => "application/javascript; charset=utf-8",
|
|
|
|
Some("png") => "image/png",
|
|
|
|
Some("jpg") | Some("jpeg") => "image/jpeg",
|
|
|
|
Some("svg") => "image/svg+xml",
|
|
|
|
_ => "application/octet-stream",
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type(content_type)
|
2025-07-18 18:06:26 +03:00
|
|
|
.body(content.data.as_ref().to_vec()))
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
2025-07-18 18:06:26 +03:00
|
|
|
None => {
|
|
|
|
Ok(HttpResponse::NotFound().body(format!("File not found: {}", file_path)))
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-18 18:06:26 +03:00
|
|
|
// Serve the main web interface from embedded assets
|
2025-07-18 17:52:58 +03:00
|
|
|
pub async fn serve_web_interface() -> Result<HttpResponse> {
|
2025-07-18 18:06:26 +03:00
|
|
|
match StaticAssets::get("index.html") {
|
|
|
|
Some(content) => {
|
2025-07-18 17:52:58 +03:00
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("text/html; charset=utf-8")
|
2025-07-18 18:06:26 +03:00
|
|
|
.body(content.data.as_ref().to_vec()))
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
2025-07-18 18:06:26 +03:00
|
|
|
None => {
|
|
|
|
Ok(HttpResponse::NotFound().body("Web interface not found"))
|
2025-07-18 17:52:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|