@@ 6,7 6,15 @@
* See LICENSE for licensing information.
*/
-use actix_web::{get, middleware::Logger, App, HttpResponse, HttpServer, Responder};
+use actix_web::{get, middleware::Logger, web, App, HttpResponse, HttpServer, Responder};
+
+use diesel::{
+ self,
+ r2d2::{self, ConnectionManager},
+ PgConnection,
+};
+
+type PgPool = r2d2::Pool<ConnectionManager<PgConnection>>;
#[get("/ping")]
async fn ping() -> impl Responder {
@@ 20,8 28,20 @@ async fn main() -> std::io::Result<()> {
// timestamp status_code latency remote_ip request_details
let log_format = "%t | %s | %Dms | %a | \"%r\"";
- HttpServer::new(|| App::new().wrap(Logger::new(log_format)).service(ping))
- .bind(("0.0.0.0", 8080))?
- .run()
- .await
+ // setup db pool
+ let db_url = std::env::var("LAURELIN_DB_URL").expect("LAURELIN_DB_URL");
+ let manager = ConnectionManager::<PgConnection>::new(db_url);
+ let pg_pool = r2d2::Pool::builder()
+ .build(manager)
+ .expect("failed to create pool");
+
+ HttpServer::new(move || {
+ App::new()
+ .app_data(web::Data::new(pg_pool.clone()))
+ .wrap(Logger::new(log_format))
+ .service(ping)
+ })
+ .bind(("0.0.0.0", 8080))?
+ .run()
+ .await
}