Skip to content

Instantly share code, notes, and snippets.

@tjmaynes
Created October 12, 2021 18:09
Show Gist options
  • Save tjmaynes/d6443a36d3a965d486c193fe8019a822 to your computer and use it in GitHub Desktop.
Save tjmaynes/d6443a36d3a965d486c193fe8019a822 to your computer and use it in GitHub Desktop.
use actix_web::{web, App, HttpServer, HttpResponse};
use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
async fn get_health_status() -> HttpResponse {
HttpResponse::Ok()
.content_type("application/json")
.body("Healthy!")
}
struct AppState {
db_conn: Pool<Postgres>
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let database_url = std::env::var("DATABASE_URL").expect("Should set 'DATABASE_URL'");
let db_conn = PgPoolOptions::new()
.max_connections(5)
.connect_timeout(Duration::from_secs(2))
.connect(database_url.as_str())
.await
.expect("Should have created a database connection");
let app_state = web::Data::new(AppState {
db_conn: db_conn
});
HttpServer::new(move || {
App::new()
.app_data(app_state.clone())
.route("/health", web::get().to(get_health_status))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment