Another commit to keep my CAN streak going.

This commit is contained in:
Erica Marigold 2023-05-28 22:09:42 +05:30
commit 28561851ec
No known key found for this signature in database
GPG key ID: DA2C44572707FB1C
12 changed files with 4084 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
**/target

7
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,7 @@
{
"rust-analyzer.linkedProjects": [
"./framework/Cargo.toml",
"./backend/Cargo.toml",
"./Cargo.toml"
]
}

7
Cargo.lock generated Normal file
View 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
View 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

File diff suppressed because it is too large Load diff

12
backend/Cargo.toml Normal file
View 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
View 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
View 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

File diff suppressed because it is too large Load diff

10
framework/Cargo.toml Normal file
View 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
View 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
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}