mirror of
https://github.com/13hannes11/ics-proxy.git
synced 2024-09-06 08:01:41 +02:00
Compare commits
45 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 | ||
| b89e4af7ac | |||
| 074c194c0c | |||
| af1e32ac24 | |||
| 129481840f | |||
| 16cd3ca123 | |||
| 936c536cc0 | |||
| 4a8f9c1ab8 | |||
| 6bd3ef4537 | |||
| 7eabecf6b4 | |||
| 3d83a6761a | |||
|
|
ba11157687 | ||
| 1aa117a28f | |||
| f59b49b1ad | |||
| 36c01035d5 | |||
|
|
c38971af3a | ||
|
|
5f45e52fb9 | ||
|
|
e804305a68 |
38
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
38
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Desktop (please complete the following information):**
|
||||
- OS: [e.g. iOS]
|
||||
- Browser [e.g. chrome, safari]
|
||||
- Version [e.g. 22]
|
||||
|
||||
**Smartphone (please complete the following information):**
|
||||
- Device: [e.g. iPhone6]
|
||||
- OS: [e.g. iOS8.1]
|
||||
- Browser [e.g. stock browser, safari]
|
||||
- Version [e.g. 22]
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
3196
Cargo.lock
generated
3196
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
18
Cargo.toml
18
Cargo.toml
@@ -1,16 +1,16 @@
|
||||
[package]
|
||||
name = "ics-proxy"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
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 = "3"
|
||||
uuid = { version = "0.8.2", features = ["v4"] }
|
||||
url = "2.2"
|
||||
reqwest = { version = "0.11", features = ["blocking"] }
|
||||
tera = "1.12.1"
|
||||
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.4.2", features = [ "sqlite", "runtime-actix-native-tls" ] }
|
||||
sqlx = { version = "0.8", features = ["sqlite", "any", "tls-rustls", "runtime-tokio"] }
|
||||
chrono = "0.4.38"
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
FROM rust:1.54 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:buster-slim
|
||||
FROM debian:stable-slim
|
||||
RUN apt-get update && apt-get install -y sqlite3 openssl ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||
WORKDIR app
|
||||
COPY --from=builder /usr/local/cargo/bin/ics-proxy ./ics-proxy
|
||||
|
||||
14
README.md
14
README.md
@@ -1,6 +1,13 @@
|
||||
# ics-proxy
|
||||
|
||||
You can find a running instance of this code on [ics-proxy.de](https://ics-proxy.de). The docker image is published on [DockerHub](https://hub.docker.com/repository/docker/13hannes11/ics-proxy).
|
||||
You can find a running instance of this code on [ics-proxy.de](https://ics-proxy.de). The docker image is published on the [GitHub Container Registry](https://github.com/13hannes11/ics-proxy/pkgs/container/ics-proxy).
|
||||
|
||||
## Screenshots
|
||||
|
||||

|
||||

|
||||
|
||||
|
||||
|
||||
## Motivation
|
||||
|
||||
@@ -12,6 +19,11 @@ Additionally, each semester this needed to be done as well. I therefore created
|
||||
|
||||
The easiest way to build this repository is to use docker. You can simply run `docker build -t ics-proxy .`
|
||||
|
||||
Alternatively you can build and run a local dev version with `docker-compose -f docker-compose.dev.yml up`.
|
||||
|
||||
## Deployment
|
||||
|
||||
To deploy you can simply use the `docker-compose.yml` file.
|
||||
|
||||
## Contributing
|
||||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
||||
|
||||
12
docker-compose.dev.yml
Normal file
12
docker-compose.dev.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
version: '3'
|
||||
services:
|
||||
ics-proxy:
|
||||
container_name: ics-proxy
|
||||
build: .
|
||||
env_file: .env
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- database:/app/db
|
||||
volumes:
|
||||
database:
|
||||
@@ -1,3 +1,4 @@
|
||||
-- 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;
|
||||
30
src/main.rs
30
src/main.rs
@@ -2,17 +2,20 @@ use std::collections::HashMap;
|
||||
use url::Url;
|
||||
|
||||
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)]
|
||||
@@ -22,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)
|
||||
@@ -59,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) {
|
||||
@@ -256,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"))?;
|
||||
@@ -291,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
|
||||
@@ -299,9 +308,10 @@ async fn main() -> std::io::Result<()> {
|
||||
let tera = Tera::new("templates/**/*.html").unwrap();
|
||||
|
||||
App::new()
|
||||
.data(db_pool.clone()) // pass database pool to application so we can access it inside handlers
|
||||
.data(tera)
|
||||
.data(conf.clone())
|
||||
.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()))
|
||||
.route("/{id}/events.ics", web::get().to(make_ics_request))
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::resource("/edit").route(web::get().to(edit_page)))
|
||||
|
||||
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