Skip to content

Commit

Permalink
fix: clippy + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Apr 4, 2024
1 parent a8c6b44 commit 3b315d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ fn main() {

util::logger::init(args.log.map(|s| s.into()));

let state;

if !args.no_auth {
let state = if !args.no_auth {
let mut username = String::new();

if args.username.is_none() {
Expand All @@ -65,17 +63,17 @@ fn main() {
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut username).unwrap();
}

// Prompt for password
print!("Password to use while authenticating: ");
std::io::stdout().flush().unwrap();
let pwd = sha2::Sha256::digest(read_password().unwrap().as_bytes()).to_vec();

state = web::server::State::new(username.trim().to_string(), pwd);
web::server::State::new(username.trim().to_string(), pwd)
} else {
error!("Running in no-auth mode. This is insecure and should only be used when developing.");
state = web::server::State::new("".to_string(), vec![]);
}
web::server::State::new("".to_string(), vec![])
};

// Pull the docker image
log!("Pulling minecraft-server Docker image");
Expand Down
38 changes: 22 additions & 16 deletions src/util/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub async fn init() {
.expect("Failed to get output")
.status
.expect("Failed to get output status")
);
);
});

stream.await;
Expand Down Expand Up @@ -202,14 +202,7 @@ pub async fn deploy_minecraft_container(
opts.kind.clone()
}
),
format!(
"PORT={}",
if opts.port == 0 {
25565
} else {
opts.port
}
),
format!("PORT={}", if opts.port == 0 { 25565 } else { opts.port }),
]),
host_config: Some(bollard::models::HostConfig {
port_bindings: Some(
Expand Down Expand Up @@ -255,7 +248,7 @@ pub async fn deploy_minecraft_container(
let container = docker
.create_container(
Some(CreateContainerOptions {
name: opts.name.clone().replace(" ", "_"),
name: opts.name.clone().replace(' ', "_"),
platform: Some("linux".to_string()),
}),
create_opts,
Expand Down Expand Up @@ -382,7 +375,10 @@ pub async fn run_in_container(
}
}

pub async fn get_logs(id: &str, since: i64) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
pub async fn get_logs(
id: &str,
since: i64,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let docker = bollard::Docker::connect_with_local_defaults().unwrap();
let mut logs = docker.logs(
id,
Expand All @@ -405,7 +401,9 @@ pub async fn get_logs(id: &str, since: i64) -> Result<String, Box<dyn std::error
Ok(result)
}

pub async fn resource_usage(id: &str) -> Result<Resources, Box<dyn std::error::Error + Send + Sync>> {
pub async fn resource_usage(
id: &str,
) -> Result<Resources, Box<dyn std::error::Error + Send + Sync>> {
let docker = bollard::Docker::connect_with_local_defaults().unwrap();
let mut stats = docker.stats(
id,
Expand All @@ -415,14 +413,22 @@ pub async fn resource_usage(id: &str) -> Result<Resources, Box<dyn std::error::E
}),
);

while let Some(data) = stats.next().await {
if let Some(data) = stats.next().await {
let data = data?;

// https://stackoverflow.com/a/64148340
// CPU
let cpu_delta = data.cpu_stats.cpu_usage.total_usage - data.precpu_stats.cpu_usage.total_usage;
let system_delta = data.cpu_stats.system_cpu_usage.unwrap_or_default() - data.precpu_stats.system_cpu_usage.unwrap_or_default();
let cpu = (cpu_delta as f64 / system_delta as f64 * data.cpu_stats.cpu_usage.percpu_usage.unwrap_or(vec![]).len() as f64 * 100.0) as u64;
let system_delta = data.cpu_stats.system_cpu_usage.unwrap_or_default()
- data.precpu_stats.system_cpu_usage.unwrap_or_default();
let cpu = (cpu_delta as f64 / system_delta as f64
* data
.cpu_stats
.cpu_usage
.percpu_usage
.unwrap_or(vec![])
.len() as f64
* 100.0) as u64;

// Memory
let memory = data.memory_stats.usage.unwrap_or_default();
Expand Down Expand Up @@ -457,4 +463,4 @@ pub async fn resource_usage(id: &str) -> Result<Resources, Box<dyn std::error::E
}

Err("Failed to get stats".into())
}
}
15 changes: 8 additions & 7 deletions src/web/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use tide::Server;

use crate::{log, util::docker::{self, Resources}};
use crate::{
log,
util::docker::{self, Resources},
};

use super::server::State;

Expand Down Expand Up @@ -92,11 +95,9 @@ pub fn register_routes(app: &mut Server<State>) {
.unwrap_or(0);

let logs = match docker::get_logs(id, since).await {
Ok(logs) => {
DockerResult {
success: true,
message: logs,
}
Ok(logs) => DockerResult {
success: true,
message: logs,
},
Err(e) => {
log!("Failed to get logs for container {}: {}", id, e);
Expand Down Expand Up @@ -143,7 +144,7 @@ pub fn register_routes(app: &mut Server<State>) {
success: false,
message: format!("Failed to request JSON: {}", e),
};

let mut response = tide::Response::new(400);
response.set_body(serde_json::to_string(&resp).unwrap_or_default());

Expand Down

0 comments on commit 3b315d1

Please sign in to comment.