mirror of
https://github.com/13hannes11/ics-proxy.git
synced 2024-09-06 08:01:41 +02:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77d0251f57 | ||
| ede6aaf8aa | |||
| a5b36ab0fb | |||
| c6afed3b11 | |||
| 2c4c78125e | |||
|
|
365bb52772 | ||
|
|
4bf9f20fd5 | ||
|
|
884fa56545 |
1430
Cargo.lock
generated
1430
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
15
Cargo.toml
@@ -1,17 +1,16 @@
|
||||
[package]
|
||||
name = "ics-proxy"
|
||||
version = "0.1.2"
|
||||
version = "0.1.6"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.0"
|
||||
uuid = { version = "1.0", features = ["v4"] }
|
||||
url = "2.2"
|
||||
reqwest = { version = "0.11", features = ["blocking"] }
|
||||
tera = "1.15"
|
||||
actix-web = "4.8"
|
||||
uuid = { version = "1.10", features = ["v4"] }
|
||||
url = "2.5"
|
||||
reqwest = { version = "0.12", features = ["blocking"] }
|
||||
tera = "1.20"
|
||||
dotenv = "0.15"
|
||||
|
||||
sqlx = { version = "0.5", features = ["sqlite", "runtime-actix-rustls"] }
|
||||
sqlx = { version = "0.8", features = ["sqlite", "any", "tls-rustls", "runtime-tokio"] }
|
||||
chrono = "0.4.38"
|
||||
|
||||
18
src/main.rs
18
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<Pool<Sqlite>>) -> impl Responder {
|
||||
let id = req.match_info().get("id").unwrap_or("");
|
||||
println!("serving ics request");
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::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<Pool<Sqlite>>,
|
||||
conf: web::Data<Config>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
println!("serving edit page");
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::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<tera::Tera>) -> Result<HttpResponse, Error> {
|
||||
// TODO: add option to prefill link with parameter
|
||||
println!("serving index page");
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::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()))
|
||||
|
||||
26
src/model.rs
26
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,27 +15,24 @@ impl Link {
|
||||
uuid: String,
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
) -> Result<Link, sqlx::Error> {
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::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
|
||||
"#,
|
||||
uuid
|
||||
)
|
||||
.fetch_one(&mut tx)
|
||||
.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)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
|
||||
@@ -50,10 +46,10 @@ impl Link {
|
||||
sqlx::query("UPDATE links SET destination = $2 WHERE uuid = $1;")
|
||||
.bind(&link.uuid)
|
||||
.bind(&link.destination)
|
||||
.execute(&mut tx)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
println!("update uuid {}", link.uuid);
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::into(SystemTime::now()).to_rfc3339();
|
||||
println!("{} update uuid {}", now, link.uuid);
|
||||
tx.commit().await?;
|
||||
Ok(link)
|
||||
}
|
||||
@@ -62,10 +58,10 @@ impl Link {
|
||||
sqlx::query("INSERT INTO links (uuid, destination) VALUES ($1, $2);")
|
||||
.bind(&link.uuid)
|
||||
.bind(&link.destination)
|
||||
.execute(&mut tx)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
println!("create uuid {}", link.uuid);
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::into(SystemTime::now()).to_rfc3339();
|
||||
println!("{} create uuid {}", now, link.uuid);
|
||||
tx.commit().await?;
|
||||
Ok(link)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user