1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2025-04-14 05:47:46 +00:00
git-webhook-client/src/states.rs
2022-12-26 23:43:44 +01:00

66 lines
1.4 KiB
Rust

use anyhow::Result;
use axum::extract::FromRef;
use std::sync::Arc;
use crate::{config, db, mailer};
pub struct DB {
pub pool: db::DBPool,
}
impl DB {
pub fn build() -> Result<Self> {
Ok(Self {
pool: db::establish_connection_pool()?,
})
}
}
pub struct StateConfig {
pub secret: Vec<u8>,
pub base_url: String,
pub hooks: Vec<config::Hook>,
}
impl From<config::Config> for StateConfig {
fn from(config: config::Config) -> Self {
Self {
secret: config.secret.as_bytes().to_owned(),
base_url: config.base_url,
hooks: config.hooks,
}
}
}
impl StateConfig {
pub fn get_hook(&self, clone_url: &str) -> Option<&config::Hook> {
self.hooks.iter().find(|&hook| hook.clone_url == clone_url)
}
}
#[derive(Clone, FromRef)]
pub struct AppState {
pub config: Arc<StateConfig>,
pub mailer: Arc<mailer::Mailer>,
pub db: Arc<DB>,
}
impl AppState {
pub fn build(config: config::Config, mailer: mailer::Mailer) -> Result<Self> {
Ok(Self {
config: Arc::new(StateConfig::from(config)),
mailer: Arc::new(mailer),
db: Arc::new(DB::build()?),
})
}
}
pub trait ConfigState {
fn config(&self) -> Arc<StateConfig>;
}
impl ConfigState for AppState {
fn config(&self) -> Arc<StateConfig> {
self.config.clone()
}
}