From fcef2fa1f1f33edaeefd3d852eb3f8489b96c495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Sat, 12 May 2018 22:55:18 +0200 Subject: [PATCH] Check that the database folder exists before connecting If the parent folder ('data' by default) doesn't exist, the database won't be able to connect. --- src/main.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index e951ff1..cbc4d0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ extern crate dotenv; #[macro_use] extern crate lazy_static; -use std::{io, env}; +use std::{io, env, path::Path, process::{exit, Command}}; use rocket::Rocket; #[macro_use] @@ -51,22 +51,34 @@ fn init_rocket() -> Rocket { embed_migrations!(); fn main() { + check_db(); + check_rsa_keys(); + check_web_vault(); + // Make sure the database is up to date (create if it doesn't exist, or run the migrations) let connection = db::get_connection().expect("Can't conect to DB"); embedded_migrations::run_with_output(&connection, &mut io::stdout()).expect("Can't run migrations"); - check_rsa_keys(); - check_web_vault(); - init_rocket().launch(); } +fn check_db() { + let path = Path::new(&CONFIG.database_url); + + if let Some(parent) = path.parent() { + use std::fs; + if fs::create_dir_all(parent).is_err() { + println!("Error creating database directory"); + exit(1); + } + } +} + fn check_rsa_keys() { // If the RSA keys don't exist, try to create them if !util::file_exists(&CONFIG.private_rsa_key) || !util::file_exists(&CONFIG.public_rsa_key) { println!("JWT keys don't exist, checking if OpenSSL is available..."); - use std::process::{exit, Command}; Command::new("openssl") .arg("version") @@ -108,9 +120,6 @@ fn check_rsa_keys() { } fn check_web_vault() { - use std::path::Path; - use std::process::exit; - let index_path = Path::new(&CONFIG.web_vault_folder).join("index.html"); if !index_path.exists() {