Split out main further

This commit is contained in:
Gregor Reitzenstein 2020-11-30 16:12:40 +01:00
parent a0d60a574f
commit dcda1accfe
3 changed files with 44 additions and 20 deletions

View File

@ -35,7 +35,7 @@ const LMDB_MAX_DB: u32 = 16;
impl Databases { impl Databases {
pub fn new(log: &Logger, config: &Settings) -> Result<Self> { pub fn new(log: &Logger, config: &Settings) -> Result<Self> {
// Initialize the LMDB environment. This blocks untill the mmap() finishes // Initialize the LMDB environment. This blocks until the mmap() finishes
info!(log, "LMDB env"); info!(log, "LMDB env");
let env = lmdb::Environment::new() let env = lmdb::Environment::new()
.set_flags(lmdb::EnvironmentFlags::MAP_ASYNC | lmdb::EnvironmentFlags::NO_SUB_DIR) .set_flags(lmdb::EnvironmentFlags::MAP_ASYNC | lmdb::EnvironmentFlags::NO_SUB_DIR)

View File

@ -44,7 +44,7 @@ use registries::Registries;
// Returning a `Result` from `main` allows us to use the `?` shorthand. // Returning a `Result` from `main` allows us to use the `?` shorthand.
// In the case of an Err it will be printed using `fmt::Debug` // In the case of an Err it will be printed using `fmt::Debug`
fn main() -> Result<(), Error> { fn maybe() -> Result<i32, Error> {
use clap::{crate_version, crate_description, crate_name}; use clap::{crate_version, crate_description, crate_name};
// Argument parsing // Argument parsing
@ -89,17 +89,12 @@ fn main() -> Result<(), Error> {
handle.write_all(&encoded)?; handle.write_all(&encoded)?;
// Early return to exit. // Early return to exit.
return Ok(()) return Ok(0)
} else if matches.is_present("dump") {
} else if matches.is_present("load") {
} else {
} }
// If no `config` option is given use a preset default. // If no `config` option is given use a preset default.
let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml"); let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml");
let config = config::read(&PathBuf::from_str(configpath).unwrap())?; let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
// Initialize the logging subsystem first to be able to better document the progress from now // Initialize the logging subsystem first to be able to better document the progress from now
// on. // on.
// TODO: Now would be a really good time to close stdin/out and move logging to syslog // 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)); let log = Arc::new(log::init(&config));
info!(log, "Starting"); 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 fn main() {
pub enum LoopResult { match maybe() {
/// Everything was fine, keep going Ok(i) => std::process::exit(i),
Continue, Err(e) => {
/// Something happened that means we should shut down println!("{}", e);
Stop, std::process::exit(-1);
/// The Server is currently overloaded }
Overloaded, }
} }

View File

@ -24,8 +24,6 @@ use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use super::LoopResult;
use crate::db::Databases; use crate::db::Databases;
pub fn serve_api_connections(log: Arc<Logger>, config: Settings, db: Databases) -> Result<(), Error> { pub fn serve_api_connections(log: Arc<Logger>, config: Settings, db: Databases) -> Result<(), Error> {
@ -157,3 +155,13 @@ pub fn serve_api_connections(log: Arc<Logger>, config: Settings, db: Databases)
// Returning () is an implicit success so this will properly set the exit code as well // Returning () is an implicit success so this will properly set the exit code as well
Ok(()) 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,
}