sharenet/backend/crates/sharenet-api-postgres/src/main.rs
continuist 69ebda5e3d
All checks were successful
Podman Rootless Demo / test-backend (push) Has been skipped
Podman Rootless Demo / test-frontend (push) Has been skipped
Podman Rootless Demo / build-backend (push) Successful in 5m16s
Podman Rootless Demo / build-frontend (push) Has been skipped
Podman Rootless Demo / deploy-prod (push) Successful in 58s
Change where backend is looking for the migrations
2025-09-21 21:16:08 -04:00

47 lines
No EOL
1.4 KiB
Rust

use std::net::SocketAddr;
use std::str::FromStr;
use api::run as run_api;
use application::Service;
use domain::{User, Product};
use postgres::{PostgresProductRepository, PostgresUserRepository};
use sqlx::postgres::PgPoolOptions;
use dotenvy;
use std::env;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Load environment variables from config file
dotenvy::from_path("config/api-postgres.env").ok();
// Get configuration from environment variables
let host = env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
let port = env::var("PORT").unwrap_or_else(|_| "3001".to_string());
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
// Create database pool
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await?;
// Run database migrations
sqlx::migrate!("../../migrations")
.run(&pool)
.await?;
// Create repositories
let user_repo = PostgresUserRepository::new(pool.clone());
let product_repo = PostgresProductRepository::new(pool);
// Create services
let user_service = Service::<User, _>::new(user_repo);
let product_service = Service::<Product, _>::new(product_repo);
// Run API server
let addr = format!("{}:{}", host, port);
let addr = SocketAddr::from_str(&addr)?;
run_api(addr, user_service, product_service).await;
Ok(())
}