refactor+deps: cleanup code, remove unused dep

This commit is contained in:
Compositr 2024-11-02 21:21:08 +11:00
parent da219bd9f9
commit 2b934876e0
6 changed files with 40 additions and 125 deletions

View file

@ -1,2 +1,7 @@
# 1.0.0 / 2024-11-02
- Added multithreading support
- Fixed spelling error in CORS headers
- Removed unnecessary crates
# v0.1.0 # v0.1.0
- Initial release - Initial release

102
Cargo.lock generated
View file

@ -56,12 +56,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]] [[package]]
name = "color_quant" name = "color_quant"
version = "1.1.0" version = "1.1.0"
@ -86,16 +80,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "ctrlc"
version = "3.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3"
dependencies = [
"nix",
"windows-sys",
]
[[package]] [[package]]
name = "data-url" name = "data-url"
version = "0.3.1" version = "0.3.1"
@ -214,7 +198,6 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
name = "luciders" name = "luciders"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ctrlc",
"indoc", "indoc",
"resvg", "resvg",
] ]
@ -238,18 +221,6 @@ dependencies = [
"simd-adler32", "simd-adler32",
] ]
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.6.0",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "pico-args" name = "pico-args"
version = "0.5.0" version = "0.5.0"
@ -505,79 +476,6 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]] [[package]]
name = "xmlwriter" name = "xmlwriter"
version = "0.1.0" version = "0.1.0"

View file

@ -5,6 +5,5 @@ edition = "2021"
license = "AGPL-3.0-only" license = "AGPL-3.0-only"
[dependencies] [dependencies]
ctrlc = "3.4.5"
indoc = "2.0.5" indoc = "2.0.5"
resvg = "0.44.0" resvg = "0.44.0"

View file

@ -6,9 +6,7 @@ use crate::{
threads::ThreadPool, threads::ThreadPool,
}; };
use std::{ use std::{
collections::HashMap, collections::HashMap, error::Error, net::{Shutdown, TcpListener}, sync::Arc
net::{Shutdown, TcpListener},
sync::Arc,
}; };
type DynHandlerFn = dyn Fn(Request) -> Response + Send + Sync; type DynHandlerFn = dyn Fn(Request) -> Response + Send + Sync;

View file

@ -1,6 +1,5 @@
use std::{net::TcpListener, process, sync::Arc}; use std::{net::TcpListener, process, sync::Arc};
use ctrlc;
use http::responses::{Body, Response}; use http::responses::{Body, Response};
use resvg::{ use resvg::{
@ -26,12 +25,6 @@ fn main() {
let listener = TcpListener::bind(format!("[::]:{}", port)) let listener = TcpListener::bind(format!("[::]:{}", port))
.unwrap_or_else(|_| fatal("Failed to bind to address")); .unwrap_or_else(|_| fatal("Failed to bind to address"));
ctrlc::set_handler(move || {
println!("Shutting down...");
process::exit(0);
})
.unwrap_or_else(|_| fatal("Failed to set termination signal handler"));
let icons_rc = Arc::new(match icons::Icons::build() { let icons_rc = Arc::new(match icons::Icons::build() {
Ok(icons) => icons, Ok(icons) => icons,
Err(e) => fatal(e), Err(e) => fatal(e),
@ -245,7 +238,9 @@ fn main() {
} }
}); });
handlers.bind(listener); handlers
.bind(listener)
.unwrap_or_else(|_| fatal("Failed to bind handlers"));
println!("luciders started on port {}", port); println!("luciders started on port {}", port);
println!("Serving {} icons", icons_rc.icons_len()) println!("Serving {} icons", icons_rc.icons_len())

View file

@ -27,15 +27,21 @@ impl ThreadPool {
} }
} }
Some(ThreadPool { sender: Some(sender), workers }) Some(ThreadPool {
sender: Some(sender),
workers,
})
} }
/// Pass a function to a worker thread to execute
///
/// f: Function to execute
pub fn execute<F>(&self, f: F) -> Result<(), mpsc::SendError<Job>> pub fn execute<F>(&self, f: F) -> Result<(), mpsc::SendError<Job>>
where where
F: FnOnce() + Send + 'static, F: FnOnce() + Send + 'static,
{ {
let job = Box::new(f); let job = Box::new(f);
match self.sender.as_ref() { match self.sender.as_ref() {
Some(sender) => sender.send(job), Some(sender) => sender.send(job),
None => Err(mpsc::SendError(job)), None => Err(mpsc::SendError(job)),
@ -46,10 +52,13 @@ impl ThreadPool {
impl Drop for ThreadPool { impl Drop for ThreadPool {
fn drop(&mut self) { fn drop(&mut self) {
drop(self.sender.take()); drop(self.sender.take());
for worker in &mut self.workers { for worker in &mut self.workers {
println!("Shutting down worker {}", worker.id);
if let Some(thread) = worker.thread.take() { if let Some(thread) = worker.thread.take() {
thread.join().expect("Failed to join worker thread on shutdown!") thread
.join()
.expect("Failed to join worker thread on shutdown!")
} }
} }
} }
@ -61,16 +70,27 @@ struct Worker {
} }
impl Worker { impl Worker {
/// Create a new Worker
///
/// id: Worker ID
/// receiver: Receiver to listen for jobs, wrapped in an Arc and Mutex to share among threads
fn build(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Option<Self> { fn build(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Option<Self> {
let builder = thread::Builder::new().name(format!("worker-{}", id)); let builder = thread::Builder::new().name(format!("worker-{}", id));
let thread = match builder.spawn(move || loop { let thread = match builder.spawn(move || {
let job = match receiver.lock().expect("Failed to lock receiver! Maybe the Mutex is poisoned?").recv() { println!("Worker {} spawned", id);
Ok(job) => job, loop {
Err(_) => break, let job = match receiver
}; .lock()
.expect("Failed to lock receiver! Maybe the Mutex is poisoned?")
.recv()
{
Ok(job) => job,
Err(_) => break,
};
job(); job();
}
}) { }) {
Ok(thread) => Some(thread), Ok(thread) => Some(thread),
Err(_) => return None, Err(_) => return None,