From ee91354de81f1ae8ea913be7b6727b820b20d4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Fri, 22 Jul 2022 15:36:08 +0200 Subject: [PATCH] Add binary options --- Cargo.lock | 1 + server/Cargo.toml | 1 + server/assets/templates/nav.html | 2 +- server/src/main.rs | 30 ++++++++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7175a0a..3fc7d31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1351,6 +1351,7 @@ dependencies = [ "base64", "byteorder", "chrono", + "dotenv", "futures", "futures-util", "gumdrop", diff --git a/server/Cargo.toml b/server/Cargo.toml index 428647c..2787ab7 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -17,6 +17,7 @@ argon2 = { version = "0.4.1" } askama = { version = "*", features = ["serde-json"] } byteorder = { version = "1.4.3" } chrono = { version = "*", features = ["serde"] } +dotenv = { version = "0.15.0" } futures = { version = "0.3.21", features = ["async-await", "std"] } futures-util = { version = "0.3.21", features = [] } gumdrop = { version = "*" } diff --git a/server/assets/templates/nav.html b/server/assets/templates/nav.html index d0af1b5..e7b4c45 100644 --- a/server/assets/templates/nav.html +++ b/server/assets/templates/nav.html @@ -48,7 +48,6 @@
Moje usługi
- {%- endif %} @@ -60,6 +59,7 @@ /> + {%- endif %} {% if h.is_admin(account) -%} diff --git a/server/src/main.rs b/server/src/main.rs index bbd8c74..248b947 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -14,14 +14,40 @@ mod routes; mod utils; pub mod view; +#[derive(gumdrop::Options)] +struct Opts { + help: bool, + port: Option, + bind: Option, + db: Option, +} + #[actix_web::main] async fn main() -> std::io::Result<()> { + let opts: Opts = gumdrop::Options::parse_args_default_or_exit(); + dotenv::dotenv().ok(); tracing_subscriber::fmt::init(); + let db_addr = std::env::var("DATABASE_URL") + .ok() + .or(opts.db) + .expect("No database connection url"); + let addr = ( + std::env::var("BIND") + .ok() + .or(opts.bind) + .unwrap_or_else(|| String::from("0.0.0.0")), + std::env::var("PORT") + .ok() + .and_then(|s| s.parse().ok()) + .or_else(|| opts.port.and_then(|s| s.parse().ok())) + .unwrap_or(8080), + ); + let pool = sqlx::postgres::PgPoolOptions::new() .max_connections(8) .min_connections(8) - .connect(&std::env::var("DATABASE_URL").expect("No database connection url")) + .connect(&db_addr) .await .unwrap(); @@ -41,7 +67,7 @@ async fn main() -> std::io::Result<()> { .configure(routes::configure) .default_service(web::to(render_index)) }) - .bind(("0.0.0.0", 8080))? + .bind(addr)? .run() .await }