VirtWeb/virtweb_backend/src/controllers/server_controller.rs

84 lines
2.4 KiB
Rust
Raw Normal View History

2023-09-02 08:28:08 +00:00
use crate::app_config::AppConfig;
2023-09-05 11:19:25 +00:00
use crate::constants;
2023-10-26 09:43:05 +00:00
use crate::constants::{DISK_NAME_MAX_LEN, DISK_NAME_MIN_LEN, DISK_SIZE_MAX, DISK_SIZE_MIN};
2023-09-06 16:54:38 +00:00
use crate::controllers::{HttpResult, LibVirtReq};
2023-09-02 08:28:08 +00:00
use crate::extractors::local_auth_extractor::LocalAuthEnabled;
2023-10-04 09:18:50 +00:00
use crate::libvirt_rest_structures::HypervisorInfo;
2023-09-02 06:07:06 +00:00
use actix_web::{HttpResponse, Responder};
2023-09-08 07:45:41 +00:00
use sysinfo::{System, SystemExt};
2023-09-02 06:07:06 +00:00
pub async fn root_index() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
2023-09-02 08:28:08 +00:00
#[derive(serde::Serialize)]
struct StaticConfig {
2023-09-08 07:04:48 +00:00
auth_disabled: bool,
2023-09-02 08:28:08 +00:00
local_auth_enabled: bool,
oidc_auth_enabled: bool,
2023-09-05 11:19:25 +00:00
iso_mimetypes: &'static [&'static str],
2023-10-16 17:00:15 +00:00
constraints: ServerConstraints,
}
#[derive(serde::Serialize)]
struct LenConstraints {
min: usize,
max: usize,
}
#[derive(serde::Serialize)]
struct ServerConstraints {
2023-09-05 11:19:25 +00:00
iso_max_size: usize,
2023-10-16 17:00:15 +00:00
name_size: LenConstraints,
title_size: LenConstraints,
memory_size: LenConstraints,
2023-10-26 09:43:05 +00:00
disk_name_size: LenConstraints,
disk_size: LenConstraints,
2023-09-02 08:28:08 +00:00
}
pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
HttpResponse::Ok().json(StaticConfig {
2023-09-08 07:04:48 +00:00
auth_disabled: AppConfig::get().unsecure_disable_auth,
2023-09-02 08:28:08 +00:00
local_auth_enabled: *local_auth,
oidc_auth_enabled: !AppConfig::get().disable_oidc,
2023-09-05 11:19:25 +00:00
iso_mimetypes: &constants::ALLOWED_ISO_MIME_TYPES,
2023-10-16 17:00:15 +00:00
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,
},
2023-10-26 09:43:05 +00:00
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,
},
2023-10-16 17:00:15 +00:00
},
2023-09-02 08:28:08 +00:00
})
}
2023-09-06 16:54:38 +00:00
#[derive(serde::Serialize)]
struct ServerInfo {
hypervisor: HypervisorInfo,
2023-09-08 07:45:41 +00:00
system: System,
2023-09-06 16:54:38 +00:00
}
pub async fn server_info(client: LibVirtReq) -> HttpResult {
2023-09-08 07:45:41 +00:00
let mut system = System::new();
system.refresh_disks_list();
system.refresh_components_list();
2023-09-08 12:00:39 +00:00
system.refresh_networks_list();
2023-09-08 07:45:41 +00:00
system.refresh_all();
2023-09-06 16:54:38 +00:00
Ok(HttpResponse::Ok().json(ServerInfo {
hypervisor: client.get_info().await?,
2023-09-08 07:45:41 +00:00
system,
2023-09-06 16:54:38 +00:00
}))
}