serve frontend by backend if feature flag is set

This commit is contained in:
lukas-heiligenbrunner 2022-12-08 13:06:19 +01:00
parent b77b34442b
commit 21520a7b1c
5 changed files with 177 additions and 13 deletions

View File

@ -1,17 +1,39 @@
stages:
- build
- build_frontend
- build_backend
flutter-web:
stage: build_frontend
image: cirrusci/flutter
script:
- flutter build web
artifacts:
expire_in: 2 days
paths:
- "./app/build/web/"
rust-latest:
stage: build
stage: build_backend
image: rust:latest
script:
- mkdir ./lib/webroot
- cp -r ./app/build/web/* ./lib/webroot
- cargo build -r --manifest-path=lib/Cargo.toml --features static
- mv ./lib/target/release/raid_manager ./lib/target/release/raid_manager_static
- cargo build -r --manifest-path=lib/Cargo.toml
- cargo test -r --manifest-path=lib/Cargo.toml
artifacts:
expire_in: 2 days
paths:
- "./lib/target/release/raid_manager_static"
- "./lib/target/release/raid_manager"
rust-nightly:
stage: build
stage: build_backend
image: rustlang/rust:nightly
script:
- cargo build -r --manifest-path=lib/Cargo.toml
- cargo build -r --manifest-path=lib/Cargo.toml --features static
- cargo test -r --manifest-path=lib/Cargo.toml
allow_failure: true

84
lib/Cargo.lock generated
View File

@ -519,6 +519,25 @@ dependencies = [
"want",
]
[[package]]
name = "include_dir"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e"
dependencies = [
"include_dir_macros",
]
[[package]]
name = "include_dir_macros"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "indexmap"
version = "1.9.2"
@ -814,9 +833,11 @@ dependencies = [
name = "raid_manager"
version = "0.1.0"
dependencies = [
"include_dir",
"lazy_static",
"regex",
"rocket",
"rust-embed",
]
[[package]]
@ -995,6 +1016,40 @@ dependencies = [
"uncased",
]
[[package]]
name = "rust-embed"
version = "6.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "283ffe2f866869428c92e0d61c2f35dfb4355293cdfdc48f49e895c15f1333d1"
dependencies = [
"rust-embed-impl",
"rust-embed-utils",
"walkdir",
]
[[package]]
name = "rust-embed-impl"
version = "6.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31ab23d42d71fb9be1b643fe6765d292c5e14d46912d13f3ae2815ca048ea04d"
dependencies = [
"proc-macro2",
"quote",
"rust-embed-utils",
"syn",
"walkdir",
]
[[package]]
name = "rust-embed-utils"
version = "7.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1669d81dfabd1b5f8e2856b8bbe146c6192b0ba22162edc738ac0a5de18f054"
dependencies = [
"sha2",
"walkdir",
]
[[package]]
name = "rustversion"
version = "1.0.9"
@ -1007,6 +1062,15 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
@ -1392,6 +1456,17 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.0"
@ -1424,6 +1499,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -3,9 +3,12 @@ name = "raid_manager"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = {version = "0.5.0-rc.2", features = ["json"]}
regex = "1"
lazy_static = "1.4.0"
include_dir = "0.7.3"
rust-embed = "6.4.2"
[features]
static = []

51
lib/src/embed/mod.rs Normal file
View File

@ -0,0 +1,51 @@
use std::io::Cursor;
use rust_embed::RustEmbed;
use rocket::{Data, Request, Response, Route};
use rocket::http::{ContentType, Method};
use rocket::http::uri::{Segments};
use rocket::http::uri::fmt::Path;
use rocket::route::{Handler, Outcome};
#[cfg(feature = "static")]
#[derive(RustEmbed)]
#[folder = "webroot/"]
struct Asset;
#[cfg(feature = "static")]
#[derive(Clone)]
pub struct CustomHandler {
}
#[cfg(feature = "static")]
impl Into<Vec<Route>> for CustomHandler {
fn into(self) -> Vec<Route> {
vec![
Route::ranked(-2, Method::Get, "/<path..>", self),
]
}
}
#[cfg(feature = "static")]
#[rocket::async_trait]
impl Handler for CustomHandler {
async fn handle<'r>(&self, request: &'r Request<'_>, _: Data<'r>) -> Outcome<'r> {
let path = request.segments::<Segments<'_, Path>>(0..).ok()
.and_then(|segments| segments.to_path_buf(true).ok()).unwrap();
let path = if path.is_dir() || path.to_str() == Some("") {
path.join("index.html")
} else {
path
};
let file_content =
<Asset as RustEmbed>::get(path.to_string_lossy().as_ref()).unwrap();
let content_type: ContentType = path
.extension()
.map(|x| x.to_string_lossy())
.and_then(|x| ContentType::from_extension(&x))
.unwrap_or(ContentType::Plain);
let rsp = Response::build().header(content_type).sized_body(file_content.data.len(), Cursor::new(file_content.data)).finalize();
Outcome::Success(rsp)
}
}

View File

@ -1,9 +1,13 @@
mod parser;
#[macro_use]
extern crate rocket;
mod parser;
#[cfg(feature = "static")]
mod embed;
use rocket::serde::json::Json;
#[cfg(feature = "static")]
use crate::embed::CustomHandler;
use crate::parser::lsblk_parser::{Disk, parse_lsblk};
use crate::parser::mdstat_parser::{MdRaidSystem, parse_mdstat};
@ -20,10 +24,10 @@ fn get_disks() -> Json<Vec<Disk>> {
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
println!("init");
let _rocket = rocket::build()
.mount("/api", routes![get_raid_devices, get_disks])
.launch()
.await?;
let b = rocket::build();
let b = b.mount("/api", routes![get_raid_devices, get_disks]);
#[cfg(feature = "static")]
let b = b.mount("/", CustomHandler{});
let _rocket = b.launch().await?;
Ok(())
}