diff --git a/Cargo.lock b/Cargo.lock index 44063b09..27cb4eda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -641,6 +641,17 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" +[[package]] +name = "database" +version = "0.1.0" +dependencies = [ + "actix-web", + "anyhow", + "dotenv", + "mongodb", + "serde", +] + [[package]] name = "deranged" version = "0.3.8" @@ -1048,6 +1059,7 @@ dependencies = [ "anyhow", "async-trait", "chrono", + "database", "dotenv", "env_logger", "futures", diff --git a/Cargo.toml b/Cargo.toml index 9475a6d0..a9631805 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,8 @@ members = [ "lib", "http-server", - "fplus" + "fplus", + "database" ] resolver = "2" diff --git a/database/Cargo.toml b/database/Cargo.toml new file mode 100644 index 00000000..3e7da0ac --- /dev/null +++ b/database/Cargo.toml @@ -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" diff --git a/http-server/src/db/collections/logs.rs b/database/src/core/collections/logs.rs similarity index 96% rename from http-server/src/db/collections/logs.rs rename to database/src/core/collections/logs.rs index bbef70db..1fe41b90 100644 --- a/http-server/src/db/collections/logs.rs +++ b/database/src/core/collections/logs.rs @@ -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"; diff --git a/http-server/src/db/collections/mod.rs b/database/src/core/collections/mod.rs similarity index 100% rename from http-server/src/db/collections/mod.rs rename to database/src/core/collections/mod.rs diff --git a/http-server/src/db/collections/notary.rs b/database/src/core/collections/notary.rs similarity index 96% rename from http-server/src/db/collections/notary.rs rename to database/src/core/collections/notary.rs index e4d6d5ec..3b546503 100644 --- a/http-server/src/db/collections/notary.rs +++ b/database/src/core/collections/notary.rs @@ -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"; diff --git a/http-server/src/db/collections/rkh.rs b/database/src/core/collections/rkh.rs similarity index 96% rename from http-server/src/db/collections/rkh.rs rename to database/src/core/collections/rkh.rs index 361f45bc..bf3248d4 100644 --- a/http-server/src/db/collections/rkh.rs +++ b/database/src/core/collections/rkh.rs @@ -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"; diff --git a/http-server/src/db/common.rs b/database/src/core/common.rs similarity index 100% rename from http-server/src/db/common.rs rename to database/src/core/common.rs diff --git a/http-server/src/db/mod.rs b/database/src/core/mod.rs similarity index 100% rename from http-server/src/db/mod.rs rename to database/src/core/mod.rs diff --git a/http-server/src/db/setup.rs b/database/src/core/setup.rs similarity index 100% rename from http-server/src/db/setup.rs rename to database/src/core/setup.rs diff --git a/database/src/lib.rs b/database/src/lib.rs new file mode 100644 index 00000000..5a7ca06a --- /dev/null +++ b/database/src/lib.rs @@ -0,0 +1 @@ +pub mod core; diff --git a/http-server/Cargo.toml b/http-server/Cargo.toml index f012897f..d7043226 100644 --- a/http-server/Cargo.toml +++ b/http-server/Cargo.toml @@ -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" diff --git a/http-server/src/main.rs b/http-server/src/main.rs index fa65ce68..aaf1f3b6 100644 --- a/http-server/src/main.rs +++ b/http-server/src/main.rs @@ -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 @@ -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), }; diff --git a/http-server/src/router/logs.rs b/http-server/src/router/logs.rs index a8fb330a..95dd69b9 100644 --- a/http-server/src/router/logs.rs +++ b/http-server/src/router/logs.rs @@ -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>) -> 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()), @@ -16,9 +15,9 @@ pub async fn get(db_connection: web::Data>) -> HttpResponse { #[post("/logs")] pub async fn post( db_connection: web::Data>, - rkh: web::Json, + rkh: web::Json, ) -> 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(), } diff --git a/http-server/src/router/mod.rs b/http-server/src/router/mod.rs index b62f473f..7656d51f 100644 --- a/http-server/src/router/mod.rs +++ b/http-server/src/router/mod.rs @@ -12,7 +12,7 @@ pub mod rkh; #[get("/health")] pub async fn health(client: actix_web::web::Data>) -> 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)), } diff --git a/http-server/src/router/notary.rs b/http-server/src/router/notary.rs index c618eb21..e3a75c7c 100644 --- a/http-server/src/router/notary.rs +++ b/http-server/src/router/notary.rs @@ -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>) -> 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()), @@ -16,9 +15,9 @@ pub async fn get(db_connection: web::Data>) -> HttpResponse { #[post("/notary")] pub async fn post( db_connection: web::Data>, - rkh: web::Json, + rkh: web::Json, ) -> 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(), } diff --git a/http-server/src/router/rkh.rs b/http-server/src/router/rkh.rs index 1559a960..b35d6733 100644 --- a/http-server/src/router/rkh.rs +++ b/http-server/src/router/rkh.rs @@ -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>) -> 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()), @@ -16,9 +15,9 @@ pub async fn get(db_connection: web::Data>) -> HttpResponse { #[post("/rkh")] pub async fn post( db_connection: web::Data>, - rkh: web::Json, + rkh: web::Json, ) -> 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(), }