Files
osirose-new/database-service/src/main.rs
raven 815cb210dc - fix: warnings about unused variables
- add: LOG_LEVEL env variable
2024-11-26 13:15:33 -05:00

84 lines
3.0 KiB
Rust

use database::database_service_server::DatabaseServiceServer;
use database_service::database;
use database_service::db::Database;
use database_service::grpc::MyDatabaseService;
use database_service::redis_cache::RedisCache;
use dotenv::dotenv;
use sqlx::postgres::PgPoolOptions;
use std::env;
use std::net::ToSocketAddrs;
use std::str::FromStr;
use std::sync::Arc;
use tokio::{select, signal};
use tonic::transport::Server;
use tracing::{info, Level};
use warp::Filter;
mod consul_registration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
tracing_subscriber::fmt()
.with_max_level(Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())).unwrap_or_else(|_| Level::INFO))
.init();
let addr = env::var("DATABASE_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1".to_string());
let port = env::var("DATABASE_SERVICE_PORT").unwrap_or_else(|_| "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 consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
let service_name = env::var("SERVICE_NAME").unwrap_or_else(|_| "database-service".to_string());
let service_address = addr.as_str();
let service_port = port.clone();
let health_check_url = format!("http://{}:{}/health", service_address, service_port);
let health_check_endpoint_addr = format!("{}:8080", service_address);
// Register service with Consul
consul_registration::register_service(
&consul_url,
service_name.as_str(),
service_address,
service_port.parse().unwrap_or(50052),
&health_check_url,
)
.await?;
// Start health-check endpoint
let health_route = warp::path!("health")
.map(|| warp::reply::with_status("OK", warp::http::StatusCode::OK));
tokio::spawn(warp::serve(health_route).run(health_check_endpoint_addr.to_socket_addrs()?.next().unwrap()));
let full_addr = format!("{}:{}", &addr, port);
let address = full_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
info!("Database Service running on {}", address);
tokio::spawn(Server::builder()
.add_service(DatabaseServiceServer::new(database_service))
.serve(address));
select! {
_ = signal::ctrl_c() => {},
}
consul_registration::deregister_service(&consul_url, service_name.as_str()).await.expect("");
info!("service {} deregistered", service_name);
Ok(())
}