5 // use futures::sink::With;
6 use listenfd
::ListenFd
;
8 use actix_web
::{ get
, Responder
, middleware
, App
, HttpServer
, web
::Query
};
11 use std
::{ /*sync::Mutex, */fs
::File
, env
::args
, io
::prelude
::* };
12 use ron
::{ de
::from_reader
, ser
::{ to_string_pretty
, PrettyConfig
} };
13 use serde
::{ Deserialize
, Serialize
};
17 mod valheim_controller
;
20 #[template(path = "main.html")]
28 #[derive(Deserialize)]
33 const VALUE_UNKNOWN
: &str = "-";
36 async
fn main_page(/*key_shared: web::Data<Mutex<String>>,*/ query
: Query
<Request
>) -> impl Responder
{
37 //let key = key_shared.lock().unwrap();
39 match valheim_controller
::get_valheim_executable_information() {
42 text_status
: String
::from("Valheim server is up and running :)"),
43 memory
: info
.format_memory(),
44 load_average
: info
.format_load_average(),
45 uptime
: info
.format_uptime()
48 let value_unknown
= String
::from(VALUE_UNKNOWN
);
49 MainTemplate
{ text_status
: String
::from("Valheim server is down :("), memory
: value_unknown
.clone(), load_average
: value_unknown
.clone(), uptime
: value_unknown
.clone() }
56 if valheim_controller::is_valheim_running() { String::from("Valheim server is up and running") } else { String::from("Valheim server is down :(") };
58 MainTemplate { sentence: m }
62 #[derive(Debug, Deserialize, Serialize)]
67 const DEFAULT_CONFIG
: Config
= Config
{ port
: 8082 };
69 fn get_exe_name() -> String
{
70 let first_arg
= std
::env
::args().next().unwrap();
71 let sep
: &[_
] = &['
\\'
, '
/'
];
72 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
75 fn load_config() -> Config
{
76 // unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
77 match File
::open(consts
::FILE_CONF
) {
78 Ok(file
) => from_reader(file
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
)),
80 let mut file
= File
::create(consts
::FILE_CONF
) .unwrap();
81 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.
88 async
fn main() -> std
::io
::Result
<()> {
90 if process_args() { return Ok(()) }
92 println!("Starting Valheim Admin as web server...");
94 let config
= load_config();
96 println!("Configuration: {:?}", config
);
98 let mut listenfd
= ListenFd
::from_env();
103 // .app_data(key_shared.clone())
104 .wrap(middleware
::Compress
::default())
105 .wrap(middleware
::Logger
::default())
107 .service(fs
::Files
::new("/static", "static").show_files_listing())
112 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
113 server
.listen(l
).unwrap()
115 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
121 fn process_args() -> bool
{
124 println!(" {} [--help] [--status]", get_exe_name());
127 let args
: Vec
<String
> = args().collect();
129 if args
.iter().any(|arg
| arg
== "--help") {
132 } else if args
.iter().any(|arg
| arg
== "--status") {
133 println!("{:?}", valheim_controller
::get_valheim_executable_information());