VirtWeb/virtweb_backend/src/controllers/server_controller.rs
2023-10-26 11:43:05 +02:00

84 lines
2.4 KiB
Rust

use crate::app_config::AppConfig;
use crate::constants;
use crate::constants::{DISK_NAME_MAX_LEN, DISK_NAME_MIN_LEN, DISK_SIZE_MAX, DISK_SIZE_MIN};
use crate::controllers::{HttpResult, LibVirtReq};
use crate::extractors::local_auth_extractor::LocalAuthEnabled;
use crate::libvirt_rest_structures::HypervisorInfo;
use actix_web::{HttpResponse, Responder};
use sysinfo::{System, SystemExt};
pub async fn root_index() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[derive(serde::Serialize)]
struct StaticConfig {
auth_disabled: bool,
local_auth_enabled: bool,
oidc_auth_enabled: bool,
iso_mimetypes: &'static [&'static str],
constraints: ServerConstraints,
}
#[derive(serde::Serialize)]
struct LenConstraints {
min: usize,
max: usize,
}
#[derive(serde::Serialize)]
struct ServerConstraints {
iso_max_size: usize,
name_size: LenConstraints,
title_size: LenConstraints,
memory_size: LenConstraints,
disk_name_size: LenConstraints,
disk_size: LenConstraints,
}
pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
HttpResponse::Ok().json(StaticConfig {
auth_disabled: AppConfig::get().unsecure_disable_auth,
local_auth_enabled: *local_auth,
oidc_auth_enabled: !AppConfig::get().disable_oidc,
iso_mimetypes: &constants::ALLOWED_ISO_MIME_TYPES,
constraints: ServerConstraints {
iso_max_size: constants::ISO_MAX_SIZE,
name_size: LenConstraints { min: 2, max: 50 },
title_size: LenConstraints { min: 0, max: 50 },
memory_size: LenConstraints {
min: constants::MIN_VM_MEMORY,
max: constants::MAX_VM_MEMORY,
},
disk_name_size: LenConstraints {
min: DISK_NAME_MIN_LEN,
max: DISK_NAME_MAX_LEN,
},
disk_size: LenConstraints {
min: DISK_SIZE_MIN,
max: DISK_SIZE_MAX,
},
},
})
}
#[derive(serde::Serialize)]
struct ServerInfo {
hypervisor: HypervisorInfo,
system: System,
}
pub async fn server_info(client: LibVirtReq) -> HttpResult {
let mut system = System::new();
system.refresh_disks_list();
system.refresh_components_list();
system.refresh_networks_list();
system.refresh_all();
Ok(HttpResponse::Ok().json(ServerInfo {
hypervisor: client.get_info().await?,
system,
}))
}