feat: add health check endpoint and update Docker Compose healthcheck configuration
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use axum::{middleware, routing::{delete, get, post, put}, Router};
|
use axum::{http::StatusCode, middleware, routing::{delete, get, post, put}, Router};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tower_http::{cors::CorsLayer, trace::TraceLayer};
|
use tower_http::{cors::CorsLayer, trace::TraceLayer};
|
||||||
@@ -34,6 +34,10 @@ pub struct AppState {
|
|||||||
pub config: Config,
|
pub config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn health_handler() -> StatusCode {
|
||||||
|
StatusCode::OK
|
||||||
|
}
|
||||||
|
|
||||||
/// Constrói o router Axum com todas as rotas e middlewares.
|
/// Constrói o router Axum com todas as rotas e middlewares.
|
||||||
pub fn build(db: PgPool, config: Config) -> Router {
|
pub fn build(db: PgPool, config: Config) -> Router {
|
||||||
let db = Arc::new(db);
|
let db = Arc::new(db);
|
||||||
@@ -112,6 +116,7 @@ pub fn build(db: PgPool, config: Config) -> Router {
|
|||||||
.route_layer(middleware::from_fn_with_state(state.clone(), auth_middleware));
|
.route_layer(middleware::from_fn_with_state(state.clone(), auth_middleware));
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
|
.route("/health", get(health_handler))
|
||||||
.nest("/api/v1", public_routes.merge(protected_routes))
|
.nest("/api/v1", public_routes.merge(protected_routes))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
|
|||||||
+7
-1
@@ -26,8 +26,14 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
|
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
|
||||||
ports:
|
ports:
|
||||||
- "${PORT:-8080}:8080"
|
- "${PORT:-8080}:${PORT:-8080}"
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "curl -f http://localhost:${PORT:-8080}/health || exit 1"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
start_period: 10s
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user