refactored backend initialization logic: added centralized configuration management, improved database setup with connection pooling, and modularized core components (config, models, services)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
@@ -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<SqliteConnection> {
|
||||
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<SqliteConnection> {
|
||||
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<SqlitePool> {
|
||||
let pool = SqlitePool::connect_with(opts).await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user