mirror of
https://github.com/13hannes11/ics-proxy.git
synced 2024-09-06 08:01:41 +02:00
add last used fieldi and some logging
This commit is contained in:
831
Cargo.lock
generated
831
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -13,4 +13,5 @@ reqwest = { version = "0.11", features = ["blocking"] }
|
||||
tera = "1.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"
|
||||
|
||||
@@ -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 cargo install sqlx-cli
|
||||
WORKDIR /usr/src/ics-proxy
|
||||
COPY . .
|
||||
RUN cd db && ./create_db.sh
|
||||
RUN sqlx database create
|
||||
RUN sqlx migrate run
|
||||
RUN cargo install --path .
|
||||
|
||||
FROM debian:stable-slim
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS LINKS(
|
||||
UUID TEXT NOT NULL PRIMARY KEY,
|
||||
DESTINATION TEXT NOT NULL
|
||||
);
|
||||
2
migrations/20240425193610_add_last_used_column.sql
Normal file
2
migrations/20240425193610_add_last_used_column.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
-- Add migration script here
|
||||
ALTER TABLE LINKS ADD COLUMN last_used TEXT;
|
||||
11
src/main.rs
11
src/main.rs
@@ -23,7 +23,7 @@ struct Config {
|
||||
|
||||
async fn make_ics_request(req: HttpRequest, db_pool: web::Data<Pool<Sqlite>>) -> impl Responder {
|
||||
let id = req.match_info().get("id").unwrap_or("");
|
||||
|
||||
println!("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,6 +60,7 @@ async fn edit_page(
|
||||
db_pool: web::Data<Pool<Sqlite>>,
|
||||
conf: web::Data<Config>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
println!("serving edit page");
|
||||
// one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78
|
||||
if let Some(uuid_str) = query.get("uuid") {
|
||||
match Uuid::parse_str(uuid_str) {
|
||||
@@ -257,7 +258,7 @@ async fn index_process(
|
||||
// store tera template in application state
|
||||
async fn index(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
|
||||
// TODO: add option to prefill link with parameter
|
||||
|
||||
println!("serving index page");
|
||||
let s = tmpl
|
||||
.render("index.html", &tera::Context::new())
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
|
||||
@@ -292,10 +293,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.await
|
||||
.expect("could not create db pool");
|
||||
|
||||
sqlx::migrate!("./migrations")
|
||||
.run(&db_pool)
|
||||
.await.unwrap();
|
||||
|
||||
sqlx::migrate!("./migrations").run(&db_pool).await.unwrap();
|
||||
|
||||
println!(
|
||||
"Listening on: {}://{}, open browser and visit have a try!",
|
||||
protocol, base_url
|
||||
|
||||
20
src/model.rs
20
src/model.rs
@@ -1,4 +1,8 @@
|
||||
use actix_web::web;
|
||||
use chrono::DateTime;
|
||||
use chrono::Utc;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
// Change to strings if to much headache
|
||||
@@ -13,6 +17,7 @@ impl Link {
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
) -> Result<Link, sqlx::Error> {
|
||||
let mut tx = pool.begin().await?;
|
||||
println!("find by uuid {uuid}");
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
SELECT * FROM links WHERE uuid = $1
|
||||
@@ -22,6 +27,19 @@ impl Link {
|
||||
.fetch_one(&mut tx)
|
||||
.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 {
|
||||
uuid: rec.UUID,
|
||||
destination: rec.DESTINATION,
|
||||
@@ -35,6 +53,7 @@ impl Link {
|
||||
.execute(&mut tx)
|
||||
.await?;
|
||||
|
||||
println!("update uuid {}", link.uuid);
|
||||
tx.commit().await?;
|
||||
Ok(link)
|
||||
}
|
||||
@@ -46,6 +65,7 @@ impl Link {
|
||||
.execute(&mut tx)
|
||||
.await?;
|
||||
|
||||
println!("create uuid {}", link.uuid);
|
||||
tx.commit().await?;
|
||||
Ok(link)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user