1use std::fmt::{Debug, Formatter};
2#[allow(unused_imports)]
3use std::sync::Arc;
4
5use crate::Environment;
6#[cfg(feature = "jwt")]
7use crate::helpers::jwt::Jwt;
8#[cfg(feature = "crypto")]
9use crate::helpers::password::Password;
10#[cfg(feature = "rabbitmq")]
11use crate::rabbitmq::RabbitMQ;
12#[cfg(feature = "redis")]
13use crate::redis::Redis;
14#[cfg(feature = "templating")]
15use tera::{Context, Tera};
16
17#[derive(Clone)]
18pub struct FoxtiveState {
19 pub env: Environment,
20 pub app_code: String,
21 pub app_name: String,
22 pub app_key: String,
23 pub app_private_key: String,
24 pub app_public_key: String,
25 pub app_env_prefix: String,
26
27 #[cfg(feature = "database")]
28 pub(crate) database: crate::database::DBPool,
29
30 #[cfg(feature = "templating")]
31 pub(crate) tera: Tera,
32
33 #[cfg(feature = "redis")]
34 pub(crate) redis_pool: deadpool_redis::Pool,
35 #[cfg(feature = "redis")]
36 pub(crate) redis: Arc<Redis>,
37
38 #[cfg(feature = "rabbitmq")]
39 pub rabbitmq_pool: deadpool_lapin::Pool,
40 #[cfg(feature = "rabbitmq")]
41 pub rabbitmq: Arc<tokio::sync::Mutex<RabbitMQ>>,
42
43 #[cfg(feature = "jwt")]
45 pub jwt_iss_public_key: String,
46
47 #[cfg(feature = "jwt")]
49 pub jwt_token_lifetime: i64,
50
51 #[cfg(feature = "cache")]
52 pub cache: Arc<crate::cache::Cache>,
53
54 pub helpers: FoxtiveHelpers,
55}
56
57#[derive(Clone)]
58pub struct FoxtiveHelpers {
59 #[cfg(feature = "jwt")]
60 pub jwt: Arc<Jwt>,
61 #[cfg(feature = "crypto")]
62 pub password: Arc<Password>,
63}
64
65impl FoxtiveState {
66 #[cfg(feature = "database")]
67 pub fn database(&self) -> &crate::database::DBPool {
68 &self.database
69 }
70
71 #[cfg(feature = "redis")]
72 pub fn redis(&self) -> Arc<Redis> {
73 self.redis.clone()
74 }
75
76 #[cfg(feature = "rabbitmq")]
77 pub fn rabbitmq(&self) -> Arc<tokio::sync::Mutex<RabbitMQ>> {
78 Arc::clone(&self.rabbitmq)
79 }
80
81 pub fn title(&self, text: &str) -> String {
82 format!("{} - {}", text, self.app_name)
83 }
84
85 #[cfg(feature = "templating")]
86 pub fn render(&self, mut file: String, context: Context) -> crate::results::AppResult<String> {
87 if !file.ends_with(".tera.html") {
88 file.push_str(".tera.html");
89 }
90
91 self.tera.render(&file, &context).map_err(crate::Error::msg)
92 }
93}
94
95impl Debug for FoxtiveState {
96 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
97 f.write_str("application state")
98 }
99}