use chrono::{prelude::*, Duration};
use clap::Parser;
use serde::Deserialize;
+use log::{debug, error, log_enabled, info, Level};
use config::Config;
use user::User;
///// UTILS /////
fn get_ip_and_user_agent(req: &HttpRequest) -> (String, String) {
+ let ip =
+ match req.headers().get(consts::REVERSE_PROXY_IP_HTTP_FIELD) {
+ Some(v) => v.to_str().unwrap_or_default().to_string(),
+ None => req.peer_addr().map(|addr| addr.ip().to_string()).unwrap_or_default()
+ };
+
let user_agent = req.headers().get(header::USER_AGENT).map(|v| v.to_str().unwrap_or_default()).unwrap_or_default().to_string();
- let ip = req.peer_addr().map(|addr| addr.ip().to_string()).unwrap_or_default();
+
(ip, user_agent)
}
Ok(user) =>
Some(user),
Err(error) => {
- eprintln!("Error during authentication: {:?}", error);
+ error!("Error during authentication: {}", error);
None
}
},
Err(error) => {
- eprintln!("Error during authentication: {:?}", error);
+ error!("Error during authentication: {}", error);
None
},
},
#[post("/signup")]
async fn sign_up_post(req: HttpRequest, form: web::Form<SignUpFormData>, connection: web::Data<db::Connection>, config: web::Data<Config>) -> impl Responder {
- println!("Sign up, email: {}, passwords: {}/{}", form.email, form.password_1, form.password_2);
-
fn error_response(error: SignUpError, form: &web::Form<SignUpFormData>, user: Option<User>) -> HttpResponse {
SignUpFormTemplate {
user,
.insert_header((header::LOCATION, "/signup_check_email"))
.finish(),
Err(error) => {
- eprintln!("Email validation error: {:?}", error);
+ error!("Email validation error: {}", error);
error_response(SignUpError::UnableSendEmail, &form, user)
},
}
},
Err(error) => {
- eprintln!("Signup database error: {:?}", error);
+ error!("Signup database error: {}", error);
error_response(SignUpError::DatabaseError, &form, user)
},
}
Ok(user) =>
Some(user),
Err(error) => {
- eprintln!("Error retrieving user by id: {}", error);
+ error!("Error retrieving user by id: {}", error);
None
}
};
}.to_response();
if let Err(error) = response.add_cookie(&cookie) {
- eprintln!("Unable to set cookie after validation: {:?}", error);
+ error!("Unable to set cookie after validation: {}", error);
};
response
#[post("/signin")]
async fn sign_in_post(req: HttpRequest, form: web::Form<SignInFormData>, connection: web::Data<db::Connection>) -> impl Responder {
- println!("Sign in, email: {}, password: {}", form.email, form.password);
-
fn error_response(error: SignInError, form: &web::Form<SignInFormData>, user: Option<User>) -> HttpResponse {
SignInFormTemplate {
user,
.insert_header((header::LOCATION, "/"))
.finish();
if let Err(error) = response.add_cookie(&cookie) {
- eprintln!("Unable to set cookie after sign in: {:?}", error);
+ error!("Unable to set cookie after sign in: {}", error);
};
response
},
Err(error) => {
- eprintln!("Signin error: {:?}", error);
+ error!("Signin error: {}", error);
error_response(SignInError::AuthenticationFailed, &form, user)
},
}
if let Some(token_cookie) = req.cookie(COOKIE_AUTH_TOKEN_NAME) {
if let Err(error) = connection.sign_out(token_cookie.value()) {
- eprintln!("Unable to sign out: {:?}", error);
+ error!("Unable to sign out: {}", error);
};
if let Err(error) = response.add_removal_cookie(&Cookie::new(COOKIE_AUTH_TOKEN_NAME, "")) {
- eprintln!("Unable to set a removal cookie after sign out: {:?}", error);
+ error!("Unable to set a removal cookie after sign out: {}", error);
};
};
response
async fn main() -> std::io::Result<()> {
if process_args() { return Ok(()) }
- std::env::set_var("RUST_LOG", "actix_web=debug");
+ std::env::set_var("RUST_LOG", "info,actix_web=info");
env_logger::init();
println!("Starting Recipes as web server...");
let db_connection = web::Data::new(db::Connection::new().unwrap());
- std::env::set_var("RUST_LOG", "actix_web=info");
-
let server =
HttpServer::new(move || {
App::new()
match db::Connection::new() {
Ok(con) => {
if let Err(error) = con.execute_file("sql/data_test.sql") {
- println!("Error: {:?}", error);
+ error!("{}", error);
}
// Set the creation datetime to 'now'.
con.execute_sql("UPDATE [User] SET [creation_datetime] = ?1 WHERE [email] = 'paul@test.org'", [Utc::now()]).unwrap();
},
Err(error) => {
- println!("Error: {:?}", error)
+ error!("Error: {}", error)
},
}