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:
@@ -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