Skip to content

Commit

Permalink
Gracefully exit and error reporting on application startup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsanchezq committed Jul 3, 2020
1 parent 199b8ef commit 48a53f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
10 changes: 3 additions & 7 deletions vit-servicing-station-lib/src/v0/context.rs
Expand Up @@ -21,17 +21,12 @@ impl Context {

pub fn new_shared_context(
db_connection_pool: db::DBConnectionPool,
block0_path: &str,
block0: Vec<u8>,
) -> SharedContext {
let block0 = load_block0(block0_path);
let context = Context::new(db_connection_pool, block0);
Arc::new(RwLock::new(context))
}

pub fn load_block0(block0_path: &str) -> Vec<u8> {
std::fs::read(block0_path).unwrap()
}

#[cfg(test)]
pub mod test {
use super::*;
Expand All @@ -45,6 +40,7 @@ pub mod test {

pub fn new_test_shared_context(db_url: &str, block0_path: &str) -> SharedContext {
let pool = db::load_db_connection_pool(db_url).unwrap();
new_shared_context(pool, block0_path)
let block0 = std::fs::read(block0_path).unwrap();
new_shared_context(pool, block0)
}
}
Expand Up @@ -12,15 +12,15 @@ pub async fn get_genesis(context: SharedContext) -> Result<impl Reply, Rejection
#[cfg(test)]
mod test {
use super::*;
use crate::v0::context::{load_block0, test::new_test_shared_context};
use crate::v0::context::test::new_test_shared_context;
use warp::Filter;

#[tokio::test]
async fn get_block0_succeed() {
// build context
let block0_path = "../resources/tests/block0.bin";
let shared_context = new_test_shared_context("", block0_path);
let block0 = load_block0(block0_path);
let block0 = std::fs::read(block0_path).unwrap();

let with_context = warp::any().map(move || shared_context.clone());

Expand Down
28 changes: 21 additions & 7 deletions vit-servicing-station-server/src/main.rs
Expand Up @@ -10,28 +10,42 @@ async fn main() {
let mut settings: ServiceSettings = ServiceSettings::from_args();
// dump settings and exit if specified
if let Some(settings_file) = &settings.out_settings_file {
server_settings::dump_settings_to_file(settings_file, &settings)
.unwrap_or_else(|e| panic!("Error writing settings to file {}: {}", settings_file, e));
server_settings::dump_settings_to_file(settings_file, &settings).unwrap_or_else(|e| {
println!("Error writing settings to file {}: {}", settings_file, e);
std::process::exit(1)
});
return;
}

// load settings from file if specified
if let Some(settings_file) = &settings.in_settings_file {
settings = server_settings::load_settings_from_file(settings_file).unwrap_or_else(|e| {
panic!("Error loading settings from file {}: {}", settings_file, e)
println!("Error loading settings from file {}: {}", settings_file, e);
std::process::exit(1)
});
};

// run server with settings
let db_pool =
db::load_db_connection_pool(&settings.db_url).expect("Error connecting to database");
let context = v0::context::new_shared_context(db_pool, &settings.block0_path);
// load db pool
let db_pool = db::load_db_connection_pool(&settings.db_url).unwrap_or_else(|e| {
println!("Error connecting to database: {}", e);
std::process::exit(1)
});

// load block0
let block0 = std::fs::read(&settings.block0_path).unwrap_or_else(|e| {
println!("Error loading block0 from {}: {}", &settings.block0_path, e,);
std::process::exit(1)
});

let context = v0::context::new_shared_context(db_pool, block0);

let app = v0::filter(context).await;

println!(
"Running server at {}, database located at {}",
settings.address, settings.db_url
);

// run server with settings
server::start_server(app, Some(settings)).await
}

0 comments on commit 48a53f8

Please sign in to comment.