add last used fieldi and some logging

This commit is contained in:
2024-04-25 22:29:10 +02:00
parent 5964a1ddad
commit 18beebfdd0
7 changed files with 505 additions and 372 deletions

831
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -13,4 +13,5 @@ reqwest = { version = "0.11", features = ["blocking"] }
tera = "1.15" tera = "1.15"
dotenv = "0.15" dotenv = "0.15"
sqlx = { version = "0.5", features = [ "sqlite", "runtime-actix-rustls" ] } sqlx = { version = "0.5", features = ["sqlite", "runtime-actix-rustls"] }
chrono = "0.4.38"

View File

@@ -1,8 +1,10 @@
FROM rust:1.72 as builder FROM rust:1.77 as builder
RUN apt-get update && apt-get install -y sqlite3 && rm -rf /var/lib/apt/lists/* RUN apt-get update && apt-get install -y sqlite3 && rm -rf /var/lib/apt/lists/*
RUN cargo install sqlx-cli
WORKDIR /usr/src/ics-proxy WORKDIR /usr/src/ics-proxy
COPY . . COPY . .
RUN cd db && ./create_db.sh RUN sqlx database create
RUN sqlx migrate run
RUN cargo install --path . RUN cargo install --path .
FROM debian:stable-slim FROM debian:stable-slim

View File

@@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS LINKS(
UUID TEXT NOT NULL PRIMARY KEY,
DESTINATION TEXT NOT NULL
);

View File

@@ -0,0 +1,2 @@
-- Add migration script here
ALTER TABLE LINKS ADD COLUMN last_used TEXT;

View File

@@ -23,7 +23,7 @@ struct Config {
async fn make_ics_request(req: HttpRequest, db_pool: web::Data<Pool<Sqlite>>) -> impl Responder { async fn make_ics_request(req: HttpRequest, db_pool: web::Data<Pool<Sqlite>>) -> impl Responder {
let id = req.match_info().get("id").unwrap_or(""); let id = req.match_info().get("id").unwrap_or("");
println!("serving ics request");
match Uuid::parse_str(id) { match Uuid::parse_str(id) {
Ok(uuid) => match Link::find_by_uuid(uuid.to_string(), db_pool).await { Ok(uuid) => match Link::find_by_uuid(uuid.to_string(), db_pool).await {
Ok(link) => match reqwest::get(link.destination).await { Ok(link) => match reqwest::get(link.destination).await {
@@ -60,6 +60,7 @@ async fn edit_page(
db_pool: web::Data<Pool<Sqlite>>, db_pool: web::Data<Pool<Sqlite>>,
conf: web::Data<Config>, conf: web::Data<Config>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
println!("serving edit page");
// one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78 // one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78
if let Some(uuid_str) = query.get("uuid") { if let Some(uuid_str) = query.get("uuid") {
match Uuid::parse_str(uuid_str) { match Uuid::parse_str(uuid_str) {
@@ -257,7 +258,7 @@ async fn index_process(
// store tera template in application state // store tera template in application state
async fn index(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> { async fn index(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
// TODO: add option to prefill link with parameter // TODO: add option to prefill link with parameter
println!("serving index page");
let s = tmpl let s = tmpl
.render("index.html", &tera::Context::new()) .render("index.html", &tera::Context::new())
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|_| error::ErrorInternalServerError("Template error"))?;
@@ -292,9 +293,7 @@ async fn main() -> std::io::Result<()> {
.await .await
.expect("could not create db pool"); .expect("could not create db pool");
sqlx::migrate!("./migrations") sqlx::migrate!("./migrations").run(&db_pool).await.unwrap();
.run(&db_pool)
.await.unwrap();
println!( println!(
"Listening on: {}://{}, open browser and visit have a try!", "Listening on: {}://{}, open browser and visit have a try!",

View File

@@ -1,4 +1,8 @@
use actix_web::web; use actix_web::web;
use chrono::DateTime;
use chrono::Utc;
use std::time::SystemTime;
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
// Change to strings if to much headache // Change to strings if to much headache
@@ -13,6 +17,7 @@ impl Link {
pool: web::Data<Pool<Sqlite>>, pool: web::Data<Pool<Sqlite>>,
) -> Result<Link, sqlx::Error> { ) -> Result<Link, sqlx::Error> {
let mut tx = pool.begin().await?; let mut tx = pool.begin().await?;
println!("find by uuid {uuid}");
let rec = sqlx::query!( let rec = sqlx::query!(
r#" r#"
SELECT * FROM links WHERE uuid = $1 SELECT * FROM links WHERE uuid = $1
@@ -22,6 +27,19 @@ impl Link {
.fetch_one(&mut tx) .fetch_one(&mut tx)
.await?; .await?;
let now = SystemTime::now();
let now: DateTime<Utc> = now.into();
let now = now.to_rfc3339();
sqlx::query!(
r#" UPDATE links SET last_used = $1 WHERE uuid = $2"#,
now,
uuid,
)
.execute(&mut tx)
.await?;
tx.commit().await?;
Ok(Link { Ok(Link {
uuid: rec.UUID, uuid: rec.UUID,
destination: rec.DESTINATION, destination: rec.DESTINATION,
@@ -35,6 +53,7 @@ impl Link {
.execute(&mut tx) .execute(&mut tx)
.await?; .await?;
println!("update uuid {}", link.uuid);
tx.commit().await?; tx.commit().await?;
Ok(link) Ok(link)
} }
@@ -46,6 +65,7 @@ impl Link {
.execute(&mut tx) .execute(&mut tx)
.await?; .await?;
println!("create uuid {}", link.uuid);
tx.commit().await?; tx.commit().await?;
Ok(link) Ok(link)
} }