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
- 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"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "color_quant"
version = "1.1.0"
@ -86,16 +80,6 @@ dependencies = [
"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]]
name = "data-url"
version = "0.3.1"
@ -214,7 +198,6 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
name = "luciders"
version = "0.1.0"
dependencies = [
"ctrlc",
"indoc",
"resvg",
]
@ -238,18 +221,6 @@ dependencies = [
"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]]
name = "pico-args"
version = "0.5.0"
@ -505,79 +476,6 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "xmlwriter"
version = "0.1.0"

View file

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

View file

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

View file

@ -1,6 +1,5 @@
use std::{net::TcpListener, process, sync::Arc};
use ctrlc;
use http::responses::{Body, Response};
use resvg::{
@ -26,12 +25,6 @@ fn main() {
let listener = TcpListener::bind(format!("[::]:{}", port))
.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() {
Ok(icons) => icons,
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!("Serving {} icons", icons_rc.icons_len())

View file

@ -27,9 +27,15 @@ 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>>
where
F: FnOnce() + Send + 'static,
@ -48,8 +54,11 @@ impl Drop for ThreadPool {
drop(self.sender.take());
for worker in &mut self.workers {
println!("Shutting down worker {}", worker.id);
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 {
/// 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> {
let builder = thread::Builder::new().name(format!("worker-{}", id));
let thread = match builder.spawn(move || loop {
let job = match receiver.lock().expect("Failed to lock receiver! Maybe the Mutex is poisoned?").recv() {
let thread = match builder.spawn(move || {
println!("Worker {} spawned", id);
loop {
let job = match receiver
.lock()
.expect("Failed to lock receiver! Maybe the Mutex is poisoned?")
.recv()
{
Ok(job) => job,
Err(_) => break,
};
job();
}
}) {
Ok(thread) => Some(thread),
Err(_) => return None,