Skip to content

Commit

Permalink
Merge pull request #29 from filecoin-project/rkh-and-notary-collections
Browse files Browse the repository at this point in the history
add `notary` and `rkh` and `logs` collections wtih simple find/insert functions
  • Loading branch information
jbesraa authored Oct 5, 2023
2 parents 147b487 + d548143 commit 38d20aa
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ members = [
"http-server",
"fplus"
]

resolver = "2"
2 changes: 2 additions & 0 deletions http-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

39 changes: 39 additions & 0 deletions http-server/src/db/collections/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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<Mutex<Client>>) -> Result<Vec<Log>> {
let logs_collection: Collection<Log> = 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)
}

pub async fn insert(state: web::Data<Mutex<Client>>, log: Log) -> Result<()> {
let log_collection: Collection<Log> = get_collection(state, COLLECTION_NAME).await?;
log_collection.insert_one(log, None).await?;
Ok(())
}
2 changes: 2 additions & 0 deletions http-server/src/db/collections/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod logs;
pub mod notary;
pub mod rkh;
40 changes: 40 additions & 0 deletions http-server/src/db/collections/notary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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<Mutex<Client>>) -> Result<Vec<Notary>> {
let notary_collection: Collection<Notary> = 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)
}

pub async fn insert(state: web::Data<Mutex<Client>>, notary: Notary) -> Result<()> {
let notary_collection: Collection<Notary> = get_collection(state, COLLECTION_NAME).await?;
notary_collection.insert_one(notary, None).await?;
Ok(())
}
38 changes: 38 additions & 0 deletions http-server/src/db/collections/rkh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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<Mutex<Client>>) -> Result<Vec<RootKeyHolder>> {
let rkh_collection: Collection<RootKeyHolder> = 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)
}

pub async fn insert(state: web::Data<Mutex<Client>>, rkh: RootKeyHolder) -> Result<()> {
let rkh_collection: Collection<RootKeyHolder> = get_collection(state, COLLECTION_NAME).await?;
rkh_collection.insert_one(rkh, None).await?;
Ok(())
}
18 changes: 18 additions & 0 deletions http-server/src/db/common.rs
Original file line number Diff line number Diff line change
@@ -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<T>(
state: web::Data<Mutex<Client>>,
collection_name: &str,
) -> Result<Collection<T>> {
let col: Collection<T> = state
.lock()
.unwrap()
.database(DATABASE)
.collection(collection_name);
Ok(col)
}
1 change: 1 addition & 0 deletions http-server/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod collections;
pub mod common;
pub mod setup;
7 changes: 5 additions & 2 deletions http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
25 changes: 25 additions & 0 deletions http-server/src/router/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::db;
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<Mutex<Client>>) -> HttpResponse {
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<Mutex<Client>>,
rkh: web::Json<db::collections::logs::Log>,
) -> HttpResponse {
match db::collections::logs::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}
3 changes: 3 additions & 0 deletions http-server/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
25 changes: 25 additions & 0 deletions http-server/src/router/notary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::db;
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<Mutex<Client>>) -> HttpResponse {
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<Mutex<Client>>,
rkh: web::Json<db::collections::notary::Notary>,
) -> HttpResponse {
match db::collections::notary::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}
25 changes: 25 additions & 0 deletions http-server/src/router/rkh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::db;
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<Mutex<Client>>) -> HttpResponse {
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<Mutex<Client>>,
rkh: web::Json<db::collections::rkh::RootKeyHolder>,
) -> HttpResponse {
match db::collections::rkh::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}

0 comments on commit 38d20aa

Please sign in to comment.