From dcda1accfe80bc1adf7afd444819edc4517951fb Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Mon, 30 Nov 2020 16:12:40 +0100 Subject: [PATCH] Split out main further --- src/db.rs | 2 +- src/main.rs | 50 +++++++++++++++++++++++++++++++++----------------- src/server.rs | 12 ++++++++++-- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/db.rs b/src/db.rs index 35c7eea..113d32f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -35,7 +35,7 @@ const LMDB_MAX_DB: u32 = 16; impl Databases { pub fn new(log: &Logger, config: &Settings) -> Result { - // Initialize the LMDB environment. This blocks untill the mmap() finishes + // Initialize the LMDB environment. This blocks until the mmap() finishes info!(log, "LMDB env"); let env = lmdb::Environment::new() .set_flags(lmdb::EnvironmentFlags::MAP_ASYNC | lmdb::EnvironmentFlags::NO_SUB_DIR) diff --git a/src/main.rs b/src/main.rs index c0f53c0..c9be230 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,7 +44,7 @@ use registries::Registries; // Returning a `Result` from `main` allows us to use the `?` shorthand. // In the case of an Err it will be printed using `fmt::Debug` -fn main() -> Result<(), Error> { +fn maybe() -> Result { use clap::{crate_version, crate_description, crate_name}; // Argument parsing @@ -89,17 +89,12 @@ fn main() -> Result<(), Error> { handle.write_all(&encoded)?; // Early return to exit. - return Ok(()) - } else if matches.is_present("dump") { - } else if matches.is_present("load") { - } else { + return Ok(0) } - // If no `config` option is given use a preset default. let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml"); let config = config::read(&PathBuf::from_str(configpath).unwrap())?; - // Initialize the logging subsystem first to be able to better document the progress from now // on. // TODO: Now would be a really good time to close stdin/out and move logging to syslog @@ -107,17 +102,38 @@ fn main() -> Result<(), Error> { let log = Arc::new(log::init(&config)); info!(log, "Starting"); - let db = db::Databases::new(&log, &config)?; - server::serve_api_connections(log, config, db) + if matches.is_present("dump") { + error!(log, "Dumping is currently not implemented"); + Ok(-2) + } else if matches.is_present("load") { + error!(log, "Loading is currently not implemented"); + Ok(-2) + } else { + let db = match db::Databases::new(&log, &config) { + Err(e) => { + error!(log, "{}", e); + return Ok(-1); + }, + Ok(ok) => ok + }; + + match server::serve_api_connections(log.clone(), config, db) { + Err(e) => { + error!(log, "{}", e); + Ok(-1) + }, + ok => Ok(0) + } + } } -/// The result of one iteration of the core loop -pub enum LoopResult { - /// Everything was fine, keep going - Continue, - /// Something happened that means we should shut down - Stop, - /// The Server is currently overloaded - Overloaded, +fn main() { + match maybe() { + Ok(i) => std::process::exit(i), + Err(e) => { + println!("{}", e); + std::process::exit(-1); + } + } } diff --git a/src/server.rs b/src/server.rs index 4987da4..b985614 100644 --- a/src/server.rs +++ b/src/server.rs @@ -24,8 +24,6 @@ use std::str::FromStr; use std::sync::Arc; -use super::LoopResult; - use crate::db::Databases; pub fn serve_api_connections(log: Arc, config: Settings, db: Databases) -> Result<(), Error> { @@ -157,3 +155,13 @@ pub fn serve_api_connections(log: Arc, config: Settings, db: Databases) // Returning () is an implicit success so this will properly set the exit code as well Ok(()) } + +/// The result of one iteration of the core loop +pub enum LoopResult { + /// Everything was fine, keep going + Continue, + /// Something happened that means we should shut down + Stop, + /// The Server is currently overloaded + Overloaded, +}