Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add database crate #31

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
members = [
"lib",
"http-server",
"fplus"
"fplus",
"database"
]

resolver = "2"
13 changes: 13 additions & 0 deletions database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "database"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.4.0"
anyhow = "1.0.75"
dotenv = "0.15.0"
mongodb = "2.7.0"
serde = "1.0.188"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Serialize, Deserialize};
use std::sync::Mutex;
use anyhow::Result;

use crate::db::common::get_collection;
use crate::core::common::get_collection;

const COLLECTION_NAME: &str = "logs";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Serialize, Deserialize};
use std::sync::Mutex;
use anyhow::Result;

use crate::db::common::get_collection;
use crate::core::common::get_collection;

const COLLECTION_NAME: &str = "notary";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Serialize, Deserialize};
use std::sync::Mutex;
use anyhow::Result;

use crate::db::common::get_collection;
use crate::core::common::get_collection;

const COLLECTION_NAME: &str = "rkh";

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod core;
1 change: 1 addition & 0 deletions http-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ reqwest = { version = "0.11.18", features = ["json"] }
futures = "0.3.28"
mongodb = "2.6.1"
lib = { path = "../lib" }
database = { path = "../database" }
anyhow = "1.0.75"
async-trait = "0.1.73"

7 changes: 3 additions & 4 deletions http-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use actix_web::middleware::Logger;
use actix_web::{App, HttpServer, web};
use std::sync::Mutex;
use actix_web::{web, App, HttpServer};
use env_logger;
use std::sync::Mutex;

pub(crate) mod db;
pub(crate) mod router;

/// Http Server Setup
Expand All @@ -23,7 +22,7 @@ pub(crate) mod router;
#[tokio::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));
let client =match db::setup::setup().await {
let client = match database::core::setup::setup().await {
Ok(client) => client,
Err(e) => panic!("Error setting up database: {}", e),
};
Expand Down
7 changes: 3 additions & 4 deletions http-server/src/router/logs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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 {
match database::core::collections::logs::find(db_connection).await {
Ok(i) => HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&i).unwrap()),
Expand All @@ -16,9 +15,9 @@ pub async fn get(db_connection: web::Data<Mutex<Client>>) -> HttpResponse {
#[post("/logs")]
pub async fn post(
db_connection: web::Data<Mutex<Client>>,
rkh: web::Json<db::collections::logs::Log>,
rkh: web::Json<database::core::collections::logs::Log>,
) -> HttpResponse {
match db::collections::logs::insert(db_connection, rkh.into_inner()).await {
match database::core::collections::logs::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
Expand Down
2 changes: 1 addition & 1 deletion http-server/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod rkh;
#[get("/health")]
pub async fn health(client: actix_web::web::Data<Mutex<mongodb::Client>>) -> impl Responder {
let client = client.lock().unwrap();
match crate::db::setup::db_health_check(client.clone()).await {
match database::core::setup::db_health_check(client.clone()).await {
Ok(_) => HttpResponse::Ok().body("OK"),
Err(e) => HttpResponse::InternalServerError().body(format!("Error: {}", e)),
}
Expand Down
7 changes: 3 additions & 4 deletions http-server/src/router/notary.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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 {
match database::core::collections::notary::find(db_connection).await {
Ok(i) => HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&i).unwrap()),
Expand All @@ -16,9 +15,9 @@ pub async fn get(db_connection: web::Data<Mutex<Client>>) -> HttpResponse {
#[post("/notary")]
pub async fn post(
db_connection: web::Data<Mutex<Client>>,
rkh: web::Json<db::collections::notary::Notary>,
rkh: web::Json<database::core::collections::notary::Notary>,
) -> HttpResponse {
match db::collections::notary::insert(db_connection, rkh.into_inner()).await {
match database::core::collections::notary::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
Expand Down
7 changes: 3 additions & 4 deletions http-server/src/router/rkh.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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 {
match database::core::collections::rkh::find(db_connection).await {
Ok(i) => HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&i).unwrap()),
Expand All @@ -16,9 +15,9 @@ pub async fn get(db_connection: web::Data<Mutex<Client>>) -> HttpResponse {
#[post("/rkh")]
pub async fn post(
db_connection: web::Data<Mutex<Client>>,
rkh: web::Json<db::collections::rkh::RootKeyHolder>,
rkh: web::Json<database::core::collections::rkh::RootKeyHolder>,
) -> HttpResponse {
match db::collections::rkh::insert(db_connection, rkh.into_inner()).await {
match database::core::collections::rkh::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
Expand Down