From a16045a49f5a1a1ad9b3aff98195f00650d6023f Mon Sep 17 00:00:00 2001 From: jbesraa Date: Thu, 5 Oct 2023 12:48:47 +0300 Subject: [PATCH 1/2] add `notary` and `rkh` and `logs` collections wtih simple find function --- Cargo.lock | 8 ++++++ Cargo.toml | 2 ++ http-server/Cargo.toml | 2 ++ http-server/src/db/collections/logs.rs | 33 +++++++++++++++++++++++ http-server/src/db/collections/mod.rs | 2 ++ http-server/src/db/collections/notary.rs | 34 ++++++++++++++++++++++++ http-server/src/db/collections/rkh.rs | 32 ++++++++++++++++++++++ http-server/src/db/common.rs | 18 +++++++++++++ http-server/src/db/mod.rs | 1 + http-server/src/main.rs | 7 +++-- http-server/src/router/logs.rs | 17 ++++++++++++ http-server/src/router/mod.rs | 3 +++ http-server/src/router/notary.rs | 17 ++++++++++++ http-server/src/router/rkh.rs | 17 ++++++++++++ 14 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 http-server/src/db/collections/notary.rs create mode 100644 http-server/src/db/collections/rkh.rs create mode 100644 http-server/src/db/common.rs create mode 100644 http-server/src/router/notary.rs create mode 100644 http-server/src/router/rkh.rs diff --git a/Cargo.lock b/Cargo.lock index 9cc31c69..101283d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -310,6 +310,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "arc-swap" version = "1.6.0" @@ -1039,6 +1045,8 @@ version = "0.1.0" dependencies = [ "actix-cors", "actix-web", + "anyhow", + "async-trait", "chrono", "dotenv", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 52ff715a..9475a6d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,5 @@ members = [ "http-server", "fplus" ] + +resolver = "2" diff --git a/http-server/Cargo.toml b/http-server/Cargo.toml index bd78928a..f012897f 100644 --- a/http-server/Cargo.toml +++ b/http-server/Cargo.toml @@ -19,4 +19,6 @@ reqwest = { version = "0.11.18", features = ["json"] } futures = "0.3.28" mongodb = "2.6.1" lib = { path = "../lib" } +anyhow = "1.0.75" +async-trait = "0.1.73" diff --git a/http-server/src/db/collections/logs.rs b/http-server/src/db/collections/logs.rs index e69de29b..c5d491be 100644 --- a/http-server/src/db/collections/logs.rs +++ b/http-server/src/db/collections/logs.rs @@ -0,0 +1,33 @@ +use actix_web::web; +use mongodb::{Client, Collection}; +use serde::{Serialize, Deserialize}; +use std::sync::Mutex; +use anyhow::Result; + +use crate::db::common::get_collection; + +const COLLECTION_NAME: &str = "logs"; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Log { + pub timestamp: String, + pub message: String, +} + +pub async fn find(state: web::Data>) -> Result> { + let logs_collection: Collection = get_collection(state, COLLECTION_NAME).await?; + let mut cursor = logs_collection.find(None, None).await?; + let mut ret = vec![]; + while let Ok(result) = cursor.advance().await { + if result { + let d = match cursor.deserialize_current() { + Ok(d) => d, + Err(_) => { continue; } + }; + ret.push(d); + } else { + break; + } + } + Ok(ret) +} diff --git a/http-server/src/db/collections/mod.rs b/http-server/src/db/collections/mod.rs index af2c2c34..4754949a 100644 --- a/http-server/src/db/collections/mod.rs +++ b/http-server/src/db/collections/mod.rs @@ -1 +1,3 @@ pub mod logs; +pub mod notary; +pub mod rkh; diff --git a/http-server/src/db/collections/notary.rs b/http-server/src/db/collections/notary.rs new file mode 100644 index 00000000..d88b2857 --- /dev/null +++ b/http-server/src/db/collections/notary.rs @@ -0,0 +1,34 @@ +use actix_web::web; +use mongodb::{Client, Collection}; +use serde::{Serialize, Deserialize}; +use std::sync::Mutex; +use anyhow::Result; + +use crate::db::common::get_collection; + +const COLLECTION_NAME: &str = "notary"; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Notary { + pub github_handle: String, + pub on_chain_address: String, +} + + +pub async fn find(state: web::Data>) -> Result> { + let notary_collection: Collection = get_collection(state, COLLECTION_NAME).await?; + let mut cursor = notary_collection.find(None, None).await?; + let mut ret = vec![]; + while let Ok(result) = cursor.advance().await { + if result { + let d = match cursor.deserialize_current() { + Ok(d) => d, + Err(_) => { continue; } + }; + ret.push(d); + } else { + break; + } + } + Ok(ret) +} diff --git a/http-server/src/db/collections/rkh.rs b/http-server/src/db/collections/rkh.rs new file mode 100644 index 00000000..14616228 --- /dev/null +++ b/http-server/src/db/collections/rkh.rs @@ -0,0 +1,32 @@ +use actix_web::web; +use mongodb::{Client, Collection}; +use serde::{Serialize, Deserialize}; +use std::sync::Mutex; +use anyhow::Result; + +use crate::db::common::get_collection; + +const COLLECTION_NAME: &str = "rkh"; + +#[derive(Debug, Serialize, Deserialize)] +pub struct RootKeyHolder { + pub github_handle: String, +} + +pub async fn find(state: web::Data>) -> Result> { + let rkh_collection: Collection = get_collection(state, COLLECTION_NAME).await?; + let mut cursor = rkh_collection.find(None, None).await?; + let mut ret = vec![]; + while let Ok(result) = cursor.advance().await { + if result { + let d = match cursor.deserialize_current() { + Ok(d) => d, + Err(_) => { continue; } + }; + ret.push(d); + } else { + break; + } + } + Ok(ret) +} diff --git a/http-server/src/db/common.rs b/http-server/src/db/common.rs new file mode 100644 index 00000000..f4e5edf6 --- /dev/null +++ b/http-server/src/db/common.rs @@ -0,0 +1,18 @@ +use actix_web::web; +use anyhow::Result; +use mongodb::{Client, Collection}; +use std::sync::Mutex; + +pub const DATABASE: &str = "fplus-db"; + +pub async fn get_collection( + state: web::Data>, + collection_name: &str, +) -> Result> { + let col: Collection = state + .lock() + .unwrap() + .database(DATABASE) + .collection(collection_name); + Ok(col) +} diff --git a/http-server/src/db/mod.rs b/http-server/src/db/mod.rs index f171fa5d..29c9fbb0 100644 --- a/http-server/src/db/mod.rs +++ b/http-server/src/db/mod.rs @@ -1,2 +1,3 @@ pub mod collections; +pub mod common; pub mod setup; diff --git a/http-server/src/main.rs b/http-server/src/main.rs index 4c1d1d84..f70f446e 100644 --- a/http-server/src/main.rs +++ b/http-server/src/main.rs @@ -27,14 +27,14 @@ async fn main() -> std::io::Result<()> { Err(e) => panic!("Error setting up database: {}", e), }; - let state = web::Data::new(Mutex::new(client)); + let db_connection = web::Data::new(Mutex::new(client)); HttpServer::new(move || { let cors = actix_cors::Cors::default() .allow_any_origin() .allow_any_method() .allow_any_header(); App::new() - .app_data(state.clone()) + .app_data(db_connection.clone()) .wrap(Logger::default()) .wrap(cors) .service(router::health) @@ -47,6 +47,9 @@ async fn main() -> std::io::Result<()> { .service(router::application::get_all_applications) .service(router::blockchain::address_allowance) .service(router::blockchain::verified_clients) + .service(router::logs::get) + .service(router::notary::get) + .service(router::rkh::get) }) .bind(("0.0.0.0", 8080))? .run() diff --git a/http-server/src/router/logs.rs b/http-server/src/router/logs.rs index e69de29b..f0b584fc 100644 --- a/http-server/src/router/logs.rs +++ b/http-server/src/router/logs.rs @@ -0,0 +1,17 @@ +use crate::db; +use actix_web::{get, http::header::ContentType, web, HttpResponse}; +use mongodb::Client; +use std::sync::Mutex; + +#[get("/logs")] +pub async fn get(db_connection: web::Data>) -> HttpResponse { + let items = match db::collections::logs::find(db_connection).await { + Ok(items) => items, + Err(_) => { + return HttpResponse::InternalServerError().finish(); + } + }; + return HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&items).unwrap()); +} diff --git a/http-server/src/router/mod.rs b/http-server/src/router/mod.rs index f781cc9b..b62f473f 100644 --- a/http-server/src/router/mod.rs +++ b/http-server/src/router/mod.rs @@ -4,6 +4,9 @@ use actix_web::{get, HttpResponse, Responder}; pub mod application; pub mod blockchain; +pub mod logs; +pub mod notary; +pub mod rkh; /// Return server health status #[get("/health")] diff --git a/http-server/src/router/notary.rs b/http-server/src/router/notary.rs new file mode 100644 index 00000000..9d4c5481 --- /dev/null +++ b/http-server/src/router/notary.rs @@ -0,0 +1,17 @@ +use crate::db; +use actix_web::{get, http::header::ContentType, web, HttpResponse}; +use mongodb::Client; +use std::sync::Mutex; + +#[get("/notary")] +pub async fn get(db_connection: web::Data>) -> HttpResponse { + let items = match db::collections::notary::find(db_connection).await { + Ok(items) => items, + Err(_) => { + return HttpResponse::InternalServerError().finish(); + } + }; + return HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&items).unwrap()); +} diff --git a/http-server/src/router/rkh.rs b/http-server/src/router/rkh.rs new file mode 100644 index 00000000..2e96e272 --- /dev/null +++ b/http-server/src/router/rkh.rs @@ -0,0 +1,17 @@ +use crate::db; +use actix_web::{get, http::header::ContentType, web, HttpResponse}; +use mongodb::Client; +use std::sync::Mutex; + +#[get("/rkh")] +pub async fn get(db_connection: web::Data>) -> HttpResponse { + let items = match db::collections::rkh::find(db_connection).await { + Ok(items) => items, + Err(_) => { + return HttpResponse::InternalServerError().finish(); + } + }; + return HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&items).unwrap()); +} From d5481434b45e9af4a03a90ac28209133971d39e8 Mon Sep 17 00:00:00 2001 From: jbesraa Date: Thu, 5 Oct 2023 12:57:41 +0300 Subject: [PATCH 2/2] add post endpoint to insert documents --- http-server/src/db/collections/logs.rs | 6 +++++ http-server/src/db/collections/notary.rs | 6 +++++ http-server/src/db/collections/rkh.rs | 6 +++++ http-server/src/router/logs.rs | 28 +++++++++++++++--------- http-server/src/router/notary.rs | 28 +++++++++++++++--------- http-server/src/router/rkh.rs | 28 +++++++++++++++--------- 6 files changed, 72 insertions(+), 30 deletions(-) diff --git a/http-server/src/db/collections/logs.rs b/http-server/src/db/collections/logs.rs index c5d491be..bbef70db 100644 --- a/http-server/src/db/collections/logs.rs +++ b/http-server/src/db/collections/logs.rs @@ -31,3 +31,9 @@ pub async fn find(state: web::Data>) -> Result> { } Ok(ret) } + +pub async fn insert(state: web::Data>, log: Log) -> Result<()> { + let log_collection: Collection = get_collection(state, COLLECTION_NAME).await?; + log_collection.insert_one(log, None).await?; + Ok(()) +} diff --git a/http-server/src/db/collections/notary.rs b/http-server/src/db/collections/notary.rs index d88b2857..e4d6d5ec 100644 --- a/http-server/src/db/collections/notary.rs +++ b/http-server/src/db/collections/notary.rs @@ -32,3 +32,9 @@ pub async fn find(state: web::Data>) -> Result> { } Ok(ret) } + +pub async fn insert(state: web::Data>, notary: Notary) -> Result<()> { + let notary_collection: Collection = get_collection(state, COLLECTION_NAME).await?; + notary_collection.insert_one(notary, None).await?; + Ok(()) +} diff --git a/http-server/src/db/collections/rkh.rs b/http-server/src/db/collections/rkh.rs index 14616228..361f45bc 100644 --- a/http-server/src/db/collections/rkh.rs +++ b/http-server/src/db/collections/rkh.rs @@ -30,3 +30,9 @@ pub async fn find(state: web::Data>) -> Result> } Ok(ret) } + +pub async fn insert(state: web::Data>, rkh: RootKeyHolder) -> Result<()> { + let rkh_collection: Collection = get_collection(state, COLLECTION_NAME).await?; + rkh_collection.insert_one(rkh, None).await?; + Ok(()) +} diff --git a/http-server/src/router/logs.rs b/http-server/src/router/logs.rs index f0b584fc..a8fb330a 100644 --- a/http-server/src/router/logs.rs +++ b/http-server/src/router/logs.rs @@ -1,17 +1,25 @@ use crate::db; -use actix_web::{get, http::header::ContentType, web, HttpResponse}; +use actix_web::{get, http::header::ContentType, web, HttpResponse, post}; use mongodb::Client; use std::sync::Mutex; #[get("/logs")] pub async fn get(db_connection: web::Data>) -> HttpResponse { - let items = match db::collections::logs::find(db_connection).await { - Ok(items) => items, - Err(_) => { - return HttpResponse::InternalServerError().finish(); - } - }; - return HttpResponse::Ok() - .content_type(ContentType::json()) - .body(serde_json::to_string(&items).unwrap()); + match db::collections::logs::find(db_connection).await { + Ok(i) => HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&i).unwrap()), + Err(_) => HttpResponse::InternalServerError().finish(), + } +} + +#[post("/logs")] +pub async fn post( + db_connection: web::Data>, + rkh: web::Json, +) -> HttpResponse { + match db::collections::logs::insert(db_connection, rkh.into_inner()).await { + Ok(_) => HttpResponse::Ok().finish(), + Err(_) => HttpResponse::InternalServerError().finish(), + } } diff --git a/http-server/src/router/notary.rs b/http-server/src/router/notary.rs index 9d4c5481..c618eb21 100644 --- a/http-server/src/router/notary.rs +++ b/http-server/src/router/notary.rs @@ -1,17 +1,25 @@ use crate::db; -use actix_web::{get, http::header::ContentType, web, HttpResponse}; +use actix_web::{get, http::header::ContentType, web, HttpResponse, post}; use mongodb::Client; use std::sync::Mutex; #[get("/notary")] pub async fn get(db_connection: web::Data>) -> HttpResponse { - let items = match db::collections::notary::find(db_connection).await { - Ok(items) => items, - Err(_) => { - return HttpResponse::InternalServerError().finish(); - } - }; - return HttpResponse::Ok() - .content_type(ContentType::json()) - .body(serde_json::to_string(&items).unwrap()); + match db::collections::notary::find(db_connection).await { + Ok(i) => HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&i).unwrap()), + Err(_) => HttpResponse::InternalServerError().finish(), + } +} + +#[post("/notary")] +pub async fn post( + db_connection: web::Data>, + rkh: web::Json, +) -> HttpResponse { + match db::collections::notary::insert(db_connection, rkh.into_inner()).await { + Ok(_) => HttpResponse::Ok().finish(), + Err(_) => HttpResponse::InternalServerError().finish(), + } } diff --git a/http-server/src/router/rkh.rs b/http-server/src/router/rkh.rs index 2e96e272..1559a960 100644 --- a/http-server/src/router/rkh.rs +++ b/http-server/src/router/rkh.rs @@ -1,17 +1,25 @@ use crate::db; -use actix_web::{get, http::header::ContentType, web, HttpResponse}; +use actix_web::{get, http::header::ContentType, web, HttpResponse, post}; use mongodb::Client; use std::sync::Mutex; #[get("/rkh")] pub async fn get(db_connection: web::Data>) -> HttpResponse { - let items = match db::collections::rkh::find(db_connection).await { - Ok(items) => items, - Err(_) => { - return HttpResponse::InternalServerError().finish(); - } - }; - return HttpResponse::Ok() - .content_type(ContentType::json()) - .body(serde_json::to_string(&items).unwrap()); + match db::collections::rkh::find(db_connection).await { + Ok(i) => HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&i).unwrap()), + Err(_) => HttpResponse::InternalServerError().finish(), + } +} + +#[post("/rkh")] +pub async fn post( + db_connection: web::Data>, + rkh: web::Json, +) -> HttpResponse { + match db::collections::rkh::insert(db_connection, rkh.into_inner()).await { + Ok(_) => HttpResponse::Ok().finish(), + Err(_) => HttpResponse::InternalServerError().finish(), + } }