use listenfd::ListenFd;
use actix_files as fs;
-use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query};
+use actix_web::{web, middleware, App, HttpServer, HttpResponse, web::Query};
use askama::Template;
use std::io::prelude::*;
m: Option<String>
}
-fn main_page(query: Query<Request>, key: &str) -> Result<HttpResponse> {
+fn main_page(query: Query<Request>, key: &str) -> HttpResponse {
let m =
match &query.m {
Some(b) =>
let hello = MainTemplate { sentence: &m };
let s = hello.render().unwrap();
- Ok(HttpResponse::Ok().content_type("text/html").body(s))
+ HttpResponse::Ok().content_type("text/html").body(s)
}
#[derive(Debug, Deserialize)]
}
fn get_exe_name() -> String {
- let first_arg = std::env::args().nth(0).unwrap();
+ let first_arg = std::env::args().next().unwrap();
let sep: &[_] = &['\\', '/'];
first_arg[first_arg.rfind(sep).unwrap()+1..].to_string()
}
fn read_key() -> String {
let mut key = String::new();
File::open(consts::FILE_KEY)
- .expect(&format!("Failed to open key file: {}", consts::FILE_KEY))
+ .unwrap_or_else(|_| panic!("Failed to open key file: {}", consts::FILE_KEY))
.read_to_string(&mut key)
- .expect(&format!("Failed to read key file: {}", consts::FILE_KEY));
+ .unwrap_or_else(|_| panic!("Failed to read key file: {}", consts::FILE_KEY));
String::from(
percent_encoding::percent_decode(key.replace('\n', "").as_bytes())
.decode_utf8()
- .expect(&format!("Failed to decode key file: {}", consts::FILE_KEY))
+ .unwrap_or_else(|_| panic!("Failed to decode key file: {}", consts::FILE_KEY))
)
}
-fn main() -> std::io::Result<()> {
+#[actix_rt::main]
+async fn main() -> std::io::Result<()> {
let key = read_key();
if process_args(&key) { return Ok(()) }
println!("Starting RUP as web server...");
let config: Config = {
- let f = File::open(consts::FILE_CONF).expect(&format!("Failed to open configuration file {}", consts::FILE_CONF));
+ let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
match from_reader(f) {
Ok(c) => c,
Err(e) => panic!("Failed to load config: {}", e)
server.bind(&format!("0.0.0.0:{}", config.port)).unwrap()
};
- server.run()
+ server.run().await
}
-fn process_args(key: &String) -> bool {
+fn process_args(key: &str) -> bool {
fn print_usage() {
println!("Usage:");
println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
print_usage();
return true
} else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") {
- match args.iter().nth(position_arg_encrypt + 1) {
+ match args.get(position_arg_encrypt + 1) {
Some(mess_to_encrypt) => {
- match crypto::encrypt(&key, mess_to_encrypt) {
+ // Encrypt to version 2 (version 1 is obsolete).
+ match crypto::encrypt(&key, mess_to_encrypt, 2) {
Ok(encrypted_mess) => {
let encrypted_mess_encoded = percent_encoding::utf8_percent_encode(&encrypted_mess, percent_encoding::NON_ALPHANUMERIC).to_string();
println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded); },
return true
} else if let Some((position_arg_decrypt, _)) = args.iter().find_position(|arg| arg == &"--decrypt") {
- match args.iter().nth(position_arg_decrypt + 1) {
+ match args.get(position_arg_decrypt + 1) {
Some(cipher_text) => {
let cipher_text_decoded = percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
match crypto::decrypt(&key, &cipher_text_decoded) {
return true
}
- return false
+ false
}