X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fmain.rs;h=b3025204b6c0851d843b69e86350a2319ecace67;hb=745387dbdf62a58db23a753f3a8ec8ac7e937a0e;hp=c4c51a0ddfc0ed859c93e1d8c4597d7d4859a09a;hpb=c4620d6f981e84059c464604969a55ff085152f6;p=valheim_web.git diff --git a/backend/src/main.rs b/backend/src/main.rs index c4c51a0..b302520 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,12 +1,12 @@ extern crate askama; use std::{ sync::Mutex, env::args, fs::File, io::prelude::* }; - use actix_files as fs; use actix_web::{ get, web, Responder, middleware, App, HttpServer }; use askama::Template; use ron::{ de::from_reader, ser::{ to_string_pretty, PrettyConfig } }; use serde::{ Deserialize, Serialize }; +use cached::proc_macro::cached; mod consts; mod tests; @@ -19,27 +19,43 @@ struct MainTemplate { memory: String, load_average: String, uptime: String, - world_size: String + world_size: String, + active_players: String, + last_backup: String, } const VALUE_UNKNOWN: &str = "-"; +#[cached(size = 1, time = 10)] +fn get_valheim_executable_information_cached(world_path: String, backup_path: String) -> Option { + valheim_controller::get_valheim_executable_information(&world_path, &backup_path) +} + #[get("/")] async fn main_page(config_shared: web::Data>) -> impl Responder { let config = config_shared.lock().unwrap(); - match valheim_controller::get_valheim_executable_information(&config.world_path) { + match get_valheim_executable_information_cached(config.world_path.clone(), config.backup_path.clone()) { Some(info) => MainTemplate { text_status: String::from("Valheim server is up and running :)"), memory: info.format_memory(), load_average: info.format_load_average(), uptime: info.format_uptime(), - world_size: info.format_world_size() + world_size: info.format_world_size(), + active_players: info.format_active_players(), + last_backup: info.format_last_backup() }, None => { let value_unknown = String::from(VALUE_UNKNOWN); - MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone(), world_size: value_unknown.clone() } + MainTemplate { + text_status: String::from("Valheim server is down :("), + memory: value_unknown.clone(), + load_average: value_unknown.clone(), + uptime: value_unknown.clone(), + world_size: value_unknown.clone(), + active_players: value_unknown.clone(), + last_backup: value_unknown.clone() } } } } @@ -47,15 +63,19 @@ async fn main_page(config_shared: web::Data>) -> impl Responder { #[derive(Debug, Deserialize, Serialize)] struct Config { port: u16, + #[serde(default = "empty_string")] world_path: String, + + #[serde(default = "empty_string")] + backup_path: String, } fn empty_string() -> String { "".to_owned() } impl Config { fn default() -> Self { - Config { port: 8082, world_path: String::from("") } + Config { port: 8082, world_path: String::from(""), backup_path: String::from("") } } } @@ -78,7 +98,7 @@ fn load_config() -> Config { } } -#[actix_rt::main] +#[actix_web::main] async fn main() -> std::io::Result<()> { let config = load_config(); let port = config.port; @@ -120,7 +140,7 @@ fn process_args(config: &Config) -> bool { print_usage(); return true } else if args.iter().any(|arg| arg == "--status") { - println!("{:?}", valheim_controller::get_valheim_executable_information(&config.world_path)); + println!("{:?}", valheim_controller::get_valheim_executable_information(&config.world_path, &config.backup_path)); return true }