Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop relying on npm to have the UI static files available in the server #312

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ target

/flamegraph*.svg
/perf.data*

/static
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ trustify-module-importer = { path = "modules/importer" }
trustify-module-ingestor = { path = "modules/ingestor" }
trustify-module-search = { path = "modules/search" }
trustify-module-storage = { path = "modules/storage" }
trustify-module-ui = { path = "modules/ui", default-features = false }
trustify-module-ui = { path = "modules/ui" }
trustify-module-advisory = { path = "modules/advisory" }
trustify-module-organization = { path = "modules/organization" }
trustify-module-vulnerability = { path = "modules/vulnerability" }
trustify-server = { path = "server", default-features = false }
trustify-ui = { git = "https://github.com/trustification/trustify-ui.git" }
trustify-ui = { git = "https://github.com/carlosthe19916/trustify-ui", branch = "static-ui" }

[patch.crates-io]
#csaf-walker = { git = "https://github.com/ctron/csaf-walker", rev = "7b6e64dd56e4be79e184b053ef754a42e1496fe0" }
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ cargo run --bin trustd --no-default-features

That will create its own database on your local filesystem.

* The first command also contains **UI** so point your browser at <http://localhost:8080>.
* The second runs without **UI** then you can go at <http://localhost:8080/swagger-ui/> to view the API.
* You can go at <http://localhost:8080> to view the UI.
* You can go at <http://localhost:8080/swagger-ui/> to view the API.

### Running server with the latest version of the UI

- The server renders the UI from the `./static-local` folder
- If the `./static` directory exists then it will be used for serving the UI

Copy the UI static files executing:

```shell
podman cp $(docker create --name ui-download ghcr.io/trustification/trustify-ui:latest):/opt/app-root/dist/client/dist static && podman rm ui-download
```

### Running containerized UI

Expand Down
13 changes: 9 additions & 4 deletions modules/ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ edition = "2021"
actix-web = { workspace = true }
log = { workspace = true }
actix-web-static-files = { workspace = true }
trustify-ui = { workspace = true, optional = true }
trustify-ui = { workspace = true }

[features]
default = [ "ui" ]
ui = ["trustify-ui"]
static-files = "0.2.1"
serde_json = "1.0.117"
tera = "1.19.1"
base64 = "0.22.1"
serde = { version = "1.0.201", features = ["derive"] }

[build-dependencies]
static-files = "0.2.1"
13 changes: 13 additions & 0 deletions modules/ui/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use static_files::resource_dir;
use std::path::Path;

static STATIC_DIR: &str = "../../static";
static STATIC_LOCAL_DIR: &str = "../../static-local";

pub fn main() {
if Path::new(STATIC_DIR).exists() {
resource_dir(STATIC_DIR).build().unwrap();
} else {
resource_dir(STATIC_LOCAL_DIR).build().unwrap();
}
}
5 changes: 3 additions & 2 deletions modules/ui/src/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::ui::ui_resources;
use actix_web::web;
use actix_web_static_files::ResourceFiles;
use trustify_ui::{trustify_ui, UI};
use trustify_ui::UI;

pub fn configure(config: &mut web::ServiceConfig, ui: &UI) {
config.service(ResourceFiles::new("/", trustify_ui(ui)).resolve_not_found_to(""));
config.service(ResourceFiles::new("/", ui_resources(ui)).resolve_not_found_to(""));
}
5 changes: 1 addition & 4 deletions modules/ui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
#[cfg(feature = "ui")]
pub use trustify_ui::UI;

#[cfg(feature = "ui")]
pub mod endpoints;
pub mod ui;
11 changes: 11 additions & 0 deletions modules/ui/src/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::collections::HashMap;

use static_files::Resource;
use trustify_ui::{trustify_ui, UI};

include!(concat!(env!("OUT_DIR"), "/generated.rs"));

pub fn ui_resources(ui: &UI) -> HashMap<&'static str, Resource> {
let resources = generate();
trustify_ui(ui, resources)
}
5 changes: 1 addition & 4 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ url = "2"
utoipa = { workspace = true, features = ["actix_extras"] }
utoipa-swagger-ui = { workspace = true, features = ["actix-web"] }
walker-common = { workspace = true }
trustify-ui = { workspace = true }

[dev-dependencies]
urlencoding = { workspace = true }

[features]
default = [ "ui" ]
ui = ["trustify-module-ui/ui"]
8 changes: 1 addition & 7 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ use trustify_module_importer::server::importer;
use trustify_module_ingestor::graph::Graph;
use trustify_module_storage::service::dispatch::DispatchBackend;
use trustify_module_storage::service::fs::FileSystemBackend;
use trustify_ui::UI;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

#[cfg(feature = "ui")]
use trustify_module_ui::UI;

/// Run the API server
#[derive(clap::Args, Debug)]
pub struct Run {
Expand Down Expand Up @@ -82,7 +80,6 @@ struct InitData {
http: HttpServerConfig<Trustify>,
tracing: Tracing,
swagger_oidc: Option<Arc<SwaggerUiOidc>>,
#[cfg(feature = "ui")]
ui: UI,
}

Expand Down Expand Up @@ -152,7 +149,6 @@ impl InitData {

let storage = DispatchBackend::Filesystem(FileSystemBackend::new(storage).await?);

#[cfg(feature = "ui")]
let ui = UI {
// TODO: where/how should we configure these details?
version: env!("CARGO_PKG_VERSION").to_string(),
Expand All @@ -173,7 +169,6 @@ impl InitData {
tracing: run.infra.tracing,
swagger_oidc,
storage,
#[cfg(feature = "ui")]
ui,
})
}
Expand Down Expand Up @@ -212,7 +207,6 @@ impl InitData {
trustify_module_storage::endpoints::configure(svc, storage.clone());
// I think the UI must come last due to
// its use of `resolve_not_found_to`
#[cfg(feature = "ui")]
trustify_module_ui::endpoints::configure(svc, &self.ui);
});
})
Expand Down
21 changes: 21 additions & 0 deletions static-local/branding/strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"application": {
"title": "Trustification",
"name": "Trustification UI",
"description": "Trustification UI"
},
"about": {
"displayName": "Trustification",
"imageSrc": "<%= brandingRoot %>/images/masthead-logo.svg",
"documentationUrl": "https://trustification.io/"
},
"masthead": {
"leftBrand": {
"src": "<%= brandingRoot %>/images/masthead-logo.svg",
"alt": "brand",
"height": "40px"
},
"leftTitle": null,
"rightBrand": null
}
}
8 changes: 8 additions & 0 deletions static-local/index.html.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Trustify</title>
<body>
<h1>Welcome</h1>
</body>
</html>
4 changes: 0 additions & 4 deletions trustd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
url = { workspace = true }

[features]
default = ["ui"]
ui = ["trustify-server/ui"]
Loading