summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: 75c1e11a6c3c1abd00fbb5a5e4d39f3638fab993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::fmt::Debug;

use sqlx::{Pool, Postgres};

use crate::Error;

#[derive(Debug, Clone)]
pub struct AppState {
    pub pool: Pool<Postgres>,
}

impl AppState {
    pub async fn new(uri: String) -> Result<Self, Error> {
        tracing::debug!("Attempting to connect to database...");

        let pool = sqlx::postgres::PgPoolOptions::new()
            .max_connections(10)
            .connect(&uri)
            .await?;

        tracing::info!("Connected to database: {uri}");

        sqlx::migrate!().run(&pool).await?;

        Ok(Self { pool })
    }
}