aboutsummaryrefslogtreecommitdiffstats
path: root/lib/warden-core/src/state/mod.rs
blob: 18e44b802d4d3facc0fc0af3407c8545a6efe7d2 (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
28
pub(crate) mod database;
use sqlx::PgPool;
use tracing::{debug, trace};
use tracing_subscriber::EnvFilter;

use crate::{WardenError, config::Configuration};

pub type LogHandle = tracing_subscriber::reload::Handle<EnvFilter, tracing_subscriber::Registry>;

#[derive(Debug, Clone)]
pub struct AppState {
    pub log_handle: LogHandle,
    pub database: PgPool,
}

impl AppState {
    pub async fn new(log_handle: LogHandle, config: &Configuration) -> Result<Self, WardenError> {
        let database = database::connect(&config.database).await?;
        trace!("running database migrations");
        sqlx::migrate!("../../migrations").run(&database).await?;
        debug!("database up to date");

        Ok(Self {
            log_handle,
            database,
        })
    }
}