diff --git a/Cargo.lock b/Cargo.lock index 45f99c0..59249fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1287,9 +1287,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "md-5" diff --git a/src/main.rs b/src/main.rs index a9ef217..f125e25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,15 +5,17 @@ use actix_web::http::StatusCode; use actix_web::web::Data; use actix_web::{error, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder, Result}; use sqlx::{Pool, Sqlite, SqlitePool}; +use std::time::SystemTime; use tera::Tera; use uuid::Uuid; - extern crate dotenv; +use actix_web::middleware::Logger; use dotenv::dotenv; - mod model; -use model::Link; +use chrono::DateTime; +use chrono::Utc; +use model::Link; const REDIRECT_TIMEOUT_S: i32 = 2; #[derive(Clone)] @@ -23,7 +25,8 @@ struct Config { async fn make_ics_request(req: HttpRequest, db_pool: web::Data>) -> impl Responder { let id = req.match_info().get("id").unwrap_or(""); - println!("serving ics request"); + let now = >>::into(SystemTime::now()).to_rfc3339(); + println!("{now} serving ics request"); match Uuid::parse_str(id) { Ok(uuid) => match Link::find_by_uuid(uuid.to_string(), db_pool).await { Ok(link) => match reqwest::get(link.destination).await { @@ -60,7 +63,8 @@ async fn edit_page( db_pool: web::Data>, conf: web::Data, ) -> Result { - println!("serving edit page"); + let now = >>::into(SystemTime::now()).to_rfc3339(); + println!("{now} serving edit page"); // one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78 if let Some(uuid_str) = query.get("uuid") { match Uuid::parse_str(uuid_str) { @@ -258,7 +262,8 @@ async fn index_process( // store tera template in application state async fn index(tmpl: web::Data) -> Result { // TODO: add option to prefill link with parameter - println!("serving index page"); + let now = >>::into(SystemTime::now()).to_rfc3339(); + println!("{now} serving index page"); let s = tmpl .render("index.html", &tera::Context::new()) .map_err(|_| error::ErrorInternalServerError("Template error"))?; @@ -303,6 +308,7 @@ async fn main() -> std::io::Result<()> { let tera = Tera::new("templates/**/*.html").unwrap(); App::new() + .wrap(Logger::new("%a %{User-Agent}i")) .app_data(Data::new(db_pool.clone())) // pass database pool to application so we can access it inside handlers .app_data(Data::new(tera)) .app_data(Data::new(conf.clone())) diff --git a/src/model.rs b/src/model.rs index 1d605ab..a8d4546 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,9 +1,8 @@ use actix_web::web; use chrono::DateTime; use chrono::Utc; -use std::time::SystemTime; - use sqlx::{Pool, Sqlite}; +use std::time::SystemTime; // Change to strings if to much headache pub struct Link { @@ -16,8 +15,9 @@ impl Link { uuid: String, pool: web::Data>, ) -> Result { + let now = >>::into(SystemTime::now()).to_rfc3339(); let mut tx = pool.begin().await?; - println!("find by uuid {uuid}"); + println!("{now} find by uuid {uuid}"); let rec = sqlx::query!( r#" SELECT * FROM links WHERE uuid = $1 @@ -27,10 +27,6 @@ impl Link { .fetch_one(&mut *tx) .await?; - let now = SystemTime::now(); - let now: DateTime = now.into(); - let now = now.to_rfc3339(); - sqlx::query!( r#" UPDATE links SET last_used = $1 WHERE uuid = $2"#, now, @@ -52,8 +48,8 @@ impl Link { .bind(&link.destination) .execute(&mut *tx) .await?; - - println!("update uuid {}", link.uuid); + let now = >>::into(SystemTime::now()).to_rfc3339(); + println!("{} update uuid {}", now, link.uuid); tx.commit().await?; Ok(link) } @@ -64,8 +60,8 @@ impl Link { .bind(&link.destination) .execute(&mut *tx) .await?; - - println!("create uuid {}", link.uuid); + let now = >>::into(SystemTime::now()).to_rfc3339(); + println!("{} create uuid {}", now, link.uuid); tx.commit().await?; Ok(link) }