feat: Initial rust API PoC
This commit is contained in:
235
Cargo.lock
generated
Normal file
235
Cargo.lock
generated
Normal file
@@ -0,0 +1,235 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.74 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.74"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "mueapi"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.74 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.114"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde_derive 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.114"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.74 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
"checksum chrono 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
|
||||
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||
"checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
|
||||
"checksum libc 0.2.74 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10"
|
||||
"checksum num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
|
||||
"checksum num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
|
||||
"checksum ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
|
||||
"checksum proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
|
||||
"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
"checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
|
||||
"checksum rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
|
||||
"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||
"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
|
||||
"checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
"checksum serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)" = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
|
||||
"checksum serde_derive 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)" = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
|
||||
"checksum serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)" = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
|
||||
"checksum syn 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4"
|
||||
"checksum time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
|
||||
"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
"checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
"checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "mueapi"
|
||||
version = "0.1.0"
|
||||
authors = ["Wessel T <discord@go2it.eu>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.7.3"
|
||||
serde = { version = "1.0.114", features = ["derive"] }
|
||||
serde_json = "1.0.56"
|
||||
chrono = "0.4.13"
|
||||
13
src/Cargo.toml
Normal file
13
src/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "mueapi"
|
||||
version = "0.1.0"
|
||||
authors = ["Wessel T <discord@go2it.eu>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.7.3"
|
||||
serde = { version = "1.0.114", features = ["derive"] }
|
||||
serde_json = "1.0.56"
|
||||
chrono = "0.4.13"
|
||||
1927
src/images.json
Normal file
1927
src/images.json
Normal file
File diff suppressed because it is too large
Load Diff
38
src/json.rs
Normal file
38
src/json.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use serde_json;
|
||||
use rand::Rng;
|
||||
use std::fs;
|
||||
|
||||
/// Converts the given file path into a JSON object
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `filename` - The path to the file to parse
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use json::parse;
|
||||
/// let file = parse("./src/array.json");
|
||||
/// ```
|
||||
pub fn parse(filename: &str) -> serde_json::Value {
|
||||
serde_json::from_str(&fs::read_to_string(filename).expect("Expected valid JSON")).expect("JSON was not well-formatted")
|
||||
}
|
||||
|
||||
/// Returns a random value from the given array, errors if not typeof `serde_json::Value::Array`
|
||||
/// # Arguments
|
||||
/// * `input` - The array to grab a random entry from
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use json;
|
||||
/// json::rand_or_error(&json::parse("./src/array.json")).unwrap();
|
||||
/// ```
|
||||
pub fn rand_or_error(input: &serde_json::Value) -> Result<&serde_json::Value, String> {
|
||||
let mut rand = rand::thread_rng();
|
||||
let internal = String::from("{ \"code\": 500, \"message\": \"internal server error\" }");
|
||||
|
||||
if let serde_json::Value::Array(array) = input {
|
||||
let entry = &array[(rand.gen::<f32>() * array.len() as f32).floor() as usize];
|
||||
Ok(entry)
|
||||
} else {
|
||||
Err(internal)
|
||||
}
|
||||
}
|
||||
96
src/main.rs
Normal file
96
src/main.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
mod json;
|
||||
mod thread;
|
||||
|
||||
use std::str;
|
||||
use std::sync::Arc;
|
||||
use std::io::prelude::*;
|
||||
use std::net::TcpStream;
|
||||
use std::net::TcpListener;
|
||||
use std::env;
|
||||
|
||||
extern crate chrono;
|
||||
|
||||
/*
|
||||
args[1] = port
|
||||
args[2] = threads
|
||||
args[3] = host
|
||||
*/
|
||||
|
||||
fn main() {
|
||||
let mut args = vec![];
|
||||
for argument in env::args() {
|
||||
args.push(argument);
|
||||
}
|
||||
|
||||
println!("PID; {}", std::process::id());
|
||||
|
||||
// ! Port to run on
|
||||
let listener = TcpListener::bind(format!("127.0.0.1:{}", args[1])).unwrap();
|
||||
|
||||
// ! Amount of threads used
|
||||
let threads = args[2].parse().unwrap();
|
||||
let pool = thread::ThreadPool::new(threads);
|
||||
|
||||
let img = Arc::new(json::parse("./src/images.json"));
|
||||
let quote = Arc::new(json::parse("./src/quotes.json"));
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let stream = stream.unwrap_or_else(|ex| panic!(ex));
|
||||
let img_clone = Arc::clone(&img);
|
||||
let quote_clone = Arc::clone("e);
|
||||
|
||||
pool.execute(move || {
|
||||
handle_connection(stream, img_clone, quote_clone);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn response(status: String, content: String, headers: Vec<String>) -> String {
|
||||
let mut args = vec![];
|
||||
for argument in env::args() {
|
||||
args.push(argument);
|
||||
}
|
||||
|
||||
// ! Change the domain name
|
||||
let host = &args[3];
|
||||
let now = chrono::Utc::now().format("%a, %e %b %Y %T UTC").to_string();
|
||||
let mut header = Vec::new();
|
||||
|
||||
header.extend(headers.into_iter());
|
||||
|
||||
header.push(format!("Date: {}", now));
|
||||
header.push(format!("Server: {}", host));
|
||||
header.push(String::from("Cache-Control: max-age=0, private, must-revalidate"));
|
||||
header.push(String::from("Access-Control-Allow-Origin: *"));
|
||||
|
||||
format!("HTTP/1.1 {}\r\n{}\r\n\r\n{}", status, header.join("\r\n"), content)
|
||||
}
|
||||
|
||||
fn handle_connection(mut stream: TcpStream, img: Arc<serde_json::Value>, quote: Arc<serde_json::Value>) {
|
||||
let mut buffer: Vec<u8> = vec![0; 512];
|
||||
|
||||
stream.read(&mut buffer).unwrap();
|
||||
|
||||
let req: &str = str::from_utf8(&buffer).unwrap().split("\r\n").next().unwrap();
|
||||
let base_headers: Vec<String> = vec![String::from("Content-Type: application/json; charset=utf-8")];
|
||||
|
||||
let resp: String = if "GET /getimage HTTP/1.1".eq_ignore_ascii_case(req) {
|
||||
let content = json::rand_or_error(&img);
|
||||
response(String::from("200 Ok"), content.unwrap().to_string(), base_headers)
|
||||
} else if "GET /update HTTP/1.1".eq_ignore_ascii_case(req) {
|
||||
let content: String = String::from("{}");
|
||||
response(String::from("200 Ok"), content, base_headers)
|
||||
} else if "GET /quote HTTP/1.1".eq_ignore_ascii_case(req) {
|
||||
let content: Result<&serde_json::Value, String> = json::rand_or_error("e);
|
||||
response(String::from("200 Ok"), content.unwrap().to_string(), base_headers)
|
||||
} else if "GET /getcategories HTTP/1.1".eq_ignore_ascii_case(req) {
|
||||
let content: String = String::from("[\"Outdoors\", \"Cheese\"]");
|
||||
response(String::from("200 Ok"), content, base_headers)
|
||||
} else {
|
||||
let content: String = String::from("{ \"code\": 404, \"message\": \"not found\" }");
|
||||
response(String::from("404 Not Found"), content, base_headers)
|
||||
};
|
||||
|
||||
stream.write(resp.as_bytes()).unwrap();
|
||||
stream.flush().unwrap();
|
||||
}
|
||||
224
src/quotes.json
Normal file
224
src/quotes.json
Normal file
@@ -0,0 +1,224 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"author": "Albert Einstein",
|
||||
"quote": "Be a voice not an echo.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"author": "Unknown",
|
||||
"quote": "You are the only one who can limit your greatness",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"author": "Ralph Waldo Emerson",
|
||||
"quote": "Make the most of yourself...for that is all there is of you",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"author": "E.E Cummings",
|
||||
"quote": "It takes courage to grow up and become who you really are.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"author": "Mark Twain",
|
||||
"quote": "The two most important days in your life are the day you are born and the day you find out why.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"author": "Munia Khan",
|
||||
"quote": "You can find a better you inside of you. Why don’t you search for that?",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"author": "George Eliot",
|
||||
"quote": "It is never too late to be what you might have been.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"author": "Abraham Lincoln",
|
||||
"quote": "Whatever you are, be a good one.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"author": "Al Goldstein",
|
||||
"quote": "The true success is the person who invented himself.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"author": "Anna Quindlen",
|
||||
"quote": "Give up on being perfect and start working on becoming yourself.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"author": "Robert De Niro",
|
||||
"quote": "Time goes on. So whatever you’re going to do, do it. Do it now. Don’t wait.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"author": "Walt Disney",
|
||||
"quote": "All our dreams can come true, if we have the courage to pursue them.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 13,
|
||||
"author": "Confucius",
|
||||
"quote": "It does not matter how slowly you go as long as you do not stop.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 14,
|
||||
"author": "Roy T. Bennett",
|
||||
"quote": "Believe in yourself. You are braver than you think, more talented than you know, and capable of more than you imagine.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 15,
|
||||
"author": "Wayne Dyer",
|
||||
"quote": "If you believe it will work out, you’ll see opportunities. If you believe it won’t, you will see obstacles.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 16,
|
||||
"author": "George Addair",
|
||||
"quote": "Everything you’ve ever wanted is on the other side of fear",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 17,
|
||||
"author": "Winston Churchill",
|
||||
"quote": "Success is not final, failure is not fatal: it is the courage to continue that counts.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 18,
|
||||
"author": "Paulo Coelho",
|
||||
"quote": "There is only one thing that makes a dream impossible to achieve: the fear of failure.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 19,
|
||||
"author": "Brian Tracy",
|
||||
"quote": "Your true success in life begins only when you make the commitment to become excellent at what you do.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 20,
|
||||
"author": "Chantal Sutherland",
|
||||
"quote": "Believe in yourself, take on your challenges, dig deep within yourself to conquer fears. Never let anyone bring you down. You got to keep going",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 21,
|
||||
"author": "Les Brown",
|
||||
"quote": "Too many of us are not living our dreams because we are living our fears.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 22,
|
||||
"author": "Bob Riley",
|
||||
"quote": "Hard times don’t create heroes. It is during the hard times when the ‘hero’ within us is revealed.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 23,
|
||||
"author": "Jack Canfield",
|
||||
"quote": "If you can tune into your purpose and really align with it, setting goals so that your vision is an expression of that purpose, then life flows much more easily.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 24,
|
||||
"author": "Napoleon Hill",
|
||||
"quote": "Whatever the mind can conceive and believe, it can achieve.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 25,
|
||||
"author": "Jim Rohn",
|
||||
"quote": "Don’t wish it were easier. Wish you were better.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 26,
|
||||
"author": "Serena Williams",
|
||||
"quote": "A champion is defined not by their wins but by how they can recover when they fall.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 27,
|
||||
"author": "Sheryl Sandberg",
|
||||
"quote": "Motivation comes from working on things we care about.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 28,
|
||||
"author": "Reese Witherspoon",
|
||||
"quote": "With the right kind of coaching and determination you can accomplish anything.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 29,
|
||||
"author": "Hazrat Inayat Khan",
|
||||
"quote": "Some people look for a beautiful place. Others make a place beautiful.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 30,
|
||||
"author": "Albert Einstein",
|
||||
"quote": "Life is like riding a bicycle. To keep your balance, you must keep moving.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 31,
|
||||
"author": "Walt Disney",
|
||||
"quote": "The way to get started is to quit talking and begin doing.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 32,
|
||||
"author": "Winston Churchill",
|
||||
"quote": "A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 33,
|
||||
"author": "Will Rogers",
|
||||
"quote": "Don't let yesterday take up too much of today.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 34,
|
||||
"author": "Vince Lombardi",
|
||||
"quote": "It's not whether you get knocked down, it's whether you get up.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 35,
|
||||
"author": "Steve Jobs",
|
||||
"quote": "If you are working on something that you really care about, you don’t have to be pushed. The vision pulls you.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 36,
|
||||
"author": "DJ Khaled",
|
||||
"quote": "You gotta water your plants. Nobody can water them for you.",
|
||||
"language": "English"
|
||||
},
|
||||
{
|
||||
"id": 37,
|
||||
"author": "Stephen Hawking",
|
||||
"quote": "However difficult life may seem, there is always something you can do and succeed at.",
|
||||
"language": "English"
|
||||
}
|
||||
]
|
||||
98
src/thread.rs
Normal file
98
src/thread.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
use std::thread;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::mpsc;
|
||||
|
||||
enum Message {
|
||||
NewJob(Job),
|
||||
Terminate,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct ThreadPool {
|
||||
workers: Vec<Worker>,
|
||||
sender: mpsc::Sender<Message>,
|
||||
}
|
||||
|
||||
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||
|
||||
impl ThreadPool {
|
||||
pub fn new(size: usize) -> ThreadPool {
|
||||
assert!(size > 0);
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let receiver = Arc::new(Mutex::new(receiver));
|
||||
|
||||
let mut workers = Vec::with_capacity(size);
|
||||
|
||||
for id in 0..size {
|
||||
workers.push(Worker::new(id, Arc::clone(&receiver)));
|
||||
}
|
||||
|
||||
ThreadPool { workers, sender }
|
||||
}
|
||||
|
||||
pub fn execute<F>(&self, f: F)
|
||||
where
|
||||
F: FnOnce() + Send + 'static,
|
||||
{
|
||||
let job = Box::new(f);
|
||||
|
||||
self.sender.send(Message::NewJob(job)).unwrap();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Drop for ThreadPool {
|
||||
fn drop(&mut self) {
|
||||
println!("Sending terminate message to all workers.");
|
||||
|
||||
for _ in &self.workers {
|
||||
self.sender.send(Message::Terminate).unwrap();
|
||||
}
|
||||
|
||||
println!("Shutting down all workers");
|
||||
for worker in &mut self.workers {
|
||||
println!("Shutting down worker {}", worker.id);
|
||||
|
||||
if let Some(thread) = worker.thread.take() {
|
||||
thread.join().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
struct Worker {
|
||||
id: usize,
|
||||
thread: Option<thread::JoinHandle<()>>
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
|
||||
let thread = thread::spawn(move || loop {
|
||||
let message = receiver
|
||||
.lock()
|
||||
.expect("[Worker] Failed to get Mutex, possibly in poisoned state")
|
||||
.recv()
|
||||
.unwrap();
|
||||
|
||||
match message {
|
||||
Message::NewJob(job) => {
|
||||
println!("[Worker {}] got a job; executing.", id);
|
||||
|
||||
job();
|
||||
}
|
||||
Message::Terminate => {
|
||||
println!("[Worker {}] was told to terminate.", id);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Worker {
|
||||
id,
|
||||
thread: Some(thread)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user