implemented working cache
some testing still has to be done
This commit is contained in:
@ -1,11 +1,13 @@
|
||||
use axum::{http::Method, Router};
|
||||
use axum::Router;
|
||||
use config::config;
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use fred::prelude::*;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use std::fs::File;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::signal;
|
||||
use tokio::sync::Mutex;
|
||||
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
|
||||
use tower_http::{
|
||||
cors::{Any, CorsLayer},
|
||||
@ -84,6 +86,11 @@ async fn main() {
|
||||
// grabbing the database url from our env variables
|
||||
let db_connection_str = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string());
|
||||
let redis_url = match std::env::var("REDIS_URL").unwrap().as_str() {
|
||||
// TODO: fix the unwrap ^
|
||||
"" => "redis://localhost:6379".to_string(),
|
||||
x => x.to_string(),
|
||||
};
|
||||
|
||||
// set up connection pool
|
||||
let pool = PgPoolOptions::new()
|
||||
@ -93,7 +100,24 @@ async fn main() {
|
||||
.await
|
||||
.expect("Failed to connect to database");
|
||||
|
||||
let app_state = AppState { db: pool.clone() };
|
||||
let pool_size = 8;
|
||||
let config = Config::from_url(&redis_url).unwrap(); // TODO: fix the unwrap <<<
|
||||
|
||||
let redis_pool = Builder::from_config(config)
|
||||
.with_performance_config(|config| {
|
||||
config.default_command_timeout = Duration::from_secs(60);
|
||||
})
|
||||
.set_policy(ReconnectPolicy::new_exponential(0, 100, 30_000, 2))
|
||||
.build_pool(pool_size)
|
||||
.expect("Failed to create cache pool");
|
||||
|
||||
if std::env::var("REDIS_URL").unwrap() != "" {
|
||||
// TODO: fix the unwrap ^
|
||||
redis_pool.init().await.expect("Failed to connect to cache");
|
||||
let _ = redis_pool.flushall::<i32>(false).await;
|
||||
}
|
||||
|
||||
let app_state = Arc::new(Mutex::new(state::AppInternalState::new(pool, redis_pool)));
|
||||
|
||||
// build our application with some routes
|
||||
let app = Router::new()
|
||||
|
Reference in New Issue
Block a user