11use crate :: db:: pgsql_handlers:: health_check as health_check_pgsql;
2- use actix_web:: { get, web, HttpResponse , Responder } ;
2+ use actix_web:: { get, post, web, HttpResponse , Responder } ;
3+ use crate :: types:: { AppCache , make_key} ;
34use deadpool_postgres:: Pool as PgPool ;
5+ use std:: sync:: mpsc:: Sender ;
46
57
68// Health check endpoint
@@ -15,6 +17,33 @@ async fn api_health_check() -> impl Responder {
1517async fn db_health_check ( state : web:: Data < PgPool > ) -> impl Responder {
1618 match health_check_pgsql ( & state) . await {
1719 Ok ( _) => HttpResponse :: Ok ( ) . body ( "Database is running!" ) ,
18- Err ( err) => HttpResponse :: InternalServerError ( ) . json ( format ! ( "Failed: {}" , err) ) ,
20+ Err ( err) => HttpResponse :: PreconditionFailed ( ) . json ( format ! ( "Failed: {}" , err) ) ,
1921 }
2022}
23+
24+
25+ // Cache health check
26+ #[ get( "/cache" ) ]
27+ async fn cache_health_check ( cache : web:: Data < AppCache > ) -> impl Responder {
28+ const CACHE_KEY : & str = "health_check" ;
29+ const CACHE_VALUE : & str = "Cache is running!" ;
30+ let key = make_key ( CACHE_KEY ) ;
31+
32+ cache. insert ( key, CACHE_VALUE . to_string ( ) ) . await ;
33+
34+ if let Some ( cached_value) = cache. get ( & make_key ( CACHE_KEY ) ) . await {
35+ if cached_value == CACHE_VALUE {
36+ return HttpResponse :: Ok ( ) . body ( cached_value) ;
37+ }
38+ }
39+ HttpResponse :: PreconditionFailed ( ) . body ( "Cache health check failed!" )
40+ }
41+
42+
43+ // Channel Health check
44+ #[ post( "/channel" ) ]
45+ async fn channel_health_check ( state : web:: Data < Sender < u8 > > ) -> impl Responder {
46+ // Send a 7 int to the channel
47+ let _ = state. send ( 7 ) ;
48+ HttpResponse :: Ok ( ) . body ( "Channel health check initiated!" )
49+ }
0 commit comments