From 40496b1686a8fbf7e62d98c1cc21b65569fded49 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 7 Aug 2021 16:07:35 +0200 Subject: [PATCH] Refactor project structure and add singlethreaded feature Signed-off-by: trivernis --- Cargo.toml | 6 +++++- src/main.rs | 31 ++++++++++++++++++++-------- src/{ => server}/action.rs | 4 ++-- src/{ => server}/command_template.rs | 0 src/{server.rs => server/mod.rs} | 10 +++++++-- src/{ => utils}/error.rs | 0 src/{ => utils}/logging.rs | 0 src/utils/mod.rs | 3 +++ src/{ => utils}/settings.rs | 2 +- 9 files changed, 41 insertions(+), 15 deletions(-) rename src/{ => server}/action.rs (92%) rename src/{ => server}/command_template.rs (100%) rename src/{server.rs => server/mod.rs} (95%) rename src/{ => utils}/error.rs (100%) rename src/{ => utils}/logging.rs (100%) create mode 100644 src/utils/mod.rs rename src/{ => utils}/settings.rs (98%) diff --git a/Cargo.toml b/Cargo.toml index a8653b6..6cef58d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,8 @@ features = ["derive"] [dependencies.tokio] version = "1.9.0" -features = ["macros", "rt-multi-thread", "process"] \ No newline at end of file +features = ["macros", "process"] + +[features] +default = ["tokio/rt-multi-thread"] +singlethreaded = ["tokio/rt"] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 01b8878..14daa7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,29 @@ -use crate::logging::init_logger; -use crate::server::HookServer; -use crate::settings::get_settings; +#[cfg(all(feature = "singlethreaded", feature = "multithreaded"))] +compile_error!("Can't have singlethreaded and mutithreaded feature enabled at the same time."); + use std::path::{Path, PathBuf}; -mod action; -mod command_template; -mod error; -mod logging; +use utils::logging::init_logger; +use utils::settings::get_settings; + +use crate::server::HookServer; + mod server; -mod settings; +pub(crate) mod utils; +#[cfg(not(feature = "singlethreaded"))] #[tokio::main] async fn main() { + init_and_start().await +} + +#[cfg(feature = "singlethreaded")] +#[tokio::main(flavor = "current_thread")] +async fn main() { + init_and_start().await +} + +async fn init_and_start() { init_logger(); let data_dir = dirs::data_dir() .map(|d| d.join("multihook")) @@ -21,8 +33,9 @@ async fn main() { } let settings = get_settings(); let mut server = HookServer::new(); + for (name, endpoint) in &settings.endpoints { - log::info!("Adding endpoint {} with path {}", name, &endpoint.path); + log::info!("Adding endpoint '{}' with path '{}'", name, &endpoint.path); server.add_hook(endpoint.path.clone(), endpoint.action.clone().into()) } diff --git a/src/action.rs b/src/server/action.rs similarity index 92% rename from src/action.rs rename to src/server/action.rs index cbb72c8..8441800 100644 --- a/src/action.rs +++ b/src/server/action.rs @@ -1,5 +1,5 @@ -use crate::command_template::CommandTemplate; -use crate::error::MultihookResult; +use crate::server::command_template::CommandTemplate; +use crate::utils::error::MultihookResult; use serde_json::Value; use std::fs::read_to_string; use std::path::PathBuf; diff --git a/src/command_template.rs b/src/server/command_template.rs similarity index 100% rename from src/command_template.rs rename to src/server/command_template.rs diff --git a/src/server.rs b/src/server/mod.rs similarity index 95% rename from src/server.rs rename to src/server/mod.rs index 32fe35f..a4a5909 100644 --- a/src/server.rs +++ b/src/server/mod.rs @@ -1,12 +1,18 @@ -use crate::action::HookAction; -use crate::error::MultihookError; use std::collections::HashMap; use std::net::SocketAddr; use std::sync::Arc; + use warp::http::Response; use warp::hyper::body::Bytes; use warp::{Filter, Rejection}; +use action::HookAction; + +use crate::utils::error::MultihookError; + +mod action; +pub mod command_template; + pub struct HookServer { endpoints: HashMap, } diff --git a/src/error.rs b/src/utils/error.rs similarity index 100% rename from src/error.rs rename to src/utils/error.rs diff --git a/src/logging.rs b/src/utils/logging.rs similarity index 100% rename from src/logging.rs rename to src/utils/logging.rs diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..15eb2b9 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,3 @@ +pub mod error; +pub mod logging; +pub mod settings; diff --git a/src/settings.rs b/src/utils/settings.rs similarity index 98% rename from src/settings.rs rename to src/utils/settings.rs index a258938..8a6e43b 100644 --- a/src/settings.rs +++ b/src/utils/settings.rs @@ -1,4 +1,4 @@ -use crate::error::MultihookResult; +use crate::utils::error::MultihookResult; use config::File; use lazy_static::lazy_static; use serde::{Deserialize, Serialize};