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

Feat/db setup #12

Merged
merged 2 commits into from
Sep 21, 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
1,229 changes: 835 additions & 394 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ structopt = { version = "0.3", default-features = false }
actix-cors = "0.6.4"
reqwest = { version = "0.11.18", features = ["json"] }
futures = "0.3.28"
mongodb = "2.6.1"
Empty file added src/db/collections/logs.rs
Empty file.
1 change: 1 addition & 0 deletions src/db/collections/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod logs;
2 changes: 2 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod collections;
pub mod setup;
32 changes: 32 additions & 0 deletions src/db/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use dotenv;
use mongodb::{
bson::doc,
options::{ClientOptions, ServerApi, ServerApiVersion},
Client,
};

pub async fn db_health_check(client: Client) -> mongodb::error::Result<()> {
// Ping the server to see if you can connect to the cluster
client
.database("admin")
.run_command(doc! {"ping": 1}, None)
.await?;
println!("Pinged your deployment. You successfully connected to MongoDB!");

Ok(())
}

pub async fn setup() -> mongodb::error::Result<Client> {
let key = "MONGODB_URL";
let value = dotenv::var(key).expect("Expected a MONGODB_URL in the environment");
let mut client_options = ClientOptions::parse(value).await?;

// Set the server_api field of the client_options object to Stable API version 1
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
client_options.server_api = Some(server_api);

// Get a handle to the cluster
let client = Client::with_options(client_options)?;

Ok(client)
}
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
extern crate markdown;

use std::sync::Mutex;

use actix_web::middleware::Logger;
use actix_web::{App, HttpServer};
use actix_web::{App, HttpServer, web};
use env_logger;

pub(crate) mod b64;
pub(crate) mod core;
pub(crate) mod db;
pub(crate) mod external_services;
pub(crate) mod parsers;
pub(crate) mod router;
Expand All @@ -26,13 +29,19 @@ 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 {
Ok(client) => client,
Err(e) => panic!("Error setting up database: {}", e),
};

let state = 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())
.wrap(Logger::default())
.wrap(cors)
.service(router::health)
Expand Down
Empty file added src/router/logs.rs
Empty file.
10 changes: 8 additions & 2 deletions src/router/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::sync::Mutex;

use actix_web::{get, HttpResponse, Responder};

pub mod application;
pub mod blockchain;

/// Return server health status
#[get("/health")]
pub async fn health() -> impl Responder {
HttpResponse::Ok().body("OK")
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 {
Ok(_) => HttpResponse::Ok().body("OK"),
Err(e) => HttpResponse::InternalServerError().body(format!("Error: {}", e)),
}
}