first commit

This commit is contained in:
creations 2025-05-31 18:20:37 -04:00
commit 2da598738b
Signed by: creations
GPG key ID: 8F553AA4320FC711
15 changed files with 3758 additions and 0 deletions

11
src/db/mod.rs Normal file
View file

@ -0,0 +1,11 @@
pub mod postgres;
pub mod redis_helper;
pub type Db = sqlx::PgPool;
pub type Redis = redis::aio::MultiplexedConnection;
#[derive(Clone)]
pub struct AppState {
pub db: Db,
pub redis: Redis,
}

27
src/db/postgres.rs Normal file
View file

@ -0,0 +1,27 @@
use sqlx::{postgres::PgPoolOptions, PgPool};
use std::env;
pub async fn connect() -> PgPool {
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is required");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&db_url)
.await
.expect("Failed to connect to Postgres");
sqlx::query(
r#"
CREATE TABLE IF NOT EXISTS timezones (
user_id TEXT PRIMARY KEY,
username TEXT NOT NULL,
timezone TEXT NOT NULL
);
"#,
)
.execute(&pool)
.await
.expect("Failed to create timezones table");
pool
}

12
src/db/redis_helper.rs Normal file
View file

@ -0,0 +1,12 @@
use redis::Client;
use redis::aio::MultiplexedConnection;
use std::env;
pub async fn connect() -> MultiplexedConnection {
let url = env::var("REDIS_URL").expect("REDIS_URL is required");
let client = Client::open(url).expect("Failed to create Redis client");
client
.get_multiplexed_tokio_connection()
.await
.expect("Failed to connect to Redis")
}