From 47e46a6e00c47622e99cfd65083887b3dce02716 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 14 Jan 2018 15:20:03 +0100 Subject: Improve error message with missing database url. --- src/main.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index bdb8524..4e96a65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,28 +18,26 @@ mod schema; mod controllers; use controllers::home_controller; use controllers::posts_controller; -use dotenv::dotenv; -use std::env; fn main() { - dotenv().expect("No .env file found."); - - let dburl = env::var("DATABASE_URL") - .expect("DATABASE_URL environment variable must be set"); - - rocket::ignite() - .manage(utils::init_db_pool(&dburl)) - .mount("/", - routes![ - home_controller::index, - home_controller::index_json]) - .mount("/posts", - routes![ - posts_controller::new, - posts_controller::create, - posts_controller::show, - posts_controller::edit, - posts_controller::update, - posts_controller::delete]) - .launch(); + if let Ok(dburl) = dotenv::var("DATABASE_URL") { + rocket::ignite() + .manage(utils::init_db_pool(&dburl)) + .mount("/", + routes![ + home_controller::index, + home_controller::index_json]) + .mount("/posts", + routes![ + posts_controller::new, + posts_controller::create, + posts_controller::show, + posts_controller::edit, + posts_controller::update, + posts_controller::delete]) + .launch(); + } + else { + eprintln!("Error: No database specified, make sure the DATABASE_URL env var is set."); + }; } -- cgit v1.2.3