mirror of
https://github.com/0x5eal/sealz.git
synced 2024-12-12 04:50:35 +00:00
pam anderson is going to love me.
This commit is contained in:
parent
35c0356a88
commit
37e11d62ee
12 changed files with 220 additions and 33 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -3,5 +3,6 @@
|
||||||
"./framework/Cargo.toml",
|
"./framework/Cargo.toml",
|
||||||
"./backend/Cargo.toml",
|
"./backend/Cargo.toml",
|
||||||
"./Cargo.toml"
|
"./Cargo.toml"
|
||||||
]
|
],
|
||||||
|
"rust-analyzer.showUnlinkedFileNotification": false
|
||||||
}
|
}
|
33
Cargo.lock
generated
33
Cargo.lock
generated
|
@ -56,6 +56,17 @@ dependencies = [
|
||||||
"opaque-debug",
|
"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]]
|
[[package]]
|
||||||
name = "android_system_properties"
|
name = "android_system_properties"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
@ -71,6 +82,14 @@ version = "1.0.71"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anymap"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/CompeyDev/signals-rs.git?rev=8a650a1#8a650a19813fce0dbada0e6238b9cfe7cd4660c5"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "arrayref"
|
name = "arrayref"
|
||||||
version = "0.3.7"
|
version = "0.3.7"
|
||||||
|
@ -302,7 +321,11 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
name = "backend"
|
name = "backend"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anymap",
|
||||||
"framework",
|
"framework",
|
||||||
|
"rand 0.8.5",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tide",
|
"tide",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -630,6 +653,7 @@ dependencies = [
|
||||||
name = "framework"
|
name = "framework"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anymap",
|
||||||
"colored",
|
"colored",
|
||||||
"serde",
|
"serde",
|
||||||
"tide",
|
"tide",
|
||||||
|
@ -756,6 +780,15 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.19"
|
version = "0.1.19"
|
||||||
|
|
33
backend/Cargo.lock
generated
33
backend/Cargo.lock
generated
|
@ -56,6 +56,17 @@ dependencies = [
|
||||||
"opaque-debug",
|
"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]]
|
[[package]]
|
||||||
name = "android_system_properties"
|
name = "android_system_properties"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
@ -71,6 +82,14 @@ version = "1.0.71"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anymap"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/CompeyDev/signals-rs.git?rev=8a650a1#8a650a19813fce0dbada0e6238b9cfe7cd4660c5"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "arrayref"
|
name = "arrayref"
|
||||||
version = "0.3.7"
|
version = "0.3.7"
|
||||||
|
@ -302,7 +321,11 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
name = "backend"
|
name = "backend"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anymap",
|
||||||
"framework",
|
"framework",
|
||||||
|
"rand 0.8.5",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tide",
|
"tide",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -624,6 +647,7 @@ dependencies = [
|
||||||
name = "framework"
|
name = "framework"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anymap",
|
||||||
"colored",
|
"colored",
|
||||||
"serde",
|
"serde",
|
||||||
"tide",
|
"tide",
|
||||||
|
@ -750,6 +774,15 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.19"
|
version = "0.1.19"
|
||||||
|
|
|
@ -8,3 +8,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
framework = { path = "../framework" }
|
framework = { path = "../framework" }
|
||||||
tide = "0.16.0"
|
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
12
backend/src/api.rs
Normal 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;
|
||||||
|
}
|
1
backend/src/api/routes/mod.rs
Normal file
1
backend/src/api/routes/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod random;
|
97
backend/src/api/routes/random.rs
Normal file
97
backend/src/api/routes/random.rs
Normal 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;
|
||||||
|
}
|
|
@ -1,12 +1,15 @@
|
||||||
|
#![feature(associated_type_bounds)]
|
||||||
|
#![feature(drain_filter)]
|
||||||
|
|
||||||
use framework::{setup_server, ServerOptions, Server};
|
use framework::{setup_server, ServerOptions, Server};
|
||||||
|
|
||||||
mod routes;
|
mod api;
|
||||||
|
|
||||||
pub async fn init() -> tide::Result<Server> {
|
pub async fn init() -> tide::Result<Server> {
|
||||||
let server = setup_server(ServerOptions {
|
let server = setup_server(ServerOptions {
|
||||||
to_expose: false,
|
to_expose: false,
|
||||||
exposed_port: None,
|
exposed_port: None,
|
||||||
bulk_routes: Some(routes::get_routes()),
|
bulk_routes: Some(api::get_routes_default()),
|
||||||
scope: "backend",
|
scope: "backend",
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
@ -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
29
framework/Cargo.lock
generated
|
@ -56,6 +56,17 @@ dependencies = [
|
||||||
"opaque-debug",
|
"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]]
|
[[package]]
|
||||||
name = "android_system_properties"
|
name = "android_system_properties"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
@ -71,6 +82,14 @@ version = "1.0.71"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anymap"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/CompeyDev/signals-rs.git?rev=8a650a1#8a650a19813fce0dbada0e6238b9cfe7cd4660c5"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "arrayref"
|
name = "arrayref"
|
||||||
version = "0.3.7"
|
version = "0.3.7"
|
||||||
|
@ -616,6 +635,7 @@ dependencies = [
|
||||||
name = "framework"
|
name = "framework"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anymap",
|
||||||
"colored",
|
"colored",
|
||||||
"serde",
|
"serde",
|
||||||
"tide",
|
"tide",
|
||||||
|
@ -742,6 +762,15 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.19"
|
version = "0.1.19"
|
||||||
|
|
|
@ -9,3 +9,4 @@ edition = "2021"
|
||||||
tide = "0.16.0"
|
tide = "0.16.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
colored = "2.0.0"
|
colored = "2.0.0"
|
||||||
|
anymap = { git = "https://github.com/CompeyDev/signals-rs.git", rev = "8a650a1" }
|
||||||
|
|
|
@ -4,16 +4,12 @@ use colored::Colorize;
|
||||||
use std::{collections::HashMap, net::SocketAddr};
|
use std::{collections::HashMap, net::SocketAddr};
|
||||||
use tide::{Endpoint, Result};
|
use tide::{Endpoint, Result};
|
||||||
|
|
||||||
|
|
||||||
pub enum ReqType {
|
pub enum ReqType {
|
||||||
GET,
|
GET,
|
||||||
POST,
|
POST,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ServerOptions<'a, T>
|
pub struct ServerOptions<'a, T> {
|
||||||
where
|
|
||||||
T: Endpoint<()>,
|
|
||||||
{
|
|
||||||
pub to_expose: bool,
|
pub to_expose: bool,
|
||||||
pub exposed_port: Option<u16>,
|
pub exposed_port: Option<u16>,
|
||||||
pub bulk_routes: Option<HashMap<&'a str, T>>,
|
pub bulk_routes: Option<HashMap<&'a str, T>>,
|
||||||
|
@ -54,13 +50,13 @@ impl Server {
|
||||||
.clone()
|
.clone()
|
||||||
.listen(SocketAddr::from(([127, 0, 0, 1], port)))
|
.listen(SocketAddr::from(([127, 0, 0, 1], port)))
|
||||||
.await
|
.await
|
||||||
.and_then(|entry| {
|
.and_then(|entry| {
|
||||||
println!(
|
println!(
|
||||||
"{} :: {} service listening at port {}",
|
"{} :: {} service listening at port {}",
|
||||||
"[framework]".bold(),
|
"[framework]".bold(),
|
||||||
scope.green(),
|
scope.green(),
|
||||||
port.to_string().yellow().bold()
|
port.to_string().yellow().bold()
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(entry)
|
Ok(entry)
|
||||||
})?;
|
})?;
|
||||||
|
|
Loading…
Reference in a new issue