Skip to content

Commit

Permalink
Placeholder for serving future dashboard. Can be enabled by adding fe…
Browse files Browse the repository at this point in the history
…ature dashboard to build.
  • Loading branch information
scx1332 committed Sep 20, 2024
1 parent b78a4f8 commit 12c6593
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/fast-win-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ jobs:

- name: Checkout frontend
run: |
mkdir -p dashboard
git clone https://github.com/scx1332/yagna-dashboard.git yagna-dashboard
cd yagna-dashboard
git checkout e52bb7b51d7a644acc407479332f1f4b4cda263e
Expand All @@ -67,7 +66,7 @@ jobs:

- name: Build
run: |
cargo build --target x86_64-pc-windows-gnu --profile release-fast --features static-openssl
cargo build --target x86_64-pc-windows-gnu --profile release-fast --features dashboard,static-openssl
cp target/x86_64-pc-windows-gnu/release-fast/yagna.exe yagna.exe
tar -czf yagna.tar.gz yagna.exe
Expand Down
36 changes: 36 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.16.0"

[features]
default = ['erc20-driver', 'gftp/bin']
dashboard = ['rust-embed', 'mime_guess']
dummy-driver = ['ya-dummy-driver']
erc20-driver = ['ya-erc20-driver']
static-openssl = ["openssl/vendored", "openssl-probe"]
Expand Down Expand Up @@ -82,9 +83,11 @@ lazy_static = "1.4"
libsqlite3-sys = {workspace = true}
log = "0.4"
metrics = "0.12"
mime_guess = { version = "2.0", optional = true }
num_cpus = "1"
openssl-probe = {version = "0.1", optional = true}
openssl.workspace = true
rust-embed = { version = "8.5", optional = true }
serde = "1.0"
serde_json = "1.0"
structopt = "0.3"
Expand Down
13 changes: 7 additions & 6 deletions core/serv-api/web/src/middleware/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ where
let cache = self.cache.clone();
let service = self.service.clone();

// TODO: remove this hack; possibly by enabling creation of arbitrary appkey from CLI
if req.uri().to_string().starts_with("/metrics-api")
|| req.uri().to_string().starts_with("/version")
{
log::debug!("skipping authorization for uri={}", req.uri());
return Box::pin(service.borrow_mut().call(req));
let allowed_uris = vec!["/metrics-api", "/version", "/dashboard"];

for uri in allowed_uris {
if req.uri().to_string().starts_with(uri) {
log::debug!("skipping authorization for uri={}", req.uri());
return Box::pin(service.borrow_mut().call(req));
}
}

Box::pin(async move {
Expand Down
44 changes: 43 additions & 1 deletion core/serv/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::obfuscated_if_else)]

use actix_web::{middleware, web, App, HttpServer, Responder};
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Responder};
use anyhow::{Context, Result};
use futures::prelude::*;
use metrics::{counter, gauge};
Expand Down Expand Up @@ -568,6 +568,8 @@ impl ServiceCommand {
.wrap(middleware::Logger::default())
.wrap(auth::Auth::new(cors.cache()))
.wrap(cors.cors())
.route("/dashboard", web::get().to(redirect_to_dashboard))
.route("/dashboard/{_:.*}", web::get().to(dashboard_serve))
.route("/me", web::get().to(me))
.service(forward_gsb);
let rest = Services::rest(app, &context);
Expand Down Expand Up @@ -710,6 +712,46 @@ async fn forward_gsb(
Ok::<_, actix_web::Error>(web::Json(json_resp))
}

#[cfg(feature = "dashboard")]
#[derive(rust_embed::RustEmbed)]
#[folder = "dashboard"]
struct Asset;

pub async fn redirect_to_dashboard() -> impl Responder {
#[cfg(feature = "dashboard")]
{
let target = "/dashboard/";
log::debug!("Redirecting to endpoint: {target}");
HttpResponse::Ok()
.status(actix_web::http::StatusCode::PERMANENT_REDIRECT)
.append_header((actix_web::http::header::LOCATION, target))
.finish()
}
#[cfg(not(feature = "dashboard"))]
HttpResponse::NotFound().body("404 Not Found")
}

pub async fn dashboard_serve(path: web::Path<String>) -> impl Responder {
#[cfg(feature = "dashboard")]
{
let mut path = path.as_str();
let mut content = Asset::get(path);
if content.is_none() && !path.contains('.') {
path = "index.html";
content = Asset::get(path);
}
log::debug!("Serving frontend file: {path}");
match content {
Some(content) => HttpResponse::Ok()
.content_type(mime_guess::from_path(path).first_or_octet_stream().as_ref())
.body(content.data.into_owned()),
None => HttpResponse::NotFound().body("404 Not Found"),
}
}
#[cfg(not(feature = "dashboard"))]
HttpResponse::NotFound().body(format!("404 Not Found: {}", path))
}

#[actix_rt::main]
async fn main() -> Result<()> {
dotenv::dotenv().ok();
Expand Down
11 changes: 11 additions & 0 deletions dashboard/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Yagna Dashboard</title>
</head>
<body>
<h1>Yagna Dashboard</h1>
<p>This is placeholder, place your files into ./dashboard directory to serve them from yagna</p>
</body>
</html>

0 comments on commit 12c6593

Please sign in to comment.