Display some server process information (memory, cpu usage, uptime)
[valheim_web.git] / backend / src / main.rs
index d2607fd..88424da 100644 (file)
@@ -2,12 +2,13 @@
 extern crate listenfd;
 extern crate askama;
 
+// use futures::sink::With;
 use listenfd::ListenFd;
 use actix_files as fs;
-use actix_web::{ get, web, Responder, middleware, App, HttpServer, web::Query };
+use actix_web::{ get, Responder, middleware, App, HttpServer, web::Query };
 use askama::Template;
 
-use std::{ sync::Mutex, fs::File, env::args, io::prelude::* };
+use std::{ /*sync::Mutex, */fs::File, env::args, io::prelude::* };
 use ron::{ de::from_reader, ser::{ to_string_pretty, PrettyConfig } };
 use serde::{ Deserialize, Serialize };
 
@@ -18,7 +19,10 @@ mod valheim_controller;
 #[derive(Template)]
 #[template(path = "main.html")]
 struct MainTemplate {
-    sentence: String,
+    text_status: String,
+    memory: String,
+    cpu: String,
+    uptime: String
 }
 
 #[derive(Deserialize)]
@@ -26,14 +30,33 @@ pub struct Request {
    m: Option<String>
 }
 
+const VALUE_UNKNOWN: &str = "-";
+
 #[get("/")]
 async fn main_page(/*key_shared: web::Data<Mutex<String>>,*/ query: Query<Request>) -> impl Responder {
     //let key = key_shared.lock().unwrap();
 
-    let m = 
+    match valheim_controller::get_valheim_executable_information() {
+        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()
+            },
+        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() }
+        }
+    }
+
+    /*
+
+    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)]
@@ -66,7 +89,7 @@ async fn main() -> std::io::Result<()> {
 
     if process_args() { return Ok(()) }
 
-    println!("Starting RUP as web server...");
+    println!("Starting Valheim Admin as web server...");
 
     let config = load_config();
 
@@ -107,7 +130,7 @@ fn process_args() -> bool {
         print_usage();
         return true
     } else if args.iter().any(|arg| arg == "--status") {
-        valheim_controller::is_valheim_running();
+        println!("{:?}", valheim_controller::get_valheim_executable_information());
         return true
     }