Skip to content

Commit

Permalink
feat(frontend): allow managing multiple nodes on one page (#66)
Browse files Browse the repository at this point in the history
Also adds a messages icon to the header
  • Loading branch information
ravenclaw900 committed Dec 29, 2021
1 parent 12db9fa commit c2b6953
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 105 deletions.
36 changes: 32 additions & 4 deletions .github/workflows/push-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ jobs:
run: yarn build
- name: Copy "dist" folder
run: cp -r src/frontend/dist/. src/backend/dist/
- name: Build amd64
- name: Build x86_64
working-directory: src/backend
run: cross build --release --target x86_64-unknown-linux-musl
- name: Build armv6
- name: Build armv6l
working-directory: src/backend
run: cross build --release --target arm-unknown-linux-musleabihf
- name: Build armv7
- name: Build armv7l
working-directory: src/backend
run: cross build --release --target armv7-unknown-linux-musleabihf
- name: Build armv8
- name: Build aarch64
working-directory: src/backend
run: cross build --release --target aarch64-unknown-linux-musl
- uses: actions/upload-artifact@v2
Expand All @@ -70,4 +70,32 @@ jobs:
with:
name: dietpi-dashboard-aarch64
path: src/backend/target/aarch64-unknown-linux-musl/release/dietpi-dashboard
- name: Build x86_64 (backend only)
working-directory: src/backend
run: cross build --release --target x86_64-unknown-linux-musl --no-default-features
- name: Build armv6l (backend only)
working-directory: src/backend
run: cross build --release --target arm-unknown-linux-musleabihf --no-default-features
- name: Build armv7l (backend only)
working-directory: src/backend
run: cross build --release --target armv7-unknown-linux-musleabihf --no-default-features
- name: Build aarch64 (backend only)
working-directory: src/backend
run: cross build --release --target aarch64-unknown-linux-musl --no-default-features
- uses: actions/upload-artifact@v2
with:
name: dietpi-dashboard-x86_64-backend
path: src/backend/target/x86_64-unknown-linux-musl/release/dietpi-dashboard
- uses: actions/upload-artifact@v2
with:
name: dietpi-dashboard-armv6l-backend
path: src/backend/target/arm-unknown-linux-musleabihf/release/dietpi-dashboard
- uses: actions/upload-artifact@v2
with:
name: dietpi-dashboard-armv7l-backend
path: src/backend/target/armv7-unknown-linux-musleabihf/release/dietpi-dashboard
- uses: actions/upload-artifact@v2
with:
name: dietpi-dashboard-aarch64-backend
path: src/backend/target/aarch64-unknown-linux-musl/release/dietpi-dashboard

7 changes: 6 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
# To get hash: "echo -n '<PASSWORD>' | sha512sum"
#hash = "SHA512 hash of password"
# To get random secret: "openssl rand -hex 32"
#secret = "64-character secret"
#secret = "64-character secret"
# Other nodes viewable on frontend page
# - Default: []
# Please insert the IP addresses/domains and ports of these nodes
# For example: ["example.com:5252", "192.168.1.60:4386"]
nodes = ["192.168.1.100:5252"]
14 changes: 12 additions & 2 deletions src/backend/Cargo.lock

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

9 changes: 7 additions & 2 deletions src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
warp = {version = "0.3.2", default-features = false, features = ["compression", "websocket", "tls"]}
warp = {version = "0.3.2", default-features = false, features = ["websocket", "tls"]}
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "sync"] }
num_cpus = "1.13.1"
simple_logger = "1.16.0"
log = "0.4.14"
include_dir = "0.7.2"
include_dir = {version = "0.7.2", optional = true}
lazy_static = "1.4.0"
futures = "0.3.19"
nanoserde = "0.1.29"
Expand All @@ -20,6 +20,11 @@ heim = { git = "https://github.com/heim-rs/heim", features = ["cpu", "disk", "ho
infer = { version = "0.5.0", default-features = false }
jwts = "0.2.3"
sha2 = "0.10.0"
toml = "0.5.8"

[features]
default = ["frontend"]
frontend = ["include_dir", "warp/compression"]

[profile.release]
lto = "fat"
Expand Down
92 changes: 61 additions & 31 deletions src/backend/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nanoserde::{Toml, TomlParser};
use toml::Value;

pub struct Config {
pub port: u16,
Expand All @@ -10,72 +10,102 @@ pub struct Config {
pub pass: bool,
pub hash: String,
pub secret: String,

#[cfg(feature = "frontend")]
pub nodes: Vec<String>,
}

pub fn config() -> Config {
let mut cfgpath = std::env::current_exe().unwrap();
cfgpath.set_file_name("config.toml");
let cfg = TomlParser::parse(
&match std::fs::read_to_string(cfgpath) {
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
std::fs::write("config.toml", "").unwrap();
String::new()
}
Ok(cfg) => cfg,
Err(e) => {
panic!("Config file could not be read: {}", e);
}
let cfg = &match std::fs::read_to_string(cfgpath) {
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
std::fs::write("config.toml", "").unwrap();
String::new()
}
Ok(cfg) => cfg,
Err(e) => {
panic!("Config file could not be read: {}", e);
}
.lines()
.filter(|line| !line.starts_with('#')) // Remove comments, parser can't handle them
.map(|line| line.to_string() + "\n")
.collect::<String>(),
)
}
.parse::<Value>()
.expect("Invalid config file");

#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
let port: u16 = cfg.get("port").unwrap_or(&Toml::Num(5252.0)).num() as u16;
let port: u16 = cfg
.get("port")
.unwrap_or(&Value::Integer(5252))
.as_integer()
.unwrap() as u16;

let tls = cfg.get("tls").unwrap_or(&Toml::Bool(false));
let tls = cfg
.get("tls")
.unwrap_or(&Value::Boolean(false))
.as_bool()
.unwrap();
let mut cert = String::new();
let mut key = String::new();
if tls == &Toml::Bool(true) {
if tls {
cert = cfg
.get("cert")
.unwrap_or(&Toml::Str(String::new()))
.str()
.unwrap_or(&Value::String(String::new()))
.as_str()
.unwrap()
.to_string();
key = cfg
.get("key")
.unwrap_or(&Toml::Str(String::new()))
.str()
.unwrap_or(&Value::String(String::new()))
.as_str()
.unwrap()
.to_string();
}

let pass = cfg.get("pass").unwrap_or(&Toml::Bool(false));
let pass = cfg
.get("pass")
.unwrap_or(&Value::Boolean(false))
.as_bool()
.unwrap();

let mut hash = String::new();
let mut secret = String::new();
if pass == &Toml::Bool(true) {
if pass {
hash = cfg
.get("hash")
.unwrap_or(&Toml::Str(String::new()))
.str()
.unwrap_or(&Value::String(String::new()))
.as_str()
.unwrap()
.to_string();
secret = cfg
.get("secret")
.unwrap_or(&Toml::Str(String::new()))
.str()
.unwrap_or(&Value::String(String::new()))
.as_str()
.unwrap()
.to_string();
}

#[cfg(feature = "frontend")]
let mut nodes = Vec::new();

#[cfg(feature = "frontend")]
for i in cfg
.get("nodes")
.unwrap_or(&Value::Array(Vec::new()))
.as_array()
.unwrap()
{
nodes.push(i.as_str().unwrap().to_string());
}

Config {
port,
tls: tls == &Toml::Bool(true),
tls,
cert,
key,
pass: pass == &Toml::Bool(true),
pass,
hash,
secret,
#[cfg(feature = "frontend")]
nodes,
}
}
17 changes: 14 additions & 3 deletions src/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() {
.build()
.unwrap()
.block_on(async {
#[cfg(feature = "frontend")]
const DIR: include_dir::Dir = include_dir::include_dir!("dist");

SimpleLogger::new()
Expand All @@ -27,6 +28,7 @@ fn main() {
.init()
.unwrap();

#[cfg(feature = "frontend")]
let favicon_route = warp::path("favicon.png").map(|| {
warp::reply::with_header(
DIR.get_file("favicon.png").unwrap().contents(),
Expand All @@ -35,6 +37,7 @@ fn main() {
)
});

#[cfg(feature = "frontend")]
let assets_route = warp::path("assets")
.and(warp::path::param())
.map(|path: String| {
Expand Down Expand Up @@ -87,7 +90,11 @@ fn main() {
"No login needed".to_string(),
warp::http::StatusCode::OK,
)
});
})
.with(warp::reply::with::header(
"Access-Control-Allow-Origin",
"*",
));

let terminal_route = warp::path!("ws" / "term")
.and(warp::ws())
Expand All @@ -97,20 +104,21 @@ fn main() {
.and(warp::ws())
.map(|ws: warp::ws::Ws| ws.on_upgrade(sockets::socket_handler));

#[cfg(feature = "frontend")]
let main_route = warp::any().map(|| {
warp::reply::html(DIR.get_file("index.html").unwrap().contents_utf8().unwrap())
});

#[cfg(feature = "frontend")]
let page_routes = favicon_route
.or(assets_route)
.or(login_route)
.or(main_route)
.with(warp::compression::gzip());

let socket_routes = terminal_route.or(socket_route);

let routes = socket_routes
.or(page_routes)
.or(login_route)
.with(warp::log::custom(|info| {
log::info!("Request to {}", info.path());
log::debug!(
Expand All @@ -122,6 +130,9 @@ fn main() {
);
}));

#[cfg(feature = "frontend")]
let routes = routes.or(page_routes);

if CONFIG.tls {
warp::serve(routes)
.tls()
Expand Down
2 changes: 2 additions & 0 deletions src/backend/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub struct ServiceList {
pub struct GlobalData {
pub update: String,
pub login: bool,
#[cfg(feature = "frontend")]
pub nodes: Vec<String>,
}

#[derive(SerJson, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions src/backend/src/systemdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ pub fn global() -> shared::GlobalData {
shared::GlobalData {
update,
login: crate::CONFIG.pass,
#[cfg(feature = "frontend")]
nodes: crate::CONFIG.nodes.clone(),
}
}

Expand Down
Binary file modified src/frontend/.yarn/install-state.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion src/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; font-src 'self'; img-src 'self'; script-src 'self'; style-src 'unsafe-inline' 'self'; connect-src 'self' ws:;" />
content="default-src 'self'; font-src 'self'; img-src 'self'; script-src 'self'; style-src 'unsafe-inline' 'self'; connect-src * ws:;" />
<link rel="icon" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DietPi Dashboard</title>
Expand Down

0 comments on commit c2b6953

Please sign in to comment.