1 use std
::io
::prelude
::*;
2 use std
::{fs
::File
, env
::args
};
5 use actix_web
::{web
, middleware
, App
, HttpServer
, HttpResponse
, Result
, web
::Query
, middleware
::Logger
};
8 use listenfd
::ListenFd
;
9 use ron
::de
::from_reader
;
10 use serde
::Deserialize
;
13 use itertools
::Itertools
;
19 #[template(path = "main.html")]
20 struct MainTemplate
<'a
> {
24 #[derive(Deserialize)]
29 fn main_page(query
: Query
<Request
>) -> Result
<HttpResponse
> {
31 let main_template
= MainTemplate
{ test
: &"*** test ***" };
33 let s
= main_template
.render().unwrap();
34 Ok(HttpResponse
::Ok().content_type("text/html").body(s
))
37 #[derive(Debug, Deserialize)]
42 fn get_exe_name() -> String
{
43 let first_arg
= std
::env
::args().nth(0).unwrap();
44 let sep
: &[_
] = &['
\\'
, '
/'
];
45 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
48 fn main() -> std
::io
::Result
<()> {
49 if process_args() { return Ok(()) }
51 println!("Starting RUP as web server...");
53 let config
: Config
= {
54 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
55 match from_reader(f
) {
57 Err(e
) => panic!("Failed to load config: {}", e
)
61 println!("Configuration: {:?}", config
);
63 // let database_connection = db::create_or_update();
65 std
::env
::set_var("RUST_LOG", "actix_web=info");
68 let mut listenfd
= ListenFd
::from_env();
73 .wrap(middleware
::Compress
::default())
74 .wrap(Logger
::default())
75 .wrap(Logger
::new("%a %{User-Agent}i"))
76 .service(web
::resource("/").to(main_page
))
77 .service(fs
::Files
::new("/static", "static").show_files_listing())
82 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
83 server
.listen(l
).unwrap()
85 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
91 fn process_args() -> bool
{
94 println!(" {} [--help] [--test]", get_exe_name());
97 let args
: Vec
<String
> = args().collect();
99 if args
.iter().any(|arg
| arg
== "--help") {
102 } else if args
.iter().any(|arg
| arg
== "--test") {
103 let database_connection
= db
::create_or_update();