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
|
.env
|
||||||
/target
|
target
|
||||||
/logs
|
logs
|
||||||
postgres-data
|
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-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" ] }
|
||||||
|
|||||||
@ -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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/main.rs
21
src/main.rs
@ -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"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user