refactor: make windows service args and commands platform specific
Diff
src/bin/server.rs | 44 +++++++++++++++++---------------------------
src/settings/cli.rs | 10 ++++++++--
src/settings/mod.rs | 6 +++++-
src/winservice.rs | 10 ++++++----
4 files changed, 36 insertions(+), 34 deletions(-)
@@ -10,35 +10,25 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
use static_web_server::Result;
fn main() -> Result {
use static_web_server::settings::Commands;
use static_web_server::Settings;
let opts = Settings::get()?;
if let Some(commands) = opts.general.commands {
match commands {
Commands::Install {} => {
#[cfg(windows)]
return static_web_server::winservice::install_service(opts.general.config_file);
#[cfg(unix)]
println!("ignored: the `install` command is only available for Windows");
}
Commands::Uninstall {} => {
#[cfg(windows)]
return static_web_server::winservice::uninstall_service();
#[cfg(unix)]
println!("ignored: the `uninstall` command is only available for Windows");
#[cfg(windows)]
{
use static_web_server::settings::{Commands, Settings};
use static_web_server::winservice;
let opts = Settings::get()?;
if let Some(commands) = opts.general.commands {
match commands {
Commands::Install {} => {
return winservice::install_service(opts.general.config_file);
}
Commands::Uninstall {} => {
return winservice::uninstall_service();
}
}
} else if opts.general.as_windows_service {
return winservice::run_server_as_service();
}
} else if opts.general.as_windows_service {
#[cfg(windows)]
return static_web_server::winservice::run_server_as_service();
#[cfg(unix)]
println!("ignored: the `--as-windows-service` option is only available for Windows");
}
@@ -170,6 +170,10 @@ pub struct General {
pub config_file: Option<PathBuf>,
#[cfg(windows)]
#[structopt(
long,
short = "s",
@@ -180,14 +184,16 @@ pub struct General {
pub as_windows_service: bool,
#[cfg(windows)]
#[structopt(subcommand)]
pub commands: Option<Commands>,
}
#[cfg(windows)]
#[derive(Debug, StructOpt)]
pub enum Commands {
#[structopt(name = "install")]
Install {},
@@ -7,6 +7,7 @@ use crate::{Context, Result};
mod cli;
pub mod file;
#[cfg(windows)]
pub use cli::Commands;
use cli::General;
@@ -206,8 +207,11 @@ impl Settings {
grace_period,
page_fallback,
#[cfg(windows)]
as_windows_service: opts.as_windows_service,
#[cfg(windows)]
commands: opts.commands,
},
advanced: settings_advanced,
@@ -30,7 +30,7 @@ define_windows_service!(ffi_service_main, custom_service_main);
fn custom_service_main(_args: Vec<OsString>) {
if let Err(err) = run_service() {
tracing::info!("error starting the service: {:?}", err);
tracing::error!("error starting the service: {:?}", err);
}
}
@@ -58,7 +58,7 @@ fn set_service_state(
process_id: None,
};
Ok(status_handle.set_service_status(next_status)?)
}
@@ -67,7 +67,7 @@ fn run_service() -> Result {
logger::init(&opts.general.log_level)?;
tracing::info!("windows service: starting setup");
tracing::info!("windows service: starting service setup");
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
@@ -183,9 +183,11 @@ pub fn install_service(config_file: Option<PathBuf>) -> Result {
let service_binary_path = std::env::current_exe().unwrap().with_file_name(SERVICE_EXE);
let mut service_binary_arguments = vec![OsString::from("--as-windows-service=true")];
if let Some(f) = config_file {
let f = adjust_canonicalization(f);
if !f.is_empty() {