Can generate release builds

This commit is contained in:
2023-12-12 15:45:56 +01:00
parent f651c756b6
commit e8422f4b3c
11 changed files with 143 additions and 11 deletions

View File

@ -12,10 +12,17 @@ pub struct AppConfig {
#[clap(short, long, env, default_value = "0.0.0.0:8000")]
pub listen_address: String,
/// Website origin
/// Website main origin
#[clap(short, long, env, default_value = "http://localhost:3000")]
pub website_origin: String,
/// Additional allowed website origin
///
/// Warning! These origins won't be usable for OpenID authentication,
/// only for local auth
#[clap(long, env)]
pub additional_origins: Vec<String>,
/// Proxy IP, might end with a star "*"
#[clap(short, long, env)]
pub proxy_ip: Option<String>,
@ -74,7 +81,7 @@ pub struct AppConfig {
oidc_redirect_url: String,
/// Storage directory
#[arg(long, env, default_value = "storage")]
#[arg(short, long, env, default_value = "storage")]
pub storage: String,
/// Directory where temporary files are stored

View File

@ -9,6 +9,7 @@ pub mod auth_controller;
pub mod iso_controller;
pub mod network_controller;
pub mod server_controller;
pub mod static_controller;
pub mod vm_controller;
/// Custom error to ease controller writing

View File

@ -8,10 +8,6 @@ use crate::libvirt_rest_structures::HypervisorInfo;
use actix_web::{HttpResponse, Responder};
use sysinfo::{NetworksExt, System, SystemExt};
pub async fn root_index() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[derive(serde::Serialize)]
struct StaticConfig {
auth_disabled: bool,

View File

@ -0,0 +1,45 @@
#[cfg(debug_assertions)]
pub use serve_static_debug::{root_index, serve_static_content};
#[cfg(not(debug_assertions))]
pub use serve_static_release::{root_index, serve_static_content};
#[cfg(debug_assertions)]
mod serve_static_debug {
use actix_web::{HttpResponse, Responder};
pub async fn root_index() -> impl Responder {
HttpResponse::Ok().body("Hello world! Debug=on for VirtWeb!")
}
pub async fn serve_static_content() -> impl Responder {
HttpResponse::NotFound().body("Hello world! Static assets are not served in debug mode")
}
}
#[cfg(not(debug_assertions))]
mod serve_static_release {
use actix_web::{web, HttpResponse, Responder};
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "static/"]
struct Asset;
fn handle_embedded_file(path: &str, can_fallback: bool) -> HttpResponse {
match (Asset::get(path), can_fallback) {
(Some(content), _) => HttpResponse::Ok()
.content_type(mime_guess::from_path(path).first_or_octet_stream().as_ref())
.body(content.data.into_owned()),
(None, false) => HttpResponse::NotFound().body("404 Not Found"),
(None, true) => handle_embedded_file("index.html", false),
}
}
pub async fn root_index() -> impl Responder {
handle_embedded_file("index.html", false)
}
pub async fn serve_static_content(path: web::Path<String>) -> impl Responder {
handle_embedded_file(path.as_ref(), !path.as_ref().starts_with("static/"))
}
}

View File

@ -22,7 +22,8 @@ use virtweb_backend::constants::{
MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME,
};
use virtweb_backend::controllers::{
auth_controller, iso_controller, network_controller, server_controller, vm_controller,
auth_controller, iso_controller, network_controller, server_controller, static_controller,
vm_controller,
};
use virtweb_backend::libvirt_client::LibVirtClient;
use virtweb_backend::middlewares::auth_middleware::AuthChecker;
@ -69,7 +70,7 @@ async fn main() -> std::io::Result<()> {
.login_deadline(Some(Duration::from_secs(MAX_SESSION_DURATION)))
.build();
let cors = Cors::default()
let mut cors = Cors::default()
.allowed_origin(&AppConfig::get().website_origin)
.allowed_methods(vec!["GET", "POST", "DELETE", "PUT"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
@ -77,6 +78,10 @@ async fn main() -> std::io::Result<()> {
.supports_credentials()
.max_age(3600);
for additional_origin in &AppConfig::get().additional_origins {
cors = cors.allowed_origin(additional_origin);
}
App::new()
.wrap(Logger::default())
.wrap(AuthChecker)
@ -93,7 +98,6 @@ async fn main() -> std::io::Result<()> {
.app_data(MultipartFormConfig::default().total_limit(constants::ISO_MAX_SIZE))
.app_data(TempFileConfig::default().directory(&AppConfig::get().temp_dir))
// Server controller
.route("/", web::get().to(server_controller::root_index))
.route(
"/api/server/static_config",
web::get().to(server_controller::static_config),
@ -231,6 +235,12 @@ async fn main() -> std::io::Result<()> {
"/api/network/{uid}/stop",
web::get().to(network_controller::stop),
)
// Static assets
.route("/", web::get().to(static_controller::root_index))
.route(
"/{tail:.*}",
web::get().to(static_controller::serve_static_content),
)
})
.bind(&AppConfig::get().listen_address)?
.run()

View File

@ -69,7 +69,10 @@ where
let auth_disabled = AppConfig::get().unsecure_disable_auth;
// Check authentication, if required
if !auth_disabled && !constants::ROUTES_WITHOUT_AUTH.contains(&req.path()) {
if !auth_disabled
&& !constants::ROUTES_WITHOUT_AUTH.contains(&req.path())
&& req.path().starts_with("/api/")
{
let auth = match AuthExtractor::from_request(req.request(), &mut Payload::None)
.into_inner()
{