Refactor: Move pool creation to DbOpt.
This commit is contained in:
parent
c8b8a463f9
commit
b22fcb92d2
29
src/dbopt.rs
Normal file
29
src/dbopt.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||
use diesel::{Connection, ConnectionError};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
use structopt::StructOpt;
|
||||
|
||||
pub type PgPool = Pool<ConnectionManager<PgConnection>>;
|
||||
pub type PooledPg = PooledConnection<ConnectionManager<PgConnection>>;
|
||||
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(rename_all = "kebab-case")]
|
||||
pub struct DbOpt {
|
||||
/// How to connect to the postgres database.
|
||||
#[structopt(long, env = "DATABASE_URL", hide_env_values = true)]
|
||||
db_url: String,
|
||||
}
|
||||
|
||||
impl DbOpt {
|
||||
pub fn connect(&self) -> Result<PgConnection, ConnectionError> {
|
||||
PgConnection::establish(&self.db_url)
|
||||
}
|
||||
pub fn create_pool(&self) -> Result<PgPool, impl Error> {
|
||||
let db_manager = ConnectionManager::<PgConnection>::new(&self.db_url);
|
||||
Pool::builder()
|
||||
.connection_timeout(Duration::from_secs(1))
|
||||
.build(db_manager)
|
||||
}
|
||||
}
|
18
src/main.rs
18
src/main.rs
@ -4,6 +4,7 @@
|
||||
extern crate diesel;
|
||||
|
||||
mod adm;
|
||||
mod dbopt;
|
||||
mod fetch_places;
|
||||
mod models;
|
||||
mod myexif;
|
||||
@ -15,8 +16,7 @@ mod server;
|
||||
use crate::adm::result::Error;
|
||||
use crate::adm::stats::show_stats;
|
||||
use crate::adm::{findphotos, makepublic, precache, storestatics, users};
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::prelude::*;
|
||||
use crate::dbopt::DbOpt;
|
||||
use dotenv::dotenv;
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
@ -76,20 +76,6 @@ struct CacheOpt {
|
||||
memcached_url: String,
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(rename_all = "kebab-case")]
|
||||
struct DbOpt {
|
||||
/// How to connect to the postgres database.
|
||||
#[structopt(long, env = "DATABASE_URL", hide_env_values = true)]
|
||||
db_url: String,
|
||||
}
|
||||
|
||||
impl DbOpt {
|
||||
fn connect(&self) -> Result<PgConnection, ConnectionError> {
|
||||
PgConnection::establish(&self.db_url)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(rename_all = "kebab-case")]
|
||||
struct DirOpt {
|
||||
|
@ -1,8 +1,8 @@
|
||||
use super::Args;
|
||||
use crate::dbopt::{PgPool, PooledPg};
|
||||
use crate::fetch_places::OverpassOpt;
|
||||
use crate::photosdir::PhotosDir;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||
use diesel::r2d2::{Pool, PooledConnection};
|
||||
use log::{debug, warn};
|
||||
use medallion::{Header, Payload, Token};
|
||||
use r2d2_memcache::r2d2::Error;
|
||||
@ -13,8 +13,6 @@ use warp::filters::{cookie, header, BoxedFilter};
|
||||
use warp::path::{self, FullPath};
|
||||
use warp::{self, Filter};
|
||||
|
||||
type PgPool = Pool<ConnectionManager<PgConnection>>;
|
||||
type PooledPg = PooledConnection<ConnectionManager<PgConnection>>;
|
||||
type MemcachePool = Pool<MemcacheConnectionManager>;
|
||||
type PooledMemcache = PooledConnection<MemcacheConnectionManager>;
|
||||
|
||||
@ -54,15 +52,10 @@ struct GlobalContext {
|
||||
|
||||
impl GlobalContext {
|
||||
fn new(args: &Args) -> Self {
|
||||
let db_manager =
|
||||
ConnectionManager::<PgConnection>::new(&args.db.db_url);
|
||||
let mc_manager =
|
||||
MemcacheConnectionManager::new(args.cache.memcached_url.as_ref());
|
||||
GlobalContext {
|
||||
db_pool: Pool::builder()
|
||||
.connection_timeout(Duration::from_secs(1))
|
||||
.build(db_manager)
|
||||
.expect("Posgresql pool"),
|
||||
db_pool: args.db.create_pool().expect("Posgresql pool"),
|
||||
photosdir: PhotosDir::new(&args.photos.photos_dir),
|
||||
memcache_pool: Pool::builder()
|
||||
.connection_timeout(Duration::from_secs(1))
|
||||
|
Loading…
Reference in New Issue
Block a user