Some decentralize

This commit is contained in:
2024-03-22 14:09:27 +05:00
parent 54c82537b7
commit a6ab0d0945
5 changed files with 187 additions and 91 deletions

36
src/api.rs Normal file
View File

@ -0,0 +1,36 @@
mod v0;
use {
hyper::{
StatusCode,
Request,
header::HeaderValue,
body::{
Incoming,
},
},
serde_json::{
Value as Json,
json,
},
skytable::pool::ConnectionMgrTcp,
bb8::Pool,
std::sync::Arc,
};
type Res<T, E> = std::result::Result<T, E>;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
type DBPool = Arc<Pool<ConnectionMgrTcp>>;
pub async fn endpoint(req: Request<Incoming>, pool: DBPool) -> (String, StatusCode, HeaderValue) {
let uri: &str = req.uri().path().as_ref();
let res: Json = match &uri[4..uri.len()] {
"/test" => json!({"error": false, "msg": "test"}),
x if x.starts_with("/v0/") => v0::api(req, pool.clone()).await,
_ => json!({"error": true, "msg": "No endpoint"})
};
let restype: HeaderValue = "application/json".parse().unwrap();
(res.to_string(), StatusCode::IM_A_TEAPOT, restype)
}