Skip to content

Commit

Permalink
add healthcheck endpoint and startup check
Browse files Browse the repository at this point in the history
  • Loading branch information
cupcakearmy committed Jun 23, 2023
1 parent e022242 commit e2711cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/backend/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use actix_web::web;

use crate::health;
use crate::note;
use crate::status;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/api")
.service(note::init())
.service(status::init()),
.service(status::init())
.service(health::init()),
);
}
3 changes: 3 additions & 0 deletions packages/backend/src/health/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod routes;

pub use routes::*;
16 changes: 16 additions & 0 deletions packages/backend/src/health/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use actix_web::{get, web, HttpResponse, Responder, Scope};

use crate::store;

#[get("/")]
async fn get_live() -> impl Responder {
if store::can_reach_redis() {
return HttpResponse::Ok();
} else {
return HttpResponse::ServiceUnavailable();
}
}

pub fn init() -> Scope {
web::scope("/live").service(get_live)
}
10 changes: 9 additions & 1 deletion packages/backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use actix_web::{
middleware::{self, Logger},
web, App, HttpServer,
web::{self},
App, HttpServer,
};
use dotenv::dotenv;
use log::error;

#[macro_use]
extern crate lazy_static;

mod api;
mod client;
mod config;
mod health;
mod note;
mod size;
mod status;
Expand All @@ -20,6 +23,11 @@ async fn main() -> std::io::Result<()> {
dotenv().ok();
env_logger::init_from_env(env_logger::Env::new().default_filter_or(config::VERBOSITY.as_str()));

if !store::can_reach_redis() {
error!("cannot reach redis");
panic!("canont reach redis");
}

return HttpServer::new(|| {
App::new()
.wrap(Logger::new("\"%r\" %s %b %T"))
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ fn get_connection() -> Result<redis::Connection, &'static str> {
.map_err(|_| "Unable to connect to redis")
}

pub fn can_reach_redis() -> bool {
let conn = get_connection();
return match conn {
Ok(_) => true,
Err(_) => false,
};
}

pub fn set(id: &String, note: &Note) -> Result<(), &'static str> {
let serialized = serde_json::to_string(&note.clone()).unwrap();
let mut conn = get_connection()?;
Expand Down

0 comments on commit e2711cc

Please sign in to comment.