Move pool creation to main function

This commit is contained in:
Konrad Borowski 2019-08-09 09:50:40 +02:00
parent 7d07c7b516
commit c4d5da68f2
2 changed files with 6 additions and 6 deletions

View File

@ -16,7 +16,10 @@ fn main() -> Result<(), Box<dyn Error>> {
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(())
}

View File

@ -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<Extract = (impl Reply,)> {
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<Extract = (impl Reply,)> {
let pool = warp::any().map(move || pool);
let index = warp::path::end()
.and(warp::get2())