mirror of
https://github.com/13hannes11/ics-proxy.git
synced 2024-09-06 08:01:41 +02:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ede6aaf8aa | |||
| a5b36ab0fb | |||
| c6afed3b11 | |||
| 2c4c78125e | |||
|
|
365bb52772 | ||
|
|
4bf9f20fd5 | ||
|
|
884fa56545 | ||
| 18beebfdd0 | |||
| 5964a1ddad | |||
|
|
98d5539fdd | ||
|
|
c08bd57c89 | ||
|
|
a45d4cef1f | ||
|
|
dc854e9a4e | ||
|
|
5011696de9 | ||
|
|
fc52e31ef6 | ||
|
|
799d310815 | ||
|
|
9e02170c0e | ||
| 7e6cfa7f8f | |||
|
|
bdf96b366a | ||
|
|
569e7f71ef | ||
|
|
41234a1fac | ||
|
|
0064698574 | ||
|
|
271af82794 | ||
|
|
776dccb15c | ||
|
|
64b416e8b6 | ||
|
|
977110fa99 | ||
|
|
a701eb9a76 | ||
|
|
b3aeef646a |
2228
Cargo.lock
generated
2228
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
16
Cargo.toml
16
Cargo.toml
@@ -1,16 +1,16 @@
|
||||
[package]
|
||||
name = "ics-proxy"
|
||||
version = "0.1.2"
|
||||
version = "0.1.5"
|
||||
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"
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
FROM rust:1.61 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 +1,5 @@
|
||||
-- Add migration script here
|
||||
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;
|
||||
23
src/main.rs
23
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,11 +25,12 @@ 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("");
|
||||
|
||||
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::blocking::get(link.destination) {
|
||||
Ok(r) => match r.text() {
|
||||
Ok(link) => match reqwest::get(link.destination).await {
|
||||
Ok(r) => match r.text().await {
|
||||
Ok(res) => HttpResponse::Ok().content_type("text/calendar").body(res),
|
||||
Err(err) => HttpResponse::Ok()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
@@ -60,6 +63,8 @@ async fn edit_page(
|
||||
db_pool: web::Data<Pool<Sqlite>>,
|
||||
conf: web::Data<Config>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
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) {
|
||||
@@ -257,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
|
||||
|
||||
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"))?;
|
||||
@@ -292,6 +298,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.await
|
||||
.expect("could not create db pool");
|
||||
|
||||
sqlx::migrate!("./migrations").run(&db_pool).await.unwrap();
|
||||
|
||||
println!(
|
||||
"Listening on: {}://{}, open browser and visit have a try!",
|
||||
protocol, base_url
|
||||
@@ -300,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,5 +1,8 @@
|
||||
use actix_web::web;
|
||||
use chrono::DateTime;
|
||||
use chrono::Utc;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::time::SystemTime;
|
||||
|
||||
// Change to strings if to much headache
|
||||
pub struct Link {
|
||||
@@ -12,16 +15,27 @@ 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!("{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?;
|
||||
|
||||
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,
|
||||
@@ -32,9 +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?;
|
||||
|
||||
let now = <SystemTime as Into<DateTime<Utc>>>::into(SystemTime::now()).to_rfc3339();
|
||||
println!("{} update uuid {}", now, link.uuid);
|
||||
tx.commit().await?;
|
||||
Ok(link)
|
||||
}
|
||||
@@ -43,9 +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?;
|
||||
|
||||
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