b386358d279e3c09d3b4ddcdb89bd849d8992742
5 // use futures::sink::With;
6 use listenfd
::ListenFd
;
8 use actix_web
::{ get
, Responder
, middleware
, App
, HttpServer
};
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")]
29 const VALUE_UNKNOWN
: &str = "-";
32 async
fn main_page() -> impl Responder
{
33 //let key = key_shared.lock().unwrap();
35 match valheim_controller
::get_valheim_executable_information() {
38 text_status
: String
::from("Valheim server is up and running :)"),
39 memory
: info
.format_memory(),
40 load_average
: info
.format_load_average(),
41 uptime
: info
.format_uptime(),
42 world_size
: info
.format_world_size()
45 let value_unknown
= String
::from(VALUE_UNKNOWN
);
46 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() }
51 #[derive(Debug, Deserialize, Serialize)]
56 const DEFAULT_CONFIG
: Config
= Config
{ port
: 8082 };
58 fn get_exe_name() -> String
{
59 let first_arg
= std
::env
::args().next().unwrap();
60 let sep
: &[_
] = &['
\\'
, '
/'
];
61 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
64 fn load_config() -> Config
{
65 // unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
66 match File
::open(consts
::FILE_CONF
) {
67 Ok(file
) => from_reader(file
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
)),
69 let mut file
= File
::create(consts
::FILE_CONF
) .unwrap();
70 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.
77 async
fn main() -> std
::io
::Result
<()> {
79 if process_args() { return Ok(()) }
81 println!("Starting Valheim Admin as web server...");
83 let config
= load_config();
85 println!("Configuration: {:?}", config
);
87 let mut listenfd
= ListenFd
::from_env();
92 // .app_data(key_shared.clone())
93 .wrap(middleware
::Compress
::default())
94 .wrap(middleware
::Logger
::default())
96 .service(fs
::Files
::new("/static", "static").show_files_listing())
101 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
102 server
.listen(l
).unwrap()
104 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
110 fn process_args() -> bool
{
113 println!(" {} [--help] [--status]", get_exe_name());
116 let args
: Vec
<String
> = args().collect();
118 if args
.iter().any(|arg
| arg
== "--help") {
121 } else if args
.iter().any(|arg
| arg
== "--status") {
122 println!("{:?}", valheim_controller
::get_valheim_executable_information());