Refactor project structure and add singlethreaded feature

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent a15a713a0c
commit 40496b1686
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -27,4 +27,8 @@ features = ["derive"]
[dependencies.tokio] [dependencies.tokio]
version = "1.9.0" version = "1.9.0"
features = ["macros", "rt-multi-thread", "process"] features = ["macros", "process"]
[features]
default = ["tokio/rt-multi-thread"]
singlethreaded = ["tokio/rt"]

@ -1,17 +1,29 @@
use crate::logging::init_logger; #[cfg(all(feature = "singlethreaded", feature = "multithreaded"))]
use crate::server::HookServer; compile_error!("Can't have singlethreaded and mutithreaded feature enabled at the same time.");
use crate::settings::get_settings;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
mod action; use utils::logging::init_logger;
mod command_template; use utils::settings::get_settings;
mod error;
mod logging; use crate::server::HookServer;
mod server; mod server;
mod settings; pub(crate) mod utils;
#[cfg(not(feature = "singlethreaded"))]
#[tokio::main] #[tokio::main]
async fn 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(); init_logger();
let data_dir = dirs::data_dir() let data_dir = dirs::data_dir()
.map(|d| d.join("multihook")) .map(|d| d.join("multihook"))
@ -21,8 +33,9 @@ async fn main() {
} }
let settings = get_settings(); let settings = get_settings();
let mut server = HookServer::new(); let mut server = HookServer::new();
for (name, endpoint) in &settings.endpoints { 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()) server.add_hook(endpoint.path.clone(), endpoint.action.clone().into())
} }

@ -1,5 +1,5 @@
use crate::command_template::CommandTemplate; use crate::server::command_template::CommandTemplate;
use crate::error::MultihookResult; use crate::utils::error::MultihookResult;
use serde_json::Value; use serde_json::Value;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::path::PathBuf; use std::path::PathBuf;

@ -1,12 +1,18 @@
use crate::action::HookAction;
use crate::error::MultihookError;
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use warp::http::Response; use warp::http::Response;
use warp::hyper::body::Bytes; use warp::hyper::body::Bytes;
use warp::{Filter, Rejection}; use warp::{Filter, Rejection};
use action::HookAction;
use crate::utils::error::MultihookError;
mod action;
pub mod command_template;
pub struct HookServer { pub struct HookServer {
endpoints: HashMap<String, HookAction>, endpoints: HashMap<String, HookAction>,
} }

@ -0,0 +1,3 @@
pub mod error;
pub mod logging;
pub mod settings;

@ -1,4 +1,4 @@
use crate::error::MultihookResult; use crate::utils::error::MultihookResult;
use config::File; use config::File;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
Loading…
Cancel
Save