From 9c3a341e79581541fc39ceddeec28daae7dc28cf Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 20 Mar 2019 20:23:06 +0100 Subject: Pass database url into build_rocket helper function. --- src/lib.rs | 5 +---- src/main.rs | 5 ++++- tests/room_tests.rs | 5 ++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7929ab2..989a6ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,10 +26,7 @@ use std::collections::HashMap; use std::error::Error; use std::result::Result; -pub fn build_rocket() -> Result> { - let db_url = dotenv::var("DATABASE_URL") - .map_err(|_| "No database! Set DATABASE_URL env var and try again.")?; - +pub fn build_rocket(db_url: &str) -> Result> { let mut db_config = HashMap::new(); db_config.insert("url", rocket::config::Value::from(db_url)); diff --git a/src/main.rs b/src/main.rs index fdc8ff3..773fc66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,9 @@ use std::error::Error; use std::result::Result; fn main() -> Result<(), Box> { - ramaskrik::build_rocket()?.launch(); + let db_url = dotenv::var("DATABASE_URL") + .map_err(|_| "No database! Set DATABASE_URL env var and try again.")?; + + ramaskrik::build_rocket(&db_url)?.launch(); Ok(()) } diff --git a/tests/room_tests.rs b/tests/room_tests.rs index c91519d..292954a 100644 --- a/tests/room_tests.rs +++ b/tests/room_tests.rs @@ -21,7 +21,10 @@ use rocket::http::ContentType; #[test] fn getting_rooms_from_api() { - let server = ramaskrik::build_rocket().unwrap(); + let db_url = dotenv::var("DATABASE_URL") + .map_err(|_| "No database! Set DATABASE_URL env var and try again.").unwrap(); + + let server = ramaskrik::build_rocket(&db_url).unwrap(); let client = rocket::local::Client::new(server).unwrap(); let mut response = client.get("/rooms").dispatch(); assert_eq!(response.content_type(), Some(ContentType::JSON)); -- cgit v1.2.3