From b03af4e833d73c1bbcbd14d5ebd3668c18e62053 Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Fri, 3 Sep 2021 13:29:41 +0200 Subject: [PATCH] add .env support and linking of full base url --- .env | 4 +++- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 28 +++++++++++++++++++++++++++- templates/edit.html | 2 +- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 235d2a8..70274a8 100644 --- a/.env +++ b/.env @@ -1 +1,3 @@ -DATABASE_URL=sqlite://db/db.db \ No newline at end of file +DATABASE_URL=sqlite://db/db.db +PROTOCOL=http +BASE_URL=localhost:8080 \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index b99d836..a6b6602 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1129,6 +1129,7 @@ name = "ics-proxy" version = "0.1.0" dependencies = [ "actix-web", + "dotenv", "reqwest", "sqlx", "tera", diff --git a/Cargo.toml b/Cargo.toml index 97eb62d..e78a7d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,6 @@ uuid = { version = "0.8.2", features = ["v4"] } url = "2.2" reqwest = { version = "0.11", features = ["blocking"] } tera = "1.12.1" +dotenv = "0.15" sqlx = { version = "0.4.2", features = [ "sqlite", "runtime-actix-native-tls" ] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f0bfff0..28b2a91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,20 @@ use sqlx::{Pool, Sqlite, SqlitePool}; use tera::Tera; use uuid::Uuid; +extern crate dotenv; +use dotenv::dotenv; +use std::env; + mod model; use model::Link; const REDIRECT_TIMEOUT_S: i32 = 2; +#[derive(Clone)] +struct CONFIG { + root: String, +} + async fn make_ics_request(req: HttpRequest) -> impl Responder { let id = req.match_info().get("id").unwrap_or(""); @@ -46,6 +55,7 @@ async fn edit_page( tmpl: web::Data, query: web::Query>, db_pool: web::Data>, + conf: web::Data, ) -> Result { // one uuid: 9228c1a4-8956-4f1c-8b5f-53cc575bd78 if let Some(uuid_str) = query.get("uuid") { @@ -55,6 +65,7 @@ async fn edit_page( let mut ctx = tera::Context::new(); ctx.insert("link", &link.destination); ctx.insert("uuid", &link.uuid); + ctx.insert("root", &conf.root); let s = tmpl .render("edit.html", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error"))?; @@ -212,7 +223,21 @@ async fn index(tmpl: web::Data) -> Result { async fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"); - let database_url = "sqlite://db/db.db"; //env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file"); + dotenv().ok(); + + let database_url = match std::env::var("DATABASE_URL") { + Ok(var) => var, + Err(e) => panic!("{}", e.to_string()), + }; + let protocol = + std::env::var("PROTOCOL").expect("PROTOCOL environemt variable error, make sure it is set"); + let base_url = + std::env::var("BASE_URL").expect("BASE_URL environemt variable error, make sure it is set"); + + let conf = CONFIG { + root: format!("{}://{}", protocol, base_url), + }; + let db_pool = SqlitePool::connect(&database_url) .await .expect("could not create db pool"); @@ -224,6 +249,7 @@ async fn main() -> std::io::Result<()> { App::new() .data(db_pool.clone()) // pass database pool to application so we can access it inside handlers .data(tera) + .data(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))) diff --git a/templates/edit.html b/templates/edit.html index 43149f6..ec94d48 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -7,7 +7,7 @@ -

Hi, your link is /{{ uuid }}/events.ics!

+

Hi, your link is {{ root }}/{{ uuid }}/events.ics!

it takes the data from