use crate::libvirt_client::LibVirtClient; use actix_http::StatusCode; use actix_web::body::BoxBody; use actix_web::{web, HttpResponse}; use std::error::Error; use std::fmt::{Display, Formatter}; use std::io::ErrorKind; pub mod api_tokens_controller; pub mod auth_controller; pub mod iso_controller; pub mod network_controller; pub mod nwfilter_controller; pub mod server_controller; pub mod static_controller; pub mod vm_controller; /// Custom error to ease controller writing #[derive(Debug)] pub enum HttpErr { Err(anyhow::Error), HTTPResponse(HttpResponse), } impl Display for HttpErr { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { HttpErr::Err(err) => Display::fmt(err, f), HttpErr::HTTPResponse(res) => { Display::fmt(&format!("HTTP RESPONSE {}", res.status().as_str()), f) } } } } impl actix_web::error::ResponseError for HttpErr { fn status_code(&self) -> StatusCode { match self { HttpErr::Err(_) => StatusCode::INTERNAL_SERVER_ERROR, HttpErr::HTTPResponse(r) => r.status(), } } fn error_response(&self) -> HttpResponse { log::error!("Error while processing request! {}", self); HttpResponse::InternalServerError().body("Failed to execute request!") } } impl From for HttpErr { fn from(err: anyhow::Error) -> HttpErr { HttpErr::Err(err) } } impl From for HttpErr { fn from(value: serde_json::Error) -> Self { HttpErr::Err(value.into()) } } impl From> for HttpErr { fn from(value: Box) -> Self { HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into()) } } impl From for HttpErr { fn from(value: std::io::Error) -> Self { HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: std::num::ParseIntError) -> Self { HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: tempfile::PersistError) -> Self { HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: reqwest::Error) -> Self { HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: reqwest::header::ToStrError) -> Self { HttpErr::Err(value.into()) } } impl From for HttpErr { fn from(value: actix_web::Error) -> Self { HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into()) } } impl From for HttpErr { fn from(value: HttpResponse) -> Self { HttpErr::HTTPResponse(value) } } pub type HttpResult = Result; pub type LibVirtReq = web::Data;