Skip to content

Commit

Permalink
feat: health-check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloanan committed Jul 30, 2024
1 parent ef58c2f commit fdd17be
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/routes/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub fn layer() -> axum::Router<Arc<crate::AppState>> {
.layer(ValidateRequestHeaderLayer::bearer(
&bearer_token.unwrap_or_else(|_| DEFAULT_API_KEY.to_string()),
))
// No authentication required for health check
.route("/health-check", get(health_check))
}

#[typeshare]
Expand Down Expand Up @@ -148,3 +150,18 @@ async fn delete_url(

StatusCode::NO_CONTENT.into_response()
}

async fn health_check(State(state): State<Arc<AppState>>) -> impl IntoResponse {
// Check database connection
let conn = state.db.clone();
let rows = conn.query("SELECT 1", ()).await;

match rows {
Err(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
"Database connection failed",
)
.into_response(),
Ok(_) => (StatusCode::OK, "OK").into_response(),
}
}

0 comments on commit fdd17be

Please sign in to comment.