mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew
synced 2026-01-12 00:02:38 +08:00
Divide lib a little more
This commit is contained in:
65
src/lib.rs
65
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<u8> {
|
||||
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<bool> = 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
|
||||
}
|
||||
|
||||
37
src/macros.rs
Normal file
37
src/macros.rs
Normal file
@@ -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<u8> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
17
src/runtime.rs
Normal file
17
src/runtime.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::lock_onto_mutex;
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::Mutex;
|
||||
|
||||
lazy_static! {
|
||||
static ref RUNNING: Mutex<bool> = 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
|
||||
}
|
||||
Reference in New Issue
Block a user