migration and db pool
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
This commit is contained in:
parent
e93a65ae9c
commit
993395c208
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,4 +1,4 @@
|
||||
.env
|
||||
/target
|
||||
/logs
|
||||
target
|
||||
logs
|
||||
postgres-data
|
||||
|
||||
1493
Cargo.lock
generated
1493
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -9,3 +9,5 @@ tracing = "0.1.44"
|
||||
tracing-appender = "0.2.5"
|
||||
tracing-subscriber = {version="0.3.23", features = ["env-filter", "json"]}
|
||||
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" ] }
|
||||
|
||||
@ -23,6 +23,7 @@ impl AppEnv {
|
||||
pub struct Config {
|
||||
pub db_url: String,
|
||||
pub app_env: AppEnv,
|
||||
pub http_port: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@ -30,6 +31,7 @@ impl Config {
|
||||
dotenv().ok();
|
||||
Self {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@ -1,10 +1,23 @@
|
||||
use std::process::exit;
|
||||
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
|
||||
mod config;
|
||||
mod logging;
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cfg = config::Config::load();
|
||||
let _logging_guard = logging::LoggerConfig::init(cfg.app_env);
|
||||
|
||||
tracing::info!("ciao");
|
||||
tracing::debug!("{:?}", cfg);
|
||||
let db = match PgPoolOptions::new().connect(&cfg.db_url).await {
|
||||
Ok(p) => p,
|
||||
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"),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user