mirror of
https://github.com/0x5eal/sealz.git
synced 2024-12-12 12:50:36 +00:00
Another commit to keep my CAN streak going.
This commit is contained in:
commit
28561851ec
12 changed files with 4084 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
**/target
|
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"rust-analyzer.linkedProjects": [
|
||||||
|
"./framework/Cargo.toml",
|
||||||
|
"./backend/Cargo.toml",
|
||||||
|
"./Cargo.toml"
|
||||||
|
]
|
||||||
|
}
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sealz"
|
||||||
|
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "sealz"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2063
backend/Cargo.lock
generated
Normal file
2063
backend/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
12
backend/Cargo.toml
Normal file
12
backend/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
[package]
|
||||||
|
name = "backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
framework = { path = "../framework" }
|
||||||
|
async-std = { version = "1.8.0", features = ["attributes"] }
|
||||||
|
tide = "0.16.0"
|
||||||
|
tokio = { version = "1.28.2", features = ["full"] }
|
15
backend/src/main.rs
Normal file
15
backend/src/main.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
use framework::{setup_server, ServerOptions};
|
||||||
|
|
||||||
|
mod routes;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> tide::Result<()> {
|
||||||
|
setup_server(ServerOptions {
|
||||||
|
to_expose: true,
|
||||||
|
exposed_port: Some(3000),
|
||||||
|
bulk_routes: Some(routes::get_routes()),
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
23
backend/src/routes.rs
Normal file
23
backend/src/routes.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
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
|
||||||
|
}
|
1855
framework/Cargo.lock
generated
Normal file
1855
framework/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
10
framework/Cargo.toml
Normal file
10
framework/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "framework"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tide = "0.16.0"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
80
framework/src/lib.rs
Normal file
80
framework/src/lib.rs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
#![feature(let_chains)]
|
||||||
|
|
||||||
|
use std::{collections::HashMap, net::SocketAddr};
|
||||||
|
use tide::{Endpoint, Result};
|
||||||
|
|
||||||
|
pub enum ReqType {
|
||||||
|
GET,
|
||||||
|
POST,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ServerOptions<'a, T>
|
||||||
|
where
|
||||||
|
T: Endpoint<()>,
|
||||||
|
{
|
||||||
|
pub to_expose: bool,
|
||||||
|
pub exposed_port: Option<u16>,
|
||||||
|
pub bulk_routes: Option<HashMap<&'a str, T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This struct may be public, but is for internal use only.
|
||||||
|
/// To instantiate a server, use `setup_server` instead. It may
|
||||||
|
/// be acceptable to use `Server.add_route` though.
|
||||||
|
pub struct Server {
|
||||||
|
instance: tide::Server<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Server {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
instance: tide::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_route(&mut self, method: ReqType, route: &str, handler: impl Endpoint<()>) {
|
||||||
|
let mut route_addr = self.instance.at(route);
|
||||||
|
|
||||||
|
match method {
|
||||||
|
ReqType::GET => route_addr.get(handler),
|
||||||
|
ReqType::POST => route_addr.post(handler),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn expose(&self, port: u16) -> Result<()> {
|
||||||
|
self.instance
|
||||||
|
.clone()
|
||||||
|
.listen(SocketAddr::from(([127, 0, 0, 1], port)))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn setup_server(
|
||||||
|
opts: ServerOptions<'static, impl Endpoint<()>>,
|
||||||
|
) -> std::result::Result<Server, tide::Error> {
|
||||||
|
let mut server = Server::new();
|
||||||
|
|
||||||
|
if let Some(routes_map) = opts.bulk_routes {
|
||||||
|
for (route, handler) in routes_map {
|
||||||
|
let meta: Vec<&str> = route.split("::").collect();
|
||||||
|
|
||||||
|
let method = match meta[0] {
|
||||||
|
"GET" => ReqType::GET,
|
||||||
|
"POST" => ReqType::POST,
|
||||||
|
|
||||||
|
&_ => panic!("framework::setup_serer::bulk_routes -> invalid request method type"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let route = meta[1];
|
||||||
|
|
||||||
|
server.add_route(method, route, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.to_expose && let Some(port) = opts.exposed_port {
|
||||||
|
server.expose(port).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(server)
|
||||||
|
}
|
3
src/main.rs
Normal file
3
src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
Loading…
Reference in a new issue