summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: efe2192e2227dcc316e2317a0a4ceeaab5acebc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};

use crate::Error;

pub struct AppState {
    pub db_pool: Pool<Postgres>,
}

impl AppState {
    pub async fn new<S: AsRef<str>>(db_url: S) -> Result<Self, Error> {
        let db_pool = PgPoolOptions::new()
            .max_connections(10)
            .connect(db_url.as_ref())
            .await?;

        Ok(Self { db_pool })
    }
}