diff --git a/src/main.rs b/src/main.rs index 9af7456..ac367dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,10 @@ fn main() -> Result<(), Box> { env_logger::init(); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL required"); diesel_migrations::run_pending_migrations(&PgConnection::establish(&database_url)?)?; - warp::serve(routes::routes(&database_url)).run(([127, 0, 0, 1], 8080)); + let pool = &*Box::leak(Box::new( + Pool::new(ConnectionManager::new(database_url)).expect("Couldn't create a connection pool"), + )); + warp::serve(routes::routes(pool)).run(([127, 0, 0, 1], 8080)); Ok(()) } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 03a2de3..4758ba0 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -5,7 +5,7 @@ mod insert_paste; mod raw_paste; use crate::templates::{self, RenderRucte}; -use diesel::r2d2::{ConnectionManager, Pool}; +use crate::PgPool; use std::ffi::OsStr; use std::path::PathBuf; use warp::http::header::{ @@ -14,10 +14,7 @@ use warp::http::header::{ use warp::http::{Response, StatusCode}; use warp::{path, Filter, Rejection, Reply}; -pub fn routes(database_url: &str) -> impl Filter { - let pool = &*Box::leak(Box::new( - Pool::new(ConnectionManager::new(database_url)).expect("Couldn't create a connection pool"), - )); +pub fn routes(pool: &'static PgPool) -> impl Filter { let pool = warp::any().map(move || pool); let index = warp::path::end() .and(warp::get2())