Skip to content

Commit

Permalink
Import and switch to new UI. (#952)
Browse files Browse the repository at this point in the history
This PR switches to using version 0.4.0 of the Routinator UI. It also changes
the way we import the UI into Routinator by simply including the built assets
in the contrib/ui directory and updating them if necessary.

The PR also imports the mechanism to collect assets and provide them to the
HTTP server from the now deprecated routinator-ui crate.
  • Loading branch information
partim authored May 1, 2024
1 parent 763d971 commit 5b7d385
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 379 deletions.
457 changes: 105 additions & 352 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ tokio = { version = "1.24", features = [ "io-util", "macros", "process
tokio-rustls = "0.24.1"
toml_edit = "0.20"
uuid = "1.1"
routinator-ui = { version = "0.3.4", optional = true }


[target.'cfg(unix)'.dependencies]
Expand All @@ -57,7 +56,7 @@ native-tls = [ "reqwest/native-tls", "tls" ]
rta = []
socks = [ "reqwest/socks" ]
tls = []
ui = [ "routinator-ui" ]
ui = []

[profile.release]
panic = "abort"
Expand Down
134 changes: 134 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//! Build script.
//!
//! This script collects the assets for serving the Routinator UI and creates
//! a module for them in `$OUT_DIR/ui_assets.rs`.
//!
//! If built without the ui feature, does nothing.
use std::{env, fs, io, process};
use std::path::{PathBuf, Path};


const UI_DIR: &str = "contrib/ui";
const RS_FILE: &str = "ui_assets.rs";

const TYPES: &[(&str, &str)] = &[
("css", "text/css"),
("html", "text/html"),
("js", "text/javascript"),
("svg", "image/svg+xml")
];

struct Asset {
path: PathBuf,
media_type: &'static str,
content: Vec<u8>,
}

#[derive(Default)]
struct Assets(Vec<Asset>);

impl Assets {
fn load_dir(&mut self, path: PathBuf) -> Result<(), String> {
let dir = fs::read_dir(&path).map_err(|err| {
format!("Failed to open directory {}: {}", path.display(), err)
})?;
for entry in dir {
let entry = entry.map_err(|err| {
format!("Failed to read directory {}: {}", path.display(), err)
})?;
let path = entry.path();
if path.is_dir() {
self.load_dir(path)?;
}
else {
let path_ext = match path.extension().and_then(|s| s.to_str()) {
Some(ext) => ext,
None => continue,
};
for (type_ext, media_type) in TYPES {
if path_ext == *type_ext {
self.0.push(Asset {
path: path.strip_prefix(UI_DIR).map_err(|_| {
format!("Asset path {} not under {}",
path.display(), UI_DIR
)
})?.into(),
media_type,
content: fs::read(&path).map_err(|err| {
format!(
"Failed to read UI asset file {}: {}.",
path.display(), err
)
})?
})
}
}
}
}
Ok(())
}

fn write_mod(self, dest: &mut impl io::Write) -> Result<(), io::Error> {
dest.write_all(
r#"
pub struct Asset {
pub path: &'static str,
pub media_type: &'static str,
pub content: &'static [u8],
}
pub static ASSETS: &[Asset] = &[
"#.as_ref()
)?;
for item in self.0 {
writeln!(dest,
"
Asset {{
path: r#\"{}\"#,
media_type: \"{}\",
content: &{:?},
}},
",
item.path.display(),
item.media_type,
item.content.as_slice(),
)?;
}
writeln!(dest, "];")
}
}


fn main() {
if env::var_os("CARGO_FEATURE_UI").is_none() {
return
}

let out_dir = env::var_os("OUT_DIR").unwrap_or_default();
let target_path = Path::new(&out_dir).join(RS_FILE);
let mut target = match fs::File::create(&target_path) {
Ok(target) => io::BufWriter::new(target),
Err(err) => {
eprintln!("Failed to open assets module file {}: {}",
target_path.display(), err
);
process::exit(1);
}
};

let mut assets = Assets::default();
if let Err(err) = assets.load_dir(UI_DIR.into()) {
eprintln!("{}", err);
process::exit(1);
}

if let Err(err) = assets.write_mod(&mut target) {
eprintln!("Failed to write to assets module file {}: {}",
target_path.display(), err
);
process::exit(1)
}

println!("cargo:rerun-if-changed={}", UI_DIR);
}

17 changes: 17 additions & 0 deletions contrib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This directory contains data from external projects imported into this
repository for convenience.

Currently, these are:


# `ui` – Routinator UI

Project: https://github.com/NLnetLabs/routinator-ui

Included version: 0.4.0

Import with:

* `yarn build --base /ui`
* `cp -r public/* $ROUTINATOR/contrib/ui`

1 change: 1 addition & 0 deletions contrib/ui/assets/afrinic-CpNTeCk2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contrib/ui/assets/favicon-BJPY1B5p.ico
Binary file not shown.
Loading

0 comments on commit 5b7d385

Please sign in to comment.