Skip to content

Commit

Permalink
Add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
max-lt committed Mar 23, 2024
1 parent 415e249 commit 238065a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ async fn handle_request(data: Data<AppState>, req: HttpRequest) -> HttpResponse
response
}

async fn health_check() -> HttpResponse {
HttpResponse::Ok().body("ok")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
Expand All @@ -189,6 +193,18 @@ async fn main() -> std::io::Result<()> {

debug!("start main");

if !std::env::var("DATABASE_URL").is_ok() {
let host = std::env::var("POSTGRES_HOST").expect("POSTGRES_HOST must be set");
let port = std::env::var("POSTGRES_PORT").expect("POSTGRES_PORT must be set");
let user = std::env::var("POSTGRES_USER").expect("POSTGRES_USER must be set");
let password = std::env::var("POSTGRES_PASSWORD").expect("POSTGRES_PASSWORD must be set");
let database = std::env::var("POSTGRES_DB").expect("POSTGRES_DB must be set");

debug!("DATABASE_URL not set, using POSTGRES_* env vars");

std::env::set_var("DATABASE_URL", format!("postgres://{user}:{password}@{host}:{port}/{database}"));
}

let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let pool = PgPoolOptions::new()
.max_connections(4)
Expand All @@ -203,6 +219,7 @@ async fn main() -> std::io::Result<()> {

App::new()
.app_data(Data::new(AppState { db: pool.clone() }))
.route("/health", web::get().to(health_check))
.default_service(web::to(handle_request))
})
.bind(("127.0.0.1", 8080))?
Expand Down

0 comments on commit 238065a

Please sign in to comment.