From b9b344b50cfc2b0afe82a74930cea5d60d06df34 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien Date: Sun, 30 Nov 2025 10:19:22 -0600 Subject: [PATCH] Divide lib a little more --- src/lib.rs | 65 +++++--------------------------------------------- src/macros.rs | 37 ++++++++++++++++++++++++++++ src/runtime.rs | 17 +++++++++++++ 3 files changed, 60 insertions(+), 59 deletions(-) create mode 100644 src/macros.rs create mode 100644 src/runtime.rs diff --git a/src/lib.rs b/src/lib.rs index 03baad0..6e53d36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,9 @@ mod options; mod router; mod encryption; mod sql; +mod runtime; +#[macro_use] +mod macros; use actix_web::{ rt, @@ -12,12 +15,9 @@ use actix_web::{ dev::Service }; use std::fs; -use std::sync::Mutex; use std::time::Duration; -use lazy_static::lazy_static; use options::get_args; - #[actix_web::main] pub async fn run_server(in_thread: bool) -> std::io::Result<()> { let args = get_args(); @@ -52,10 +52,10 @@ pub async fn run_server(in_thread: bool) -> std::io::Result<()> { } if in_thread { - set_running(true).await; + runtime::set_running(true); let handle = rv.handle(); rt::spawn(rv); - while get_running().await { + while runtime::get_running() { actix_web::rt::time::sleep(Duration::from_millis(100)).await; } handle.stop(false).await; @@ -67,49 +67,10 @@ pub async fn run_server(in_thread: bool) -> std::io::Result<()> { #[actix_web::main] async fn stop_server() { - set_running(false).await; + runtime::set_running(false); println!("Stopping"); } - -// include_file macro: includes a file compressed at compile time, and decompresses it on reference. Decreases binary size -#[macro_export] -macro_rules! include_file { - ( $s:expr ) => { - { - let file = include_flate_codegen::deflate_file!($s); - let ret = $crate::decode(file); - std::string::String::from_utf8(ret).unwrap() - } - }; -} - -pub fn decode(bytes: &[u8]) -> Vec { - use std::io::{Cursor, Read}; - - let mut dec = libflate::deflate::Decoder::new(Cursor::new(bytes)); - let mut ret = Vec::new(); - dec.read_to_end(&mut ret).unwrap(); - ret -} - -#[macro_export] -macro_rules! lock_onto_mutex { - ($mutex:expr) => {{ - loop { - match $mutex.lock() { - Ok(value) => { - break value; - } - Err(_) => { - $mutex.clear_poison(); - actix_web::rt::time::sleep(std::time::Duration::from_millis(15)).await; - } - } - } - }}; -} - pub fn get_data_path(file_name: &str) -> String { let args = get_args(); let mut path = args.path; @@ -119,17 +80,3 @@ pub fn get_data_path(file_name: &str) -> String { fs::create_dir_all(&path).unwrap(); format!("{}/{}", path, file_name) } - -lazy_static! { - static ref RUNNING: Mutex = Mutex::new(false); -} - -async fn set_running(running: bool) { - let mut result = lock_onto_mutex!(RUNNING); - *result = running; -} - -async fn get_running() -> bool { - let result = lock_onto_mutex!(RUNNING); - *result -} diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..df26120 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,37 @@ +// include_file macro: includes a file compressed at compile time, and decompresses it on reference. Decreases binary size +#[macro_export] +macro_rules! include_file { + ( $s:expr ) => { + { + let file = include_flate_codegen::deflate_file!($s); + let ret = $crate::macros::decode(file); + std::string::String::from_utf8(ret).unwrap() + } + }; +} + +pub fn decode(bytes: &[u8]) -> Vec { + use std::io::{Cursor, Read}; + + let mut dec = libflate::deflate::Decoder::new(Cursor::new(bytes)); + let mut ret = Vec::new(); + dec.read_to_end(&mut ret).unwrap(); + ret +} + +#[macro_export] +macro_rules! lock_onto_mutex { + ($mutex:expr) => {{ + loop { + match $mutex.lock() { + Ok(value) => { + break value; + } + Err(_) => { + $mutex.clear_poison(); + std::thread::sleep(std::time::Duration::from_millis(15)); + } + } + } + }}; +} diff --git a/src/runtime.rs b/src/runtime.rs new file mode 100644 index 0000000..e0e014a --- /dev/null +++ b/src/runtime.rs @@ -0,0 +1,17 @@ +use crate::lock_onto_mutex; +use lazy_static::lazy_static; +use std::sync::Mutex; + +lazy_static! { + static ref RUNNING: Mutex = Mutex::new(false); +} + +pub fn set_running(running: bool) { + let mut result = lock_onto_mutex!(RUNNING); + *result = running; +} + +pub fn get_running() -> bool { + let result = lock_onto_mutex!(RUNNING); + *result +}