1 extern crate actix_web
;
5 use listenfd
::ListenFd
;
7 use actix_web
::{web
, middleware
, App
, HttpServer
, HttpResponse
, Result
, web
::Query
};
10 use std
::io
::prelude
::*;
11 use ron
::de
::from_reader
;
12 use serde
::Deserialize
;
13 use std
::{fs
::File
, env
::args
};
15 use itertools
::Itertools
;
20 #[template(path = "main.html")]
21 struct MainTemplate
<'a
> {
25 #[derive(Deserialize)]
30 fn main_page(query
: Query
<Request
>) -> Result
<HttpResponse
> {
32 let main_template
= MainTemplate
{ test
: &"test" };
34 let s
= main_template
.render().unwrap();
35 Ok(HttpResponse
::Ok().content_type("text/html").body(s
))
38 #[derive(Debug, Deserialize)]
43 fn get_exe_name() -> String
{
44 let first_arg
= std
::env
::args().nth(0).unwrap();
45 let sep
: &[_
] = &['
\\'
, '
/'
];
46 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
49 fn main() -> std
::io
::Result
<()> {
50 if process_args() { return Ok(()) }
52 println!("Starting RUP as web server...");
54 let config
: Config
= {
55 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
56 match from_reader(f
) {
58 Err(e
) => panic!("Failed to load config: {}", e
)
62 println!("Configuration: {:?}", config
);
64 let mut listenfd
= ListenFd
::from_env();
69 .wrap(middleware
::Compress
::default())
70 .wrap(middleware
::Logger
::default())
71 .service(web
::resource("/").to(main_page
))
72 .service(fs
::Files
::new("/static", "static").show_files_listing())
77 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
78 server
.listen(l
).unwrap()
80 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
86 fn process_args() -> bool
{
89 println!(" {} [--help]", get_exe_name());
92 let args
: Vec
<String
> = args().collect();
94 if args
.iter().any(|arg
| arg
== "--help") {