From d549c3db5a49b7d8ddd7196a2041cfb7dbcd5c7a Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Fri, 26 Apr 2019 09:31:17 +0200 Subject: [PATCH] Move the daemonize role to acme_common --- acme_common/Cargo.toml | 1 + acme_common/src/lib.rs | 15 +++++++++++++++ acmed/Cargo.toml | 1 - acmed/src/main.rs | 17 +++++------------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/acme_common/Cargo.toml b/acme_common/Cargo.toml index 586a504..8564b49 100644 --- a/acme_common/Cargo.toml +++ b/acme_common/Cargo.toml @@ -13,6 +13,7 @@ name = "acme_common" [dependencies] base64 = "0.10" +daemonize = "0.4" handlebars = "2.0.0-beta.1" http_req = "0.4" openssl = "0.10" diff --git a/acme_common/src/lib.rs b/acme_common/src/lib.rs index cb5e209..33c7625 100644 --- a/acme_common/src/lib.rs +++ b/acme_common/src/lib.rs @@ -1,6 +1,21 @@ +use daemonize::Daemonize; + pub mod error; pub mod gen; pub fn b64_encode>(input: &T) -> String { base64::encode_config(input, base64::URL_SAFE_NO_PAD) } + +pub fn init_server(foreground: bool, pid_file: &str) { + if !foreground { + let daemonize = Daemonize::new().pid_file(pid_file); + match daemonize.start() { + Ok(_) => {} + Err(e) => { + eprintln!("Error: {}", e); + std::process::exit(3); + } + } + } +} diff --git a/acmed/Cargo.toml b/acmed/Cargo.toml index 691981a..33cae5c 100644 --- a/acmed/Cargo.toml +++ b/acmed/Cargo.toml @@ -14,7 +14,6 @@ build = "build.rs" [dependencies] acme_common = { path = "../acme_common" } clap = "2.32" -daemonize = "0.4" env_logger = "0.6" handlebars = "2.0.0-beta.1" http_req = "0.4" diff --git a/acmed/src/main.rs b/acmed/src/main.rs index c271311..aa957d3 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -1,6 +1,6 @@ use crate::main_event_loop::MainEventLoop; +use acme_common::init_server; use clap::{App, Arg}; -use daemonize::Daemonize; use log::{error, LevelFilter}; mod acme_proto; @@ -92,17 +92,10 @@ fn main() { } }; - if !matches.is_present("foregroung") { - let pid_file = matches.value_of("pid-file").unwrap_or(DEFAULT_PID_FILE); - let daemonize = Daemonize::new().pid_file(pid_file); - match daemonize.start() { - Ok(_) => {} - Err(e) => { - eprintln!("Error: {}", e); - std::process::exit(3); - } - } - } + init_server( + matches.is_present("foregroung"), + matches.value_of("pid-file").unwrap_or(DEFAULT_PID_FILE), + ); let config_file = matches.value_of("config").unwrap_or(DEFAULT_CONFIG_FILE); let mut srv = match MainEventLoop::new(&config_file) {