X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fmain.rs;h=b3025204b6c0851d843b69e86350a2319ecace67;hb=aa59a1f9b79009079a5a1fbafd4f65d3130e9ee2;hp=88424da8aaee68b4b6d0a2662a088f0fbb4a5825;hpb=7ebb570a4a158743200d0f5e53ab44ee823516f0;p=valheim_web.git diff --git a/backend/src/main.rs b/backend/src/main.rs index 88424da..b302520 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,16 +1,12 @@ - -extern crate listenfd; extern crate askama; -// use futures::sink::With; -use listenfd::ListenFd; +use std::{ sync::Mutex, env::args, fs::File, io::prelude::* }; use actix_files as fs; -use actix_web::{ get, Responder, middleware, App, HttpServer, web::Query }; +use actix_web::{ get, web, Responder, middleware, App, HttpServer }; use askama::Template; - -use std::{ /*sync::Mutex, */fs::File, env::args, io::prelude::* }; use ron::{ de::from_reader, ser::{ to_string_pretty, PrettyConfig } }; use serde::{ Deserialize, Serialize }; +use cached::proc_macro::cached; mod consts; mod tests; @@ -21,50 +17,67 @@ mod valheim_controller; struct MainTemplate { text_status: String, memory: String, - cpu: String, - uptime: String -} - -#[derive(Deserialize)] -pub struct Request { - m: Option + load_average: String, + uptime: 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(/*key_shared: web::Data>,*/ query: Query) -> impl Responder { - //let key = key_shared.lock().unwrap(); +async fn main_page(config_shared: web::Data>) -> impl Responder { + let config = config_shared.lock().unwrap(); - match valheim_controller::get_valheim_executable_information() { + 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(), - cpu: info.format_cpu_usage(), - uptime: info.format_uptime() + load_average: info.format_load_average(), + uptime: info.format_uptime(), + 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(), cpu: value_unknown.clone(), uptime: 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() } } } - - /* - - let m = - if valheim_controller::is_valheim_running() { String::from("Valheim server is up and running") } else { String::from("Valheim server is down :(") }; - - MainTemplate { sentence: m } - */ } #[derive(Debug, Deserialize, Serialize)] struct Config { - port: u16 + port: u16, + + #[serde(default = "empty_string")] + world_path: String, + + #[serde(default = "empty_string")] + backup_path: String, } -const DEFAULT_CONFIG: Config = Config { port: 8082 }; +fn empty_string() -> String { "".to_owned() } + +impl Config { + fn default() -> Self { + Config { port: 8082, world_path: String::from(""), backup_path: String::from("") } + } +} fn get_exe_name() -> String { let first_arg = std::env::args().next().unwrap(); @@ -78,47 +91,44 @@ fn load_config() -> Config { Ok(file) => from_reader(file).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF)), Err(_) => { let mut file = File::create(consts::FILE_CONF) .unwrap(); - file.write_all(to_string_pretty(&DEFAULT_CONFIG, PrettyConfig::new()).unwrap().as_bytes()).unwrap(); // We do not use 'to_writer' because it can't pretty format the output. - DEFAULT_CONFIG + let default_config = Config::default(); + file.write_all(to_string_pretty(&default_config, PrettyConfig::new()).unwrap().as_bytes()).unwrap(); // We do not use 'to_writer' because it can't pretty format the output. + default_config } } } -#[actix_rt::main] +#[actix_web::main] async fn main() -> std::io::Result<()> { + let config = load_config(); + let port = config.port; - if process_args() { return Ok(()) } + if process_args(&config) { return Ok(()) } println!("Starting Valheim Admin as web server..."); - let config = load_config(); - println!("Configuration: {:?}", config); - let mut listenfd = ListenFd::from_env(); - let mut server = + let config_shared = web::Data::new(Mutex::new(config)); + + let server = HttpServer::new( move || { App::new() - // .app_data(key_shared.clone()) + .app_data(config_shared.clone()) .wrap(middleware::Compress::default()) .wrap(middleware::Logger::default()) .service(main_page) .service(fs::Files::new("/static", "static").show_files_listing()) } - ); - - server = - if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { - server.listen(l).unwrap() - } else { - server.bind(&format!("0.0.0.0:{}", config.port)).unwrap() - }; + ) + .bind(&format!("0.0.0.0:{}", port)) + .unwrap(); server.run().await } -fn process_args() -> bool { +fn process_args(config: &Config) -> bool { fn print_usage() { println!("Usage:"); println!(" {} [--help] [--status]", get_exe_name()); @@ -130,7 +140,7 @@ fn process_args() -> bool { print_usage(); return true } else if args.iter().any(|arg| arg == "--status") { - println!("{:?}", valheim_controller::get_valheim_executable_information()); + println!("{:?}", valheim_controller::get_valheim_executable_information(&config.world_path, &config.backup_path)); return true }