Merge pull request 'Remove Baseurl Env var and add more flexible db config' (#3) from fionera/datatrash:master into master

Reviewed-on: https://gitea.ctdo.de/neri/datatrash/pulls/3
This commit is contained in:
neri 2020-08-02 23:12:25 +00:00
commit 180bc08cc5
3 changed files with 35 additions and 21 deletions

View File

@ -17,11 +17,6 @@ RUN cargo install --path . --features vendored
FROM alpine 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" ENV RUST_BACKTRACE "1"
COPY --from=build /home/rust/.cargo/bin/datatrash . COPY --from=build /home/rust/.cargo/bin/datatrash .

View File

@ -4,13 +4,7 @@ mod multipart;
use actix_files::{Files, NamedFile}; use actix_files::{Files, NamedFile};
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::{ use actix_web::{error, http::header::{ContentDisposition, DispositionParam, DispositionType}, middleware, web::{self, Bytes}, App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer};
error,
http::header::{ContentDisposition, DispositionParam, DispositionType},
middleware,
web::{self, Bytes},
App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer,
};
use async_std::{ use async_std::{
fs, fs,
path::PathBuf, path::PathBuf,
@ -79,10 +73,10 @@ async fn upload(
.finish()) .finish())
} }
async fn uploaded(id: web::Path<String>, config: web::Data<Config>) -> Result<HttpResponse, Error> { async fn uploaded(id: web::Path<String>, req: web::HttpRequest) -> Result<HttpResponse, Error> {
let url = req.url_for("file", &[id.as_str()])?;
let upload_html = UPLOAD_HTML let upload_html = UPLOAD_HTML
.replace("{id}", id.as_ref()) .replace("{url}", url.as_str());
.replace("{server}", &config.server_url);
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/html") .content_type("text/html")
.body(upload_html)) .body(upload_html))
@ -130,11 +124,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 { async fn setup_db() -> PgPool {
let conn_url = &get_db_url();
log::info!("Using Connection string {}", conn_url);
let pool = PgPool::builder() let pool = PgPool::builder()
.max_size(5) .max_size(5)
.connect_timeout(std::time::Duration::from_secs(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 .await
.expect("could not create db pool"); .expect("could not create db pool");
@ -148,7 +169,6 @@ async fn setup_db() -> PgPool {
#[derive(Clone)] #[derive(Clone)]
struct Config { struct Config {
server_url: String,
files_dir: PathBuf, files_dir: PathBuf,
} }
@ -161,7 +181,6 @@ async fn main() -> std::io::Result<()> {
let pool: PgPool = setup_db().await; let pool: PgPool = setup_db().await;
let config = Config { 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())), files_dir: PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())),
}; };
fs::create_dir_all(&config.files_dir) fs::create_dir_all(&config.files_dir)
@ -196,7 +215,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/").route(web::get().to(index))) .service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/upload").route(web::post().to(upload))) .service(web::resource("/upload").route(web::post().to(upload)))
.service(web::resource("/upload/{id}").route(web::get().to(uploaded))) .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()) .service(Files::new("/static", "static").disable_content_disposition())
} }
}) })

View File

@ -11,8 +11,8 @@
<h1><a href="/">datatrash</a></h1> <h1><a href="/">datatrash</a></h1>
<p> <p>
Datei-Link: Datei-Link:
<a href="{server}/file/{id}"> <a href="{url}">
{server}/files/{id} {url}
</a> </a>
</p> </p>
</main> </main>