mirror of
https://github.com/cupcakearmy/cryptgeon.git
synced 2024-12-22 08:16:28 +00:00
add healthcheck endpoint and startup check
This commit is contained in:
parent
e02224216a
commit
e2711cc887
@ -1,5 +1,6 @@
|
||||
use actix_web::web;
|
||||
|
||||
use crate::health;
|
||||
use crate::note;
|
||||
use crate::status;
|
||||
|
||||
@ -7,6 +8,7 @@ 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
packages/backend/src/health/mod.rs
Normal file
3
packages/backend/src/health/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod routes;
|
||||
|
||||
pub use routes::*;
|
16
packages/backend/src/health/routes.rs
Normal file
16
packages/backend/src/health/routes.rs
Normal file
@ -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)
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
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;
|
||||
@ -10,6 +12,7 @@ extern crate lazy_static;
|
||||
mod api;
|
||||
mod client;
|
||||
mod config;
|
||||
mod health;
|
||||
mod note;
|
||||
mod size;
|
||||
mod status;
|
||||
@ -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"))
|
||||
|
@ -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(¬e.clone()).unwrap();
|
||||
let mut conn = get_connection()?;
|
||||
|
Loading…
Reference in New Issue
Block a user