feat: add redis caching
This commit is contained in:
parent
ca63a2a13c
commit
7ecc8c4270
4 changed files with 76 additions and 1 deletions
|
@ -7,6 +7,7 @@ use tokio::fs::read_to_string;
|
|||
#[derive(Debug, Deserialize)]
|
||||
pub struct ConfigBuilder {
|
||||
database: Database,
|
||||
cache_database: CacheDatabase,
|
||||
web: Option<WebBuilder>,
|
||||
}
|
||||
|
||||
|
@ -19,6 +20,15 @@ pub struct Database {
|
|||
port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct CacheDatabase {
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
host: String,
|
||||
database: Option<String>,
|
||||
port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct WebBuilder {
|
||||
url: Option<String>,
|
||||
|
@ -51,6 +61,7 @@ impl ConfigBuilder {
|
|||
|
||||
Config {
|
||||
database: self.database,
|
||||
cache_database: self.cache_database,
|
||||
web,
|
||||
}
|
||||
}
|
||||
|
@ -59,6 +70,7 @@ impl ConfigBuilder {
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
pub database: Database,
|
||||
pub cache_database: CacheDatabase,
|
||||
pub web: Web,
|
||||
}
|
||||
|
||||
|
@ -78,3 +90,33 @@ impl Database {
|
|||
.port(self.port)
|
||||
}
|
||||
}
|
||||
|
||||
impl CacheDatabase {
|
||||
pub fn url(&self) -> String {
|
||||
let mut url = String::from("redis://");
|
||||
|
||||
if let Some(username) = &self.username {
|
||||
url += username;
|
||||
}
|
||||
|
||||
if let Some(password) = &self.password {
|
||||
url += ":";
|
||||
url += password;
|
||||
}
|
||||
|
||||
if self.username.is_some() || self.password.is_some() {
|
||||
url += "@";
|
||||
}
|
||||
|
||||
url += &self.host;
|
||||
url += ":";
|
||||
url += &self.port.to_string();
|
||||
|
||||
if let Some(database) = &self.database {
|
||||
url += "/";
|
||||
url += database;
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue