-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_state.rs
51 lines (39 loc) · 1.23 KB
/
app_state.rs
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
use eyre::{eyre, Result};
use crate::clickhouse::ClickhouseLogger;
use crate::clickhouse::ClickhousePool;
use crate::config::AppConfig;
#[derive(Debug, Clone)]
#[allow(unused)]
pub struct AppState {
clickhouse_pool: ClickhousePool,
config: AppConfig,
ch_logger: ClickhouseLogger,
}
impl AppState {
pub async fn build(config: AppConfig) -> Result<Self> {
let clickhouse_pool = Self::connect_clickhouse(&config).await?;
let ch_logger = ClickhouseLogger::new(&clickhouse_pool);
Ok(Self {
clickhouse_pool,
config,
ch_logger,
})
}
async fn connect_clickhouse(config: &AppConfig) -> Result<ClickhousePool> {
let clickhouse_config = config.get_clickhouse_config()?;
let pool = ClickhousePool::connect(&clickhouse_config)
.await
.map_err(|e| eyre!("Failed to connect to Clickhouse: {}", e))?;
pool.check_pool().await?;
Ok(pool)
}
pub async fn check_clickhouse_connection(&self) -> Result<()> {
self.clickhouse_pool.check_pool().await
}
pub fn config(&self) -> &AppConfig {
&self.config
}
pub fn ch_logger(&self) -> &ClickhouseLogger {
&self.ch_logger
}
}