aboutsummaryrefslogtreecommitdiffstats
path: root/crates/configuration/src/state.rs
blob: de58d4b753ffb1d578903b217855dd8b4b16f3fa (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
mod cache_key;
mod routing;
mod rule;

use async_nats::jetstream::Context;
use sqlx::PgPool;
use std::{ops::Deref, sync::Arc};
use tracing::{instrument, trace};
use warden_core::configuration::ReloadEvent;
use warden_stack::{Configuration, cache::RedisManager, redis::AsyncCommands};

use crate::{
    cnfg::LocalConfig,
    server::{error::AppError, reload_stream::create_stream},
    state::cache_key::CacheKey,
};

#[derive(Clone)]
pub struct AppHandle(Arc<AppState>);

impl Deref for AppHandle {
    type Target = Arc<AppState>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Clone)]
pub struct Services {
    pub postgres: PgPool,
    pub cache: RedisManager,
    pub jetstream: Context,
}

pub struct AppState {
    pub services: Services,
    pub app_config: LocalConfig,
}

impl AppState {
    pub async fn create(
        services: Services,
        configuration: &Configuration,
    ) -> Result<AppHandle, AppError> {
        let local_config: LocalConfig = serde_json::from_value(configuration.misc.clone())?;

        local_config
            .nats
            .subject
            .split(".")
            .next()
            .ok_or_else(|| anyhow::anyhow!("expected a dot separated config for nats subjects"))?;

        create_stream(&services.jetstream, &local_config.nats).await?;

        Ok(AppHandle(Arc::new(Self {
            services,
            app_config: local_config,
        })))
    }
}

#[instrument(skip(state), err(Debug))]
pub async fn invalidate_cache(state: &AppHandle, key: CacheKey<'_>) -> Result<(), tonic::Status> {
    trace!("invalidating cache");
    let mut cache = state
        .services
        .cache
        .get()
        .await
        .map_err(|e| tonic::Status::internal(e.to_string()))?;

    cache
        .del::<_, ()>(key)
        .await
        .map_err(|e| tonic::Status::internal(e.to_string()))
}

#[instrument(skip(state), err(Debug))]
pub async fn publish_reload(
    state: &AppHandle,
    prefix: &str,
    event: ReloadEvent,
) -> Result<(), tonic::Status> {
    trace!("publishing reload event");
    state
        .services
        .jetstream
        .publish(format!("{prefix}.reload"), event.as_str_name().into())
        .await
        .map_err(|e| tonic::Status::internal(e.to_string()))?;

    Ok(())
}