Skip to content

Commit

Permalink
enhancement: webserver optional redis (#188)
Browse files Browse the repository at this point in the history
* [webserver] Add common log config like other services

* [webserver] Make redis cache optional
  • Loading branch information
joel-u410 authored Dec 10, 2024
1 parent 32b14eb commit bd3a2e1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion shared/src/log_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Display for LogFormat {
}
}

#[derive(clap::Parser)]
#[derive(clap::Parser, Clone)]
pub struct LogConfig {
#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
Expand Down
38 changes: 22 additions & 16 deletions webserver/src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use deadpool_redis::{Config, Connection, Pool as CachePool};
#[derive(Clone)]
pub struct AppState {
db: DbPool,
cache: CachePool,
cache: Option<CachePool>,
}

impl AppState {
pub fn new(db_url: String, cache_url: String) -> Self {
pub fn new(db_url: String, cache_url: Option<String>) -> Self {
let max_pool_size = env::var("DATABASE_POOL_SIZE")
.unwrap_or_else(|_| 16.to_string())
.parse::<usize>()
Expand All @@ -35,27 +35,33 @@ impl AppState {
}
};

let cache_pool = Config::from_url(cache_url)
.create_pool(Some(deadpool_redis::Runtime::Tokio1));
let cache_pool = match cache_pool {
Ok(pool) => pool,
Err(e) => {
tracing::info!("Error building redis pool: {}", e.to_string());
exit(1);
let cache = cache_url.map(|url| {
let cache_pool = Config::from_url(url)
.create_pool(Some(deadpool_redis::Runtime::Tokio1));

match cache_pool {
Ok(pool) => pool,
Err(e) => {
tracing::info!(
"Error building redis pool: {}",
e.to_string()
);
exit(1);
}
}
};
});

Self {
db: pool,
cache: cache_pool,
}
Self { db: pool, cache }
}

pub async fn get_db_connection(&self) -> Object {
self.db.get().await.unwrap()
}

pub async fn get_cache_connection(&self) -> Connection {
self.cache.get().await.unwrap()
pub async fn get_cache_connection(&self) -> Option<Connection> {
match &self.cache {
None => None,
Some(cache) => Some(cache.get().await.unwrap()),
}
}
}
7 changes: 6 additions & 1 deletion webserver/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Development,
Expand All @@ -10,7 +12,7 @@ pub struct AppConfig {
pub port: u16,

#[clap(long, env)]
pub cache_url: String,
pub cache_url: Option<String>,

#[clap(long, env)]
pub database_url: String,
Expand All @@ -20,4 +22,7 @@ pub struct AppConfig {

#[clap(long, env)]
pub tendermint_url: String,

#[clap(flatten)]
pub log: LogConfig,
}
4 changes: 1 addition & 3 deletions webserver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use webserver::config::AppConfig;
async fn main() -> anyhow::Result<()> {
let config = AppConfig::parse();

tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
config.log.init();

ApplicationServer::serve(config)
.await
Expand Down

0 comments on commit bd3a2e1

Please sign in to comment.