From b6bf4992c06a8fab0ca5231493ff93ec083c7674 Mon Sep 17 00:00:00 2001 From: Sphereso Date: Tue, 1 Apr 2025 09:52:17 +0200 Subject: [PATCH] DB API --- .env.example | 3 +- crates/backend/Cargo.toml | 7 +- crates/backend/src/db.rs | 15 ++ crates/backend/src/main.rs | 20 ++ crates/migration/erm.drawio | 355 ++++++++++-------------------------- 5 files changed, 139 insertions(+), 261 deletions(-) create mode 100644 crates/backend/src/db.rs diff --git a/.env.example b/.env.example index 75450d7..a9091ca 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,6 @@ DB_NAME= DB_USER= DB_PASSWORD= DB_HOST= +DB_PORT= -DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME} \ No newline at end of file +DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:{DB_PORT}/${DB_NAME} \ No newline at end of file diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index fd0e4ab..b36e09b 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -13,6 +13,9 @@ actix-cors = "0.7" actix-files = "0.6" tracing-actix-web = "0.7.16" -env_logger = "0.11" +env_logger = "0.11" -serde = { version = "1", features = ["derive"] } +serde = {version = "1", features = ["derive"]} +sea-orm ={ version = "1.1", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"]} + +dotenvy = "0.15" diff --git a/crates/backend/src/db.rs b/crates/backend/src/db.rs new file mode 100644 index 0000000..89568e8 --- /dev/null +++ b/crates/backend/src/db.rs @@ -0,0 +1,15 @@ +use sea_orm::{ConnectOptions, DatabaseConnection}; + + +#[derive(Clone)] +pub struct Database { + conn: DatabaseConnection +} + +impl Database { + pub async fn new(options: ConnectOptions) -> Result { + Ok(Database { + conn: sea_orm::Database::connect(options).await? + }) + } +} \ No newline at end of file diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index 76c5d63..4e203ec 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -1,16 +1,25 @@ use actix_session::{storage::RedisSessionStore, SessionMiddleware}; use actix_web::{cookie::Key, middleware::Logger, web, App, HttpResponse, HttpServer}; +use db::Database; +use std::env; mod controller; +mod db; #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + dotenvy::dotenv().ok(); + + let database_url = build_database_url(); + + let database = Database::new(database_url.into()).await.unwrap(); let redis_conn = connect_to_redis_database().await; HttpServer::new(move || { App::new() + .app_data(web::Data::new(database.clone())) .wrap(Logger::default()) .wrap(SessionMiddleware::new( redis_conn.clone(), @@ -33,3 +42,14 @@ async fn connect_to_redis_database() -> RedisSessionStore { return store; } + + +fn build_database_url() -> String { + let db_user = dotenvy::var("DB_USER").unwrap_or("pgg".to_owned()); + let db_name = dotenvy::var("DB_NAME").unwrap_or("pgg".to_owned()); + let db_password = dotenvy::var("DB_PASSWORD").unwrap_or("pgg".to_owned()); + let db_host = dotenvy::var("DB_HOST").expect("DB_HOST must be set"); + let db_port = dotenvy::var("DB_PORT").map(|x| x.parse::().expect("DB_PORT is not a valid port")).unwrap_or(5432); + + format!("postgresql://{}:{}@{}:{}/{}", db_user, db_password, db_host, db_port, db_name) +} \ No newline at end of file diff --git a/crates/migration/erm.drawio b/crates/migration/erm.drawio index 5f0b632..f23a7d3 100644 --- a/crates/migration/erm.drawio +++ b/crates/migration/erm.drawio @@ -1,373 +1,212 @@ - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - - - - - - - - - - - - - + - - - + + + - - - + + + - + - - - + + + - - - + + + - + - - - + + + - - - + + + - + - - - + + + - - - + + + - - + + - - - - - - - - - - - - - - + - - - + + + - - - + + + - + - - - + + + - - - + + + - + - - - + + + - - - + + + - + - - - + + + - - - + + + - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - + + + - - - + + + - + - - - + + + - - - + + + - - + + - - - - + + - - - - - - - - - - - - - - - - - - - + - - - - +