1 extern crate actix_web
;
5 use listenfd
::ListenFd
;
7 use actix_web
::{web
, middleware
, App
, HttpServer
, HttpResponse
, Responder
, 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
;
21 #[template(path = "main.html")]
22 struct MainTemplate
<'a
> {
26 #[derive(Deserialize)]
31 static DEFAULT_MESSAGE
: &str = "Marc, roule un pet'!";
32 static KEY
: &str = "secret";
34 fn main_page(query
: Query
<Request
>) -> Result
<HttpResponse
> {
38 match crypto
::decrypt(KEY
, b
) {
40 None
=> String
::from(DEFAULT_MESSAGE
)
42 None
=> String
::from(DEFAULT_MESSAGE
)
45 let hello
= MainTemplate
{ sentence
: &m
};
47 let s
= hello
.render().unwrap();
48 Ok(HttpResponse
::Ok().content_type("text/html").body(s
))
51 #[derive(Debug, Deserialize)]
56 fn get_exe_name() -> String
{
57 let first_arg
= std
::env
::args().nth(0).unwrap();
59 let sep
: &[_
] = &['
\\'
, '
/'
];
60 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
65 println!(" {} [--help] [--encrypt <message>]", get_exe_name());
68 fn read_key() -> String
{
69 use url
::percent_encoding
::percent_decode
;
70 let mut key
= String
::new();
71 File
::open(consts
::FILE_KEY
)
72 .expect(&format!("Failed to open key file: {}", consts
::FILE_KEY
))
73 .read_to_string(&mut key
)
74 .expect(&format!("Failed to read key file: {}", consts
::FILE_KEY
));
77 percent_decode(key
.as_bytes())
79 .expect(&format!("Failed to decode key file: {}", consts
::FILE_KEY
))
83 fn main() -> std
::io
::Result
<()> {
84 let args
: Vec
<String
> = args().collect();
88 if args
.iter().any(|arg
| arg
== "--help") {
91 } else if let Some((position_arg_encrypt
, _
)) = args
.iter().find_position(|arg
| arg
== &"--encrypt") {
92 match args
.iter().nth(position_arg_encrypt
+ 1) {
93 Some(mess_to_encrypt
) => {
94 let encrypted_mess
= crypto
::encrypt(&key
, mess_to_encrypt
);
95 println!("Encrypted message: {}", encrypted_mess
);
103 println!("Starting RUP as web server...");
105 let config
: Config
= {
106 let f
= File
::open(consts
::FILE_CONF
).expect(&format!("Failed to open configuration file {}", consts
::FILE_CONF
));
107 match from_reader(f
) {
109 Err(e
) => panic!("Failed to load config: {}", e
)
113 println!("Configuration: {:?}", config
);
115 let mut listenfd
= ListenFd
::from_env();
120 .wrap(middleware
::Compress
::default())
121 .wrap(middleware
::Logger
::default())
122 .service(web
::resource("/").to(main_page
))
123 .service(fs
::Files
::new("/static", "static").show_files_listing())
128 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
129 server
.listen(l
).unwrap()
131 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()