index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2022-05-21 22:07:06.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2022-05-21 22:07:06.0 +00:00:00
commit
97f9d152ea78aaa2f8bb0a549d77b4c1b8d2c528 [patch]
tree
7324e5876377eee4590e29844b6f950b49749759
parent
eb34587c532032074f011c27caf3d9cce7579895
download
97f9d152ea78aaa2f8bb0a549d77b4c1b8d2c528.tar.gz

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(-)

diff --git a/src/bin/server.rs b/src/bin/server.rs
index 833ae0a..0cf9154 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -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;

    // Get server config
    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");
    }

    // Run the server by default
diff --git a/src/settings/cli.rs b/src/settings/cli.rs
index 18043bd..eb2afb6 100644
--- a/src/settings/cli.rs
+++ b/src/settings/cli.rs
@@ -170,6 +170,10 @@ pub struct General {
    /// Server TOML configuration file path.
    pub config_file: Option<PathBuf>,

    //
    // Windows specific arguments and commands
    //
    #[cfg(windows)]
    #[structopt(
        long,
        short = "s",
@@ -180,14 +184,16 @@ pub struct General {
    /// Run the web server as a Windows Service.
    pub as_windows_service: bool,

    // WINDOWS-ONLY:
    // Windows commands
    #[cfg(windows)]
    #[structopt(subcommand)]
    pub commands: Option<Commands>,
}

#[cfg(windows)]
#[derive(Debug, StructOpt)]
pub enum Commands {
    /// Install a Windows Service for the server.
    /// Install a Windows Service for the web server.
    #[structopt(name = "install")]
    Install {},

diff --git a/src/settings/mod.rs b/src/settings/mod.rs
index 64cc6b3..0540ad7 100644
--- a/src/settings/mod.rs
+++ b/src/settings/mod.rs
@@ -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,

                // WINDOWS-ONLY: sub commands
                // NOTE
                // Windows-only options and commands
                #[cfg(windows)]
                as_windows_service: opts.as_windows_service,
                #[cfg(windows)]
                commands: opts.commands,
            },
            advanced: settings_advanced,
diff --git a/src/winservice.rs b/src/winservice.rs
index 6e45e06..805187b 100644
--- a/src/winservice.rs
+++ b/src/winservice.rs
@@ -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,
    };

    //  system about the service status
    // Inform the system about the service status
    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");

    // Create a channel to be able to poll a stop event from the service worker loop.
    let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
@@ -183,9 +183,11 @@ pub fn install_service(config_file: Option<PathBuf>) -> Result {

    // Set the executable path to point the current binary
    let service_binary_path = std::env::current_exe().unwrap().with_file_name(SERVICE_EXE);

    // Set service binary default arguments
    let mut service_binary_arguments = vec![OsString::from("--as-windows-service=true")];

    // Append `--config-file` path to binary arguments if present
    // Append a `--config-file` path to the binary arguments if present
    if let Some(f) = config_file {
        let f = adjust_canonicalization(f);
        if !f.is_empty() {