5 use listenfd
::ListenFd
;
7 use actix_web
::{ get
, web
, Responder
, middleware
, App
, HttpServer
, web
::Query
};
10 use std
::{ sync
::Mutex
, fs
::File
, env
::args
, io
::prelude
::* };
11 use ron
::{ de
::from_reader
, ser
::{ to_string_pretty
, PrettyConfig
} };
12 use serde
::{ Deserialize
, Serialize
};
16 mod valheim_controller
;
19 #[template(path = "main.html")]
24 #[derive(Deserialize)]
30 async
fn main_page(/*key_shared: web::Data<Mutex<String>>,*/ query
: Query
<Request
>) -> impl Responder
{
31 //let key = key_shared.lock().unwrap();
34 if valheim_controller
::is_valheim_running() { String
::from("Valheim server is up and running") } else { String
::from("Valheim server is down :(") };
36 MainTemplate
{ sentence
: m
}
39 #[derive(Debug, Deserialize, Serialize)]
44 const DEFAULT_CONFIG
: Config
= Config
{ port
: 8082 };
46 fn get_exe_name() -> String
{
47 let first_arg
= std
::env
::args().next().unwrap();
48 let sep
: &[_
] = &['
\\'
, '
/'
];
49 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
52 fn load_config() -> Config
{
53 // unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
54 match File
::open(consts
::FILE_CONF
) {
55 Ok(file
) => from_reader(file
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
)),
57 let mut file
= File
::create(consts
::FILE_CONF
) .unwrap();
58 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.
65 async
fn main() -> std
::io
::Result
<()> {
67 if process_args() { return Ok(()) }
69 println!("Starting RUP as web server...");
71 let config
= load_config();
73 println!("Configuration: {:?}", config
);
75 let mut listenfd
= ListenFd
::from_env();
80 // .app_data(key_shared.clone())
81 .wrap(middleware
::Compress
::default())
82 .wrap(middleware
::Logger
::default())
84 .service(fs
::Files
::new("/static", "static").show_files_listing())
89 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
90 server
.listen(l
).unwrap()
92 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
98 fn process_args() -> bool
{
101 println!(" {} [--help] [--status]", get_exe_name());
104 let args
: Vec
<String
> = args().collect();
106 if args
.iter().any(|arg
| arg
== "--help") {
109 } else if args
.iter().any(|arg
| arg
== "--status") {
110 valheim_controller
::is_valheim_running();