From b512674da9b77048118350940671d6db0da634a7 Mon Sep 17 00:00:00 2001 From: Fionera Date: Sun, 2 Aug 2020 03:08:07 +0200 Subject: [PATCH 1/3] Add Connection string builder --- src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e071a12..b7d29ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,11 +130,38 @@ async fn download( } } +fn get_db_url() -> String { + return env::var("DATABASE_URL").unwrap_or_else(|_| { + let mut url = "postgresql://".to_string(); + + let user = env::var("DATABASE_USER").unwrap_or_default(); + if !user.is_empty() { + url += user.as_str(); + + let pass = env::var("DATABASE_PASS").unwrap_or_default(); + if !pass.is_empty() { + url += ":"; + url += pass.as_str(); + } + + url += "@"; + } + url += env::var("DATABASE_HOST").unwrap_or_else(|_| "localhost".to_string()).as_str(); + url += "/"; + url += env::var("DATABASE_NAME").unwrap_or_else(|_| "datatrash".to_string()).as_str(); + + url.to_string() + }); +} + async fn setup_db() -> PgPool { + let conn_url = &get_db_url(); + log::info!("Using Connection string {}", conn_url); + let pool = PgPool::builder() .max_size(5) .connect_timeout(std::time::Duration::from_secs(5)) - .build(&env::var("DATABASE_URL").unwrap_or_else(|_| "postgresql://localhost".to_owned())) + .build(conn_url) .await .expect("could not create db pool"); -- 2.40.1 From 3c05a725c25ddd87d59e9c8cd223b4d80a06a7cb Mon Sep 17 00:00:00 2001 From: Fionera Date: Sun, 2 Aug 2020 14:31:48 +0200 Subject: [PATCH 2/3] Removing default env args from dockerfile --- Dockerfile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index a8983d3..815f8b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,11 +17,6 @@ RUN cargo install --path . --features vendored FROM alpine -ENV DATABASE_URL "postresql://localhost" -ENV SERVER_URL "http://localhost:8000" -ENV FILES_DIR "./files" -ENV UPLOAD_MAX_BYTES "8388608" -ENV BIND_ADDRESS "0.0.0.0:8000" ENV RUST_BACKTRACE "1" COPY --from=build /home/rust/.cargo/bin/datatrash . -- 2.40.1 From 64be9b7f0cb7f1c2b73a88f2fe4be5e90c550a53 Mon Sep 17 00:00:00 2001 From: Fionera Date: Mon, 3 Aug 2020 00:07:16 +0200 Subject: [PATCH 3/3] Use actix url generator for the uploaded url --- src/main.rs | 18 +++++------------- template/upload.html | 4 ++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index b7d29ad..08acb1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,7 @@ mod multipart; use actix_files::{Files, NamedFile}; use actix_multipart::Multipart; -use actix_web::{ - error, - http::header::{ContentDisposition, DispositionParam, DispositionType}, - middleware, - web::{self, Bytes}, - App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer, -}; +use actix_web::{error, http::header::{ContentDisposition, DispositionParam, DispositionType}, middleware, web::{self, Bytes}, App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer}; use async_std::{ fs, path::PathBuf, @@ -79,10 +73,10 @@ async fn upload( .finish()) } -async fn uploaded(id: web::Path, config: web::Data) -> Result { +async fn uploaded(id: web::Path, req: web::HttpRequest) -> Result { + let url = req.url_for("file", &[id.as_str()])?; let upload_html = UPLOAD_HTML - .replace("{id}", id.as_ref()) - .replace("{server}", &config.server_url); + .replace("{url}", url.as_str()); Ok(HttpResponse::Ok() .content_type("text/html") .body(upload_html)) @@ -175,7 +169,6 @@ async fn setup_db() -> PgPool { #[derive(Clone)] struct Config { - server_url: String, files_dir: PathBuf, } @@ -188,7 +181,6 @@ async fn main() -> std::io::Result<()> { let pool: PgPool = setup_db().await; let config = Config { - server_url: env::var("SERVER_URL").unwrap_or_else(|_| "http://localhost:8000".to_owned()), files_dir: PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())), }; fs::create_dir_all(&config.files_dir) @@ -223,7 +215,7 @@ async fn main() -> std::io::Result<()> { .service(web::resource("/").route(web::get().to(index))) .service(web::resource("/upload").route(web::post().to(upload))) .service(web::resource("/upload/{id}").route(web::get().to(uploaded))) - .service(web::resource("/file/{id}").route(web::get().to(download))) + .service(web::resource("/file/{id}").name("file").route(web::get().to(download))) .service(Files::new("/static", "static").disable_content_disposition()) } }) diff --git a/template/upload.html b/template/upload.html index 7ac8f06..ff1c2a7 100644 --- a/template/upload.html +++ b/template/upload.html @@ -11,8 +11,8 @@

datatrash

Datei-Link: - - {server}/files/{id} + + {url}

-- 2.40.1