From 5eb910fcb50e449f6c84934e9584be607eb15fd5 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sat, 29 Oct 2022 18:49:42 +0200 Subject: [PATCH] Don't panic! --- src/config.rs | 16 ++++++++-------- src/db.rs | 8 ++++---- src/logging.rs | 11 ++++++----- src/main.rs | 24 +++++++++++++++++------- src/states.rs | 10 ++++++---- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8088f71..0a08a48 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ +use anyhow::{Context, Result}; use serde::Deserialize; use std::env; use std::fs::File; @@ -20,18 +21,17 @@ pub struct Config { } impl Config { - pub fn new() -> Self { + pub fn new() -> Result { let config_file_var = "GWC_CONFIG_FILE"; let config_path = env::var(config_file_var) - .unwrap_or_else(|_| panic!("Environment variable {config_file_var} missing!")); + .with_context(|| format!("Environment variable {config_file_var} missing!"))?; - let config_file = File::open(&config_path).unwrap_or_else(|e| { - panic!("Can not open the config file at the path {config_path}: {e}") - }); + let config_file = File::open(&config_path) + .with_context(|| format!("Can not open the config file at the path {config_path}"))?; let config_reader = BufReader::new(config_file); - let config: Self = - serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!"); + let config: Self = serde_json::from_reader(config_reader) + .context("Can not parse the config file as JSON!")?; - config + Ok(config) } } diff --git a/src/db.rs b/src/db.rs index 99c6bd5..55aa6f7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -11,16 +11,16 @@ use crate::schema::hooklog; pub type DBPool = Pool>; -pub fn establish_connection_pool() -> DBPool { +pub fn establish_connection_pool() -> Result { let database_url_var = "DATABASE_URL"; let database_url = env::var(database_url_var) - .unwrap_or_else(|_| panic!("Environment variable {database_url_var} missing!")); + .with_context(|| format!("Environment variable {database_url_var} missing!"))?; - let manager = ConnectionManager::::new(&database_url); + let manager = ConnectionManager::::new(database_url); Pool::builder() .build(manager) - .expect("Could not build database connection pool!") + .context("Could not build database connection pool!") } fn get_conn(pool: &DBPool) -> Result>> { diff --git a/src/logging.rs b/src/logging.rs index a57a177..74d08d3 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,9 +1,10 @@ +use anyhow::{Context, Result}; use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode, WriteLogger}; use std::fs::OpenOptions; use crate::config; -pub fn init_logger(config: &config::Config) { +pub fn init_logger(config: &config::Config) -> Result<()> { let logger = if cfg!(debug_assertions) { TermLogger::init( LevelFilter::Debug, @@ -19,11 +20,11 @@ pub fn init_logger(config: &config::Config) { .create(true) .append(true) .open(&config.log_file) - .unwrap_or_else(|e| { - panic!("Could not open the log file {}: {e}", &config.log_file) - }), + .with_context(|| format!("Could not open the log file {}", &config.log_file))?, ) }; - logger.expect("Could not initialize the logger!"); + logger.context("Could not initialize the logger!")?; + + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 6d4bf77..4d3d271 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,21 +7,31 @@ mod routes; mod schema; mod states; +use anyhow::Result; use log::info; +use rocket::{Build, Rocket}; use rocket_dyn_templates::Template; +use std::process; -#[rocket::launch] -fn rocket() -> _ { - let config = config::Config::new(); +fn init() -> Result> { + let config = config::Config::new()?; - logging::init_logger(&config); + logging::init_logger(&config)?; info!("Starting client"); - rocket::build() + Ok(rocket::build() .mount("/", rocket::routes![routes::index]) .mount("/api", rocket::routes![routes::trigger]) - .manage(states::DB::new()) + .manage(states::DB::new()?) .manage(states::Config::new(config)) - .attach(Template::fairing()) + .attach(Template::fairing())) +} + +#[rocket::launch] +fn rocket() -> _ { + init().unwrap_or_else(|e| { + eprintln!("{e}"); + process::exit(1); + }) } diff --git a/src/states.rs b/src/states.rs index cc827fb..fba152e 100644 --- a/src/states.rs +++ b/src/states.rs @@ -1,15 +1,17 @@ use crate::config; use crate::db; +use anyhow::Result; + pub struct DB { pub pool: db::DBPool, } impl DB { - pub fn new() -> Self { - Self { - pool: db::establish_connection_pool(), - } + pub fn new() -> Result { + Ok(Self { + pool: db::establish_connection_pool()?, + }) } }