-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from filecoin-project/rkh-and-notary-collections
add `notary` and `rkh` and `logs` collections wtih simple find/insert functions
- Loading branch information
Showing
14 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ members = [ | |
"http-server", | ||
"fplus" | ||
] | ||
|
||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod logs; | ||
pub mod notary; | ||
pub mod rkh; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod collections; | ||
pub mod common; | ||
pub mod setup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |