VirtWeb/virtweb_backend/src/controllers/mod.rs

100 lines
2.5 KiB
Rust
Raw Normal View History

2023-09-06 16:54:38 +00:00
use crate::libvirt_client::LibVirtClient;
2023-09-04 09:25:03 +00:00
use actix_web::body::BoxBody;
2023-09-06 16:54:38 +00:00
use actix_web::{web, HttpResponse};
2023-09-04 09:25:03 +00:00
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
2023-09-02 16:44:16 +00:00
pub mod auth_controller;
2023-09-05 11:19:25 +00:00
pub mod iso_controller;
2023-09-02 07:12:36 +00:00
pub mod server_controller;
2023-10-04 09:18:50 +00:00
pub mod vm_controller;
2023-09-04 09:25:03 +00:00
/// Custom error to ease controller writing
#[derive(Debug)]
2023-10-13 14:44:56 +00:00
pub enum HttpErr {
Err(anyhow::Error),
HTTPResponse(HttpResponse),
2023-09-04 09:25:03 +00:00
}
impl Display for HttpErr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2023-10-13 14:44:56 +00:00
match self {
HttpErr::Err(err) => Display::fmt(err, f),
HttpErr::HTTPResponse(res) => {
Display::fmt(&format!("HTTP RESPONSE {}", res.status().as_str()), f)
}
}
2023-09-04 09:25:03 +00:00
}
}
impl actix_web::error::ResponseError for HttpErr {
fn error_response(&self) -> HttpResponse<BoxBody> {
log::error!("Error while processing request! {}", self);
HttpResponse::InternalServerError().body("Failed to execute request!")
}
}
impl From<anyhow::Error> for HttpErr {
fn from(err: anyhow::Error) -> HttpErr {
2023-10-13 14:44:56 +00:00
HttpErr::Err(err)
2023-09-04 09:25:03 +00:00
}
}
impl From<serde_json::Error> for HttpErr {
fn from(value: serde_json::Error) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(value.into())
2023-09-04 09:25:03 +00:00
}
}
impl From<Box<dyn Error>> for HttpErr {
fn from(value: Box<dyn Error>) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
2023-09-04 09:25:03 +00:00
}
}
impl From<std::io::Error> for HttpErr {
fn from(value: std::io::Error) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(value.into())
2023-09-04 09:25:03 +00:00
}
}
impl From<std::num::ParseIntError> for HttpErr {
fn from(value: std::num::ParseIntError) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(value.into())
2023-09-04 09:25:03 +00:00
}
}
2023-09-05 11:19:25 +00:00
impl From<tempfile::PersistError> for HttpErr {
fn from(value: tempfile::PersistError) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(value.into())
2023-09-05 11:19:25 +00:00
}
}
2023-09-05 14:12:20 +00:00
impl From<reqwest::Error> for HttpErr {
fn from(value: reqwest::Error) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(value.into())
2023-09-05 14:12:20 +00:00
}
}
impl From<reqwest::header::ToStrError> for HttpErr {
fn from(value: reqwest::header::ToStrError) -> Self {
2023-10-13 14:44:56 +00:00
HttpErr::Err(value.into())
2023-09-05 14:12:20 +00:00
}
}
2023-10-18 16:00:25 +00:00
impl From<actix_web::Error> for HttpErr {
fn from(value: actix_web::Error) -> Self {
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
}
}
2023-10-13 14:44:56 +00:00
impl From<HttpResponse> for HttpErr {
fn from(value: HttpResponse) -> Self {
HttpErr::HTTPResponse(value)
}
}
2023-09-04 09:25:03 +00:00
pub type HttpResult = Result<HttpResponse, HttpErr>;
2023-09-06 16:54:38 +00:00
pub type LibVirtReq = web::Data<LibVirtClient>;