From: Greg Burri Date: Thu, 11 Mar 2021 12:14:28 +0000 (+0100) Subject: Put the world path in the configuration file X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=b76be16ce4c1bf35f67bab105e0f454da6662240;p=valheim_web.git Put the world path in the configuration file --- diff --git a/backend/src/main.rs b/backend/src/main.rs index b386358..a711427 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -5,10 +5,10 @@ extern crate askama; // use futures::sink::With; use listenfd::ListenFd; use actix_files as fs; -use actix_web::{ get, Responder, middleware, App, HttpServer }; +use actix_web::{ get, web, Responder, middleware, App, HttpServer }; use askama::Template; -use std::{ /*sync::Mutex, */fs::File, env::args, io::prelude::* }; +use std::{ sync::Mutex, env::args, fs::File, io::prelude::* }; use ron::{ de::from_reader, ser::{ to_string_pretty, PrettyConfig } }; use serde::{ Deserialize, Serialize }; @@ -29,10 +29,10 @@ struct MainTemplate { const VALUE_UNKNOWN: &str = "-"; #[get("/")] -async fn main_page() -> 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 valheim_controller::get_valheim_executable_information(&config.world_path) { Some(info) => MainTemplate { text_status: String::from("Valheim server is up and running :)"), @@ -50,10 +50,18 @@ async fn main_page() -> impl Responder { #[derive(Debug, Deserialize, Serialize)] struct Config { - port: u16 + port: u16, + #[serde(default = "empty_string")] + world_path: String, } -const DEFAULT_CONFIG: Config = Config { port: 8082 }; +fn empty_string() -> String { "".to_string() } + +impl Config { + fn default() -> Self { + Config { port: 8082, world_path: String::from("") } + } +} fn get_exe_name() -> String { let first_arg = std::env::args().next().unwrap(); @@ -67,29 +75,32 @@ 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] 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 config_shared = web::Data::new(Mutex::new(config)); + let mut listenfd = ListenFd::from_env(); let mut 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) @@ -101,13 +112,13 @@ async fn main() -> std::io::Result<()> { 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() + server.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()); @@ -119,7 +130,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)); return true } diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index c09f080..a483756 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -45,9 +45,8 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { } const VALHEIM_PROCESS_NAME: &str = "valheim_server"; -const VALHEIM_WORLD_PATH: &str = "/home/greg/ValheimWorld/pouet.db"; // TODO: Put in conf. -pub fn get_valheim_executable_information() -> Option { +pub fn get_valheim_executable_information(world_path : &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); let processes = system.get_process_by_name(VALHEIM_PROCESS_NAME); @@ -55,7 +54,7 @@ pub fn get_valheim_executable_information() -> Option { if processes.len() >= 1 { let process = processes.first().unwrap(); - let world_size = match std::fs::metadata(VALHEIM_WORLD_PATH) { Ok(f) => f.len(), Err(_) => 0u64 }; + let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 }; Some( ValheimExe {