diff options
-rw-r--r-- | Cargo.lock | 119 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rwxr-xr-x | bin/dev-server | 9 | ||||
-rw-r--r-- | src/main.rs | 30 | ||||
-rw-r--r-- | src/model/config.rs | 42 |
5 files changed, 52 insertions, 150 deletions
@@ -60,55 +60,6 @@ dependencies = [ ] [[package]] -name = "anstream" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is-terminal", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" - -[[package]] -name = "anstyle-parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "anstyle-wincon" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c677ab05e09154296dd37acecd46420c17b9713e8366facafa8fc0885167cf4c" -dependencies = [ - "anstyle", - "windows-sys", -] - -[[package]] name = "atoi" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -213,7 +164,6 @@ version = "0.1.0" dependencies = [ "bcrypt", "chrono", - "clap", "env_logger", "hex", "hmac", @@ -231,7 +181,6 @@ dependencies = [ "tokio", "tokio-util", "url", - "uuid", ] [[package]] @@ -315,53 +264,6 @@ dependencies = [ ] [[package]] -name = "clap" -version = "4.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c27cdf28c0f604ba3f512b0c9a409f8de8513e4816705deb0498b627e7c3a3fd" -dependencies = [ - "clap_builder", - "clap_derive", - "once_cell", -] - -[[package]] -name = "clap_builder" -version = "4.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08a9f1ab5e9f01a9b81f202e8562eb9a10de70abf9eaeac1be465c28b75aa4aa" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_derive" -version = "4.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 2.0.28", -] - -[[package]] -name = "clap_lex" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" - -[[package]] -name = "colorchoice" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" - -[[package]] name = "const-oid" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1927,12 +1829,6 @@ dependencies = [ ] [[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - -[[package]] name = "subtle" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2282,21 +2178,6 @@ dependencies = [ ] [[package]] -name = "utf8parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" - -[[package]] -name = "uuid" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" -dependencies = [ - "getrandom", -] - -[[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -7,7 +7,6 @@ edition = "2021" [dependencies] bcrypt = "0.15" chrono = "0.4" -clap = { version = "4.3", features = ["derive"] } env_logger = "0.10" hmac = "0.12" hex = "0.4" @@ -25,4 +24,3 @@ tera = { version = "1.19", features = ["builtins"] } tokio = { version = "1", features = ["full"] } tokio-util = { version = "0.7", features = ["codec"] } url = "2.4" -uuid = { version = "1.4", features = ["v4"] } diff --git a/bin/dev-server b/bin/dev-server index 0669d6d..195791c 100755 --- a/bin/dev-server +++ b/bin/dev-server @@ -4,7 +4,14 @@ cd $(dirname "$0")/.. CMD=${1:-run} -RUST_LOG=budget=info cargo-watch \ +export RUST_LOG="budget=info" +export AUTH_SECRET="pMkW8PR7bjhc24bbvZLIEHfcooLzrGIh" +export DB_PATH="database.db" +export MOCK_MAILS="true" +export SECURE_COOKIES="false" +export SOCKET_ADDRESS="0.0.0.0:3000" + +cargo-watch \ --ignore README.md \ --ignore bin \ --ignore sql \ diff --git a/src/main.rs b/src/main.rs index 74917a9..65d52df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ -use clap::Parser; use hyper::service::{make_service_fn, service_fn}; use hyper::Server; use sqlx::sqlite::SqlitePool; use std::convert::Infallible; -use std::net::SocketAddr; #[macro_use] extern crate log; @@ -22,20 +20,7 @@ mod templates; mod utils; mod validation; -use model::config::Config; - -#[derive(Parser)] -#[clap()] -struct Args { - #[clap(short, long, default_value = "0.0.0.0:3000")] - address: SocketAddr, - - #[clap(short, long, default_value = "config.json")] - config: String, - - #[clap(short, long, default_value = "database.db")] - database: String, -} +use model::config; #[tokio::main] async fn main() { @@ -44,13 +29,10 @@ async fn main() { ) .init(); - let args = Args::parse(); - - let config_str = std::fs::read_to_string(&args.config) - .unwrap_or_else(|_| panic!("{}", format!("Missing {}", args.config))); - let config: Config = serde_json::from_str(&config_str).unwrap(); + let config = config::from_env() + .unwrap_or_else(|err| panic!("Error reading config: {err}")); - let pool = SqlitePool::connect(&format!("sqlite:{}", args.database)) + let pool = SqlitePool::connect(&format!("sqlite:{}", config.db_path)) .await .unwrap(); @@ -78,8 +60,8 @@ async fn main() { } }); - info!("Starting server at {}", args.address); - if let Err(e) = Server::bind(&args.address).serve(make_svc).await { + info!("Starting server at {}", config.socket_address); + if let Err(e) = Server::bind(&config.socket_address).serve(make_svc).await { error!("server error: {}", e); } } diff --git a/src/model/config.rs b/src/model/config.rs index e69b4c4..1fa5bb4 100644 --- a/src/model/config.rs +++ b/src/model/config.rs @@ -1,8 +1,42 @@ -use serde::Deserialize; +use std::env; +use std::net::SocketAddr; +use std::str::FromStr; -#[derive(Clone, Deserialize)] +#[derive(Clone)] pub struct Config { - pub secure_cookies: bool, - pub mock_mails: bool, pub auth_secret: String, + pub db_path: String, + pub mock_mails: bool, + pub secure_cookies: bool, + pub socket_address: SocketAddr, +} + +pub fn from_env() -> Result<Config, String> { + Ok(Config { + auth_secret: read_string("AUTH_SECRET")?, + db_path: read_string("DB_PATH")?, + mock_mails: read_bool("MOCK_MAILS")?, + secure_cookies: read_bool("SECURE_COOKIES")?, + socket_address: read_socket_address("SOCKET_ADDRESS")?, + }) +} + +fn read_socket_address(key: &str) -> Result<SocketAddr, String> { + SocketAddr::from_str(&read_string(key)?).map_err(|err| { + format!("environment variable '{key}' is not a socket address: {err}") + }) +} + +fn read_bool(key: &str) -> Result<bool, String> { + read_string(key).and_then(|v| match v.as_str() { + "true" => Ok(true), + "false" => Ok(false), + _ => Err(format!( + "environment variable '{key}' is not a boolean: '{v}'" + )), + }) +} + +fn read_string(key: &str) -> Result<String, String> { + env::var(key).map_err(|_| format!("missing environment variable '{key}'")) } |