diff --git a/backend-rust/src/config.rs b/backend-rust/src/config.rs index e69de29..5c82f7c 100644 --- a/backend-rust/src/config.rs +++ b/backend-rust/src/config.rs @@ -0,0 +1,21 @@ +use std::env; + +#[derive(Debug, Clone)] +pub struct Config { + pub db_path: String, + pub migration_path: String, +} + +impl Config { + pub fn from_env() -> Self { + Self { + db_path: env::var("DB_URL").unwrap_or_else(|_| "owlynews.sqlite3".to_string()), + migration_path: std::env::var("MIGRATION_PATH") + .unwrap_or_else(|_| "./migrations".to_string()), + } + } + + pub fn database_url(&self) -> String { + format!("sqlite:{}", self.db_path) + } +} diff --git a/backend-rust/src/db.rs b/backend-rust/src/db.rs index 575b985..fcd9564 100644 --- a/backend-rust/src/db.rs +++ b/backend-rust/src/db.rs @@ -1,17 +1,23 @@ use anyhow::Result; -use std::path::Path; -use sqlx::sqlite::{SqliteConnectOptions, SqliteConnection}; -use sqlx::Connection; use sqlx::migrate::Migrator; +use sqlx::sqlite::{SqliteConnectOptions, SqliteConnection}; +use sqlx::{Connection, SqlitePool}; use std::str::FromStr; pub const MIGRATOR: Migrator = sqlx::migrate!("./migrations"); -pub async fn initialize_db(db_path: &Path, _migrations_dir: &Path) -> Result { - let options = SqliteConnectOptions::from_str(&format!("sqlite:{}", db_path.display()))?.create_if_missing(true); +pub async fn initialize_db(db_path: &str, _migrations_dir: &str) -> Result { + let options = + SqliteConnectOptions::from_str(&format!("sqlite:{}", db_path))?.create_if_missing(true); let mut conn = SqliteConnection::connect_with(&options).await?; MIGRATOR.run(&mut conn).await?; Ok(conn) } + +pub async fn create_pool(opts: SqliteConnectOptions) -> Result { + let pool = SqlitePool::connect_with(opts).await?; + + Ok(pool) +} diff --git a/backend-rust/src/main.rs b/backend-rust/src/main.rs index d3fd81c..5eb4b24 100644 --- a/backend-rust/src/main.rs +++ b/backend-rust/src/main.rs @@ -1,21 +1,39 @@ -use std::path::Path; - +mod api; +mod config; mod db; +mod models; +mod services; + +use crate::config::Config; +use sqlx::sqlite::SqliteConnectOptions; +use std::str::FromStr; #[tokio::main] async fn main() { - let migrations_folder = String::from("./migrations"); + let config = Config::from_env(); - let db_path = Path::new("owlynews.sqlite3"); - let migrations_path = Path::new(&migrations_folder); - - match db::initialize_db(&db_path, migrations_path).await { + match db::initialize_db(&config.db_path, &config.migration_path).await { Ok(_conn) => { println!("Database initialized successfully"); - // Logic goes here + + let options = SqliteConnectOptions::from_str(&config.database_url()) + .expect("Invalid database URL") + .create_if_missing(true); + + match db::create_pool(options).await { + Ok(_pool) => { + println!("Database pool created successfully") + // App logic here + } + Err(e) => { + println!("Error creating database pool: {:?}", e); + std::process::exit(1); + } + } } Err(e) => { println!("Error initializing database: {:?}", e); + std::process::exit(1); } } }