diff --git a/.vscode/settings.json b/.vscode/settings.json index 3bd5ac9..0f4e508 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,6 @@ "./framework/Cargo.toml", "./backend/Cargo.toml", "./Cargo.toml" - ] + ], + "rust-analyzer.showUnlinkedFileNotification": false } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 805b463..491f847 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,17 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if 1.0.0", + "once_cell", + "version_check", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -71,6 +82,14 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" +[[package]] +name = "anymap" +version = "0.1.0" +source = "git+https://github.com/CompeyDev/signals-rs.git?rev=8a650a1#8a650a19813fce0dbada0e6238b9cfe7cd4660c5" +dependencies = [ + "hashbrown", +] + [[package]] name = "arrayref" version = "0.3.7" @@ -302,7 +321,11 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" name = "backend" version = "0.1.0" dependencies = [ + "anymap", "framework", + "rand 0.8.5", + "serde", + "serde_json", "tide", ] @@ -630,6 +653,7 @@ dependencies = [ name = "framework" version = "0.1.0" dependencies = [ + "anymap", "colored", "serde", "tide", @@ -756,6 +780,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + [[package]] name = "hermit-abi" version = "0.1.19" diff --git a/backend/Cargo.lock b/backend/Cargo.lock index e2ad16c..448f4cd 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -56,6 +56,17 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if 1.0.0", + "once_cell", + "version_check", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -71,6 +82,14 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" +[[package]] +name = "anymap" +version = "0.1.0" +source = "git+https://github.com/CompeyDev/signals-rs.git?rev=8a650a1#8a650a19813fce0dbada0e6238b9cfe7cd4660c5" +dependencies = [ + "hashbrown", +] + [[package]] name = "arrayref" version = "0.3.7" @@ -302,7 +321,11 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" name = "backend" version = "0.1.0" dependencies = [ + "anymap", "framework", + "rand 0.8.5", + "serde", + "serde_json", "tide", ] @@ -624,6 +647,7 @@ dependencies = [ name = "framework" version = "0.1.0" dependencies = [ + "anymap", "colored", "serde", "tide", @@ -750,6 +774,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + [[package]] name = "hermit-abi" version = "0.1.19" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index a5eb738..8740d5e 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -8,3 +8,7 @@ edition = "2021" [dependencies] framework = { path = "../framework" } tide = "0.16.0" +anymap = { git = "https://github.com/CompeyDev/signals-rs.git", rev = "8a650a1" } +serde_json = "1.0.96" +serde = { version = "1.0.163", features = ["derive"] } +rand = "0.8.5" diff --git a/backend/src/api.rs b/backend/src/api.rs new file mode 100644 index 0000000..02248a6 --- /dev/null +++ b/backend/src/api.rs @@ -0,0 +1,12 @@ +use std::collections::HashMap; +use tide::Endpoint; + +mod routes; + +pub fn get_routes_default() -> HashMap<&'static str, impl Endpoint<()>> { + let mut routes = HashMap::new(); + + routes.insert("GET::/api/random", routes::random::handler); + + return routes; +} diff --git a/backend/src/api/routes/mod.rs b/backend/src/api/routes/mod.rs new file mode 100644 index 0000000..b9f20e6 --- /dev/null +++ b/backend/src/api/routes/mod.rs @@ -0,0 +1 @@ +pub mod random; \ No newline at end of file diff --git a/backend/src/api/routes/random.rs b/backend/src/api/routes/random.rs new file mode 100644 index 0000000..24f6900 --- /dev/null +++ b/backend/src/api/routes/random.rs @@ -0,0 +1,97 @@ +use rand::Rng; +use serde_json::Value; +use std::{fs, string::String}; +use tide::{prelude::*, Body, Request, Response, Result}; + +#[derive(Deserialize)] +#[serde(default)] +struct Params { + r#type: String, +} + +impl Default for Params { + fn default() -> Self { + Self { + r#type: "any".to_string(), + } + } +} + +fn get_random_seal(seal_type: String) -> std::result::Result { + let seals = fs::read_dir("./assets/seals")?; + + let mut seals_vec = vec![]; + + for seal in seals { + seals_vec.push(seal?.path().into_os_string().into_string().unwrap()); + } + + seals_vec = seals_vec.drain_filter(|v| v.contains(&seal_type)).collect(); + + Ok((&seals_vec[rand::thread_rng().gen_range(0..seals_vec.len())]).clone()) +} + +struct ResponseOptions +where + T: Into, +{ + status: u16, + content_type: &'static str, + contents: T, +} + +fn build_response(opts: ResponseOptions) -> Response +where + T: Into, +{ + let mut resp = Response::new(opts.status); + + resp.append_header("Content-Type", opts.content_type); + resp.set_body(opts.contents); + + return resp; +} + +pub async fn handler(req: Request<()>) -> Result { + let value_type = req.query::().unwrap().r#type; + + let res = match value_type.as_str() { + "mp4" => { + let seal = fs::read(get_random_seal("mp4".to_string())?)?; + + let resp = build_response::>(ResponseOptions { + status: 200, + content_type: "video/mp4", + contents: seal, + }); + + Ok(resp) + } + + "gif" => { + let seal = fs::read(get_random_seal("gif".to_string())?)?; + + let resp = build_response::>(ResponseOptions { + status: 200, + content_type: "image/gif", + contents: seal, + }); + + Ok(resp) + } + + &_ => { + let resp = build_response::(ResponseOptions { + status: 400, + content_type: "application/json", + contents: json!({ + "message": "invalid `type` query param" + }), + }); + + Ok(resp) + } + }; + + return res; +} diff --git a/backend/src/lib.rs b/backend/src/lib.rs index f57740a..c23e666 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -1,12 +1,15 @@ +#![feature(associated_type_bounds)] +#![feature(drain_filter)] + use framework::{setup_server, ServerOptions, Server}; -mod routes; +mod api; pub async fn init() -> tide::Result { let server = setup_server(ServerOptions { to_expose: false, exposed_port: None, - bulk_routes: Some(routes::get_routes()), + bulk_routes: Some(api::get_routes_default()), scope: "backend", }) .await?; diff --git a/backend/src/routes.rs b/backend/src/routes.rs deleted file mode 100644 index a146631..0000000 --- a/backend/src/routes.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::collections::HashMap; -use tide::{prelude::*, Endpoint}; - -fn get_routes_default() -> HashMap<&'static str, impl Endpoint<()>> { - let mut routes = HashMap::new(); - - routes.insert("GET::/api/health", |_| async move { - Ok(json!({ - "status": 200, - "healthy": true, - })) - }); - - return routes; -} - -pub fn get_routes() -> HashMap<&'static str, impl Endpoint<()>> { - let routes = get_routes_default(); - - // Register additional routes here - - return routes -} diff --git a/framework/Cargo.lock b/framework/Cargo.lock index 5a3870e..a6c804d 100644 --- a/framework/Cargo.lock +++ b/framework/Cargo.lock @@ -56,6 +56,17 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if 1.0.0", + "once_cell", + "version_check", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -71,6 +82,14 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" +[[package]] +name = "anymap" +version = "0.1.0" +source = "git+https://github.com/CompeyDev/signals-rs.git?rev=8a650a1#8a650a19813fce0dbada0e6238b9cfe7cd4660c5" +dependencies = [ + "hashbrown", +] + [[package]] name = "arrayref" version = "0.3.7" @@ -616,6 +635,7 @@ dependencies = [ name = "framework" version = "0.1.0" dependencies = [ + "anymap", "colored", "serde", "tide", @@ -742,6 +762,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + [[package]] name = "hermit-abi" version = "0.1.19" diff --git a/framework/Cargo.toml b/framework/Cargo.toml index e544957..7c49578 100644 --- a/framework/Cargo.toml +++ b/framework/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" tide = "0.16.0" serde = { version = "1.0", features = ["derive"] } colored = "2.0.0" +anymap = { git = "https://github.com/CompeyDev/signals-rs.git", rev = "8a650a1" } diff --git a/framework/src/lib.rs b/framework/src/lib.rs index 3031d03..f65809b 100644 --- a/framework/src/lib.rs +++ b/framework/src/lib.rs @@ -4,16 +4,12 @@ use colored::Colorize; use std::{collections::HashMap, net::SocketAddr}; use tide::{Endpoint, Result}; - pub enum ReqType { GET, POST, } -pub struct ServerOptions<'a, T> -where - T: Endpoint<()>, -{ +pub struct ServerOptions<'a, T> { pub to_expose: bool, pub exposed_port: Option, pub bulk_routes: Option>, @@ -54,13 +50,13 @@ impl Server { .clone() .listen(SocketAddr::from(([127, 0, 0, 1], port))) .await - .and_then(|entry| { + .and_then(|entry| { println!( "{} :: {} service listening at port {}", "[framework]".bold(), scope.green(), port.to_string().yellow().bold() - ); + ); Ok(entry) })?;