pam anderson is going to love me.

This commit is contained in:
Erica Marigold 2023-06-01 17:36:58 +05:30
parent 35c0356a88
commit 37e11d62ee
No known key found for this signature in database
GPG key ID: DA2C44572707FB1C
12 changed files with 220 additions and 33 deletions

View file

@ -3,5 +3,6 @@
"./framework/Cargo.toml",
"./backend/Cargo.toml",
"./Cargo.toml"
]
],
"rust-analyzer.showUnlinkedFileNotification": false
}

33
Cargo.lock generated
View file

@ -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"

33
backend/Cargo.lock generated
View file

@ -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"

View file

@ -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"

12
backend/src/api.rs Normal file
View file

@ -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;
}

View file

@ -0,0 +1 @@
pub mod random;

View file

@ -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<String, std::io::Error> {
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<T>
where
T: Into<Body>,
{
status: u16,
content_type: &'static str,
contents: T,
}
fn build_response<T>(opts: ResponseOptions<T>) -> Response
where
T: Into<Body>,
{
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::<Params>().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::<Vec<u8>>(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::<Vec<u8>>(ResponseOptions {
status: 200,
content_type: "image/gif",
contents: seal,
});
Ok(resp)
}
&_ => {
let resp = build_response::<Value>(ResponseOptions {
status: 400,
content_type: "application/json",
contents: json!({
"message": "invalid `type` query param"
}),
});
Ok(resp)
}
};
return res;
}

View file

@ -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<Server> {
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?;

View file

@ -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
}

29
framework/Cargo.lock generated
View file

@ -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"

View file

@ -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" }

View file

@ -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<u16>,
pub bulk_routes: Option<HashMap<&'a str, T>>,
@ -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)
})?;