migration and db pool
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled

This commit is contained in:
Dmitri 2026-04-24 21:05:40 +02:00
parent e93a65ae9c
commit 993395c208
Signed by: kanopo
GPG Key ID: 759ADD40E3132AC7
5 changed files with 1515 additions and 7 deletions

4
.gitignore vendored
View File

@ -1,4 +1,4 @@
.env .env
/target target
/logs logs
postgres-data postgres-data

1493
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -9,3 +9,5 @@ tracing = "0.1.44"
tracing-appender = "0.2.5" tracing-appender = "0.2.5"
tracing-subscriber = {version="0.3.23", features = ["env-filter", "json"]} tracing-subscriber = {version="0.3.23", features = ["env-filter", "json"]}
tracing-tree = "0.4.1" tracing-tree = "0.4.1"
tokio = { version = "1.52.1", features = ["rt-multi-thread", "macros"] }
sqlx = { version = "0.8", features = [ "runtime-tokio", "postgres", "time", "uuid" ] }

View File

@ -23,6 +23,7 @@ impl AppEnv {
pub struct Config { pub struct Config {
pub db_url: String, pub db_url: String,
pub app_env: AppEnv, pub app_env: AppEnv,
pub http_port: String,
} }
impl Config { impl Config {
@ -30,6 +31,7 @@ impl Config {
dotenv().ok(); dotenv().ok();
Self { Self {
db_url: env::var("DB_URL").expect("DB_URL is not configured"), db_url: env::var("DB_URL").expect("DB_URL is not configured"),
http_port: env::var("PORT").expect("PORT is not configured"),
app_env: AppEnv::from_env(), app_env: AppEnv::from_env(),
} }
} }

View File

@ -1,10 +1,23 @@
use std::process::exit;
use sqlx::postgres::PgPoolOptions;
mod config; mod config;
mod logging; mod logging;
fn main() { #[tokio::main]
async fn main() {
let cfg = config::Config::load(); let cfg = config::Config::load();
let _logging_guard = logging::LoggerConfig::init(cfg.app_env); let _logging_guard = logging::LoggerConfig::init(cfg.app_env);
let db = match PgPoolOptions::new().connect(&cfg.db_url).await {
tracing::info!("ciao"); Ok(p) => p,
tracing::debug!("{:?}", cfg); Err(_) => {
tracing::error!("Failed to connect to the database");
exit(1);
}
};
match sqlx::migrate!().run(&db).await {
Ok(_) => tracing::info!("Migration completed succesfully"),
Err(_) => tracing::error!("Failed to apply migrations"),
}
} }