Skip to content

Commit

Permalink
consume db client through server router
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Sep 21, 2023
1 parent aa66290 commit f7f51fe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
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)),
}
}

0 comments on commit f7f51fe

Please sign in to comment.