Use a cached function
[valheim_web.git] / backend / src / main.rs
index c4c51a0..03963c1 100644 (file)
@@ -7,6 +7,7 @@ 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 +20,34 @@ struct MainTemplate {
     memory: String,
     load_average: String,
     uptime: String,
-    world_size: String
+    world_size: String,
+    nb_of_players: u32,
 }
 
 const VALUE_UNKNOWN: &str = "-";
 
+#[cached(size = 1, time = 10)]
+fn get_valheim_executable_information_cached(world_path: String) -> Option<valheim_controller::ValheimExe> {
+    valheim_controller::get_valheim_executable_information(&world_path)
+}
+
 #[get("/")]
 async fn main_page(config_shared: web::Data<Mutex<Config>>) -> 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()) {
         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(),
+                nb_of_players: info.get_nb_of_player()
             },
         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(), nb_of_players: 0 }
         }
     }
 }