Simple AppCfg setup to hold PgPool in AppState
This commit is contained in:
4
src/backend/src/config.rs
Normal file
4
src/backend/src/config.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub struct AppCfg {
|
||||||
|
pub db_path: String,
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
pub mod user;
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use sqlx::{PgPool, postgres::PgPoolOptions};
|
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||||
@@ -15,6 +17,6 @@ pub async fn connect(database_url: &str) -> Result<PgPool> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn migrate(pool: &PgPool) -> Result<()> {
|
pub async fn migrate(pool: &PgPool) -> Result<()> {
|
||||||
sqlx::migrate!().run(&pool).await?;
|
sqlx::migrate!().run(pool).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
0
src/backend/src/infra/db/user.rs
Normal file
0
src/backend/src/infra/db/user.rs
Normal file
@@ -1,4 +1,5 @@
|
|||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod config;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod domain;
|
pub mod domain;
|
||||||
pub mod infra;
|
pub mod infra;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::{Ok, Result};
|
use anyhow::{Ok, Result};
|
||||||
use rustymine_daemon::{router, state::AppState};
|
use rustymine_daemon::{config::AppCfg, router, state::AppState};
|
||||||
use tracing::{Level, debug, error, info, warn};
|
use tracing::{Level, debug, error, info, warn};
|
||||||
|
|
||||||
pub const APP_NAME: &str = env!("CARGO_PKG_NAME");
|
pub const APP_NAME: &str = env!("CARGO_PKG_NAME");
|
||||||
@@ -38,7 +38,12 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
tracing::subscriber::set_global_default(subscriber)?;
|
tracing::subscriber::set_global_default(subscriber)?;
|
||||||
|
|
||||||
let state = Arc::new(AppState::new());
|
let db_path: String = "postgres://rustymine:minecraft@localhost:5432/rustymine_dev".to_string();
|
||||||
|
let config = AppCfg {
|
||||||
|
db_path: db_path.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let state = Arc::new(AppState::new(&config).await);
|
||||||
|
|
||||||
let app_result = router::init_router(state.clone()).await;
|
let app_result = router::init_router(state.clone()).await;
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,34 @@
|
|||||||
use std::collections::HashMap;
|
use std::process::exit;
|
||||||
|
|
||||||
use crate::domain::user::InternalNewUser;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
use crate::{config::AppCfg, domain::user::InternalNewUser, infra::db};
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub users: HashMap<String, InternalNewUser>,
|
pub db_pool: PgPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new() -> Self {
|
pub async fn new(config: &AppCfg) -> Self {
|
||||||
Self {
|
debug!("Initiating new AppState");
|
||||||
users: HashMap::new(),
|
let db_pool = db::connect(&config.db_path)
|
||||||
}
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
error!("Failed to connect to database: {e}");
|
||||||
|
exit(20);
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
db::migrate(&db_pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
error!("Failed to migrade database: {e}");
|
||||||
|
exit(22);
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
info!("DB connect and migrate sucessful");
|
||||||
|
Self { db_pool: db_pool }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user