- add: initial database and auth services

This commit is contained in:
2024-11-25 20:45:16 -05:00
parent 6a35b5b373
commit 3ff22c9a5b
24 changed files with 817 additions and 1 deletions

View File

@@ -0,0 +1,48 @@
use dotenv::dotenv;
use std::env;
use tonic::transport::Server;
use database_service::db::Database;
use database_service::redis_cache::RedisCache;
use database::database_service_server::DatabaseServiceServer;
use std::sync::Arc;
use database_service::database;
use database_service::grpc::MyDatabaseService;
use sqlx::postgres::PgPoolOptions;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_thread_names(true)
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339())
.init();
let addr = env::var("DATABASE_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1:50052".to_string());
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let redis_url = std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
let addr = addr.parse().expect("Invalid address");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await
.expect("Failed to create PostgreSQL connection pool");
let redis_cache = RedisCache::new(&redis_url);
let cache = Arc::new(redis_cache); // Share the cache instance between tasks
let database_service = MyDatabaseService {
db: Database::new(pool, cache).await,
};
// Pass `shared_cache` into services as needed
println!("Database Service running on {}", addr);
Server::builder()
.add_service(DatabaseServiceServer::new(database_service))
.serve(addr)
.await?;
Ok(())
}