Basic sqlx setup migrations and init
This commit is contained in:
@@ -14,6 +14,7 @@ rand_core = { version = "0.6", features = ["getrandom"] }
|
|||||||
regex = "1.12.2"
|
regex = "1.12.2"
|
||||||
serde = { version = "1.0.228", features = ["derive", "serde_derive"] }
|
serde = { version = "1.0.228", features = ["derive", "serde_derive"] }
|
||||||
serde_json = "1.0.145"
|
serde_json = "1.0.145"
|
||||||
|
sqlx = { version = "0.8.6", features = ["runtime-tokio-rustls", "postgres", "uuid", "chrono", "migrate"] }
|
||||||
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread"] }
|
||||||
tower = { version = "0.5.2", features = ["tokio", "tracing"] }
|
tower = { version = "0.5.2", features = ["tokio", "tracing"] }
|
||||||
tower-http = { version = "0.6.7", features = ["cors"] }
|
tower-http = { version = "0.6.7", features = ["cors"] }
|
||||||
|
|||||||
7
src/backend/migrations/0001_init.sql
Normal file
7
src/backend/migrations/0001_init.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE system_state (
|
||||||
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||||
|
initialized BOOLEAN NOT NULL DEFAULT false
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO system_state (id, initialized) VALUES (1, true)
|
||||||
|
ON CONFLICT (id) DO NOTHING;
|
||||||
@@ -1,5 +1,23 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{domain::user::NewUser, state::AppState};
|
use axum::http::StatusCode;
|
||||||
|
use validator::Validate;
|
||||||
|
|
||||||
pub async fn create(state: Arc<AppState>, new_user: NewUser) -> Result<User> {}
|
use crate::{
|
||||||
|
domain::user::{InternalNewUser, NewUser, User},
|
||||||
|
state::AppState,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn create(state: Arc<AppState>, new_user: NewUser) -> Result<User, StatusCode> {
|
||||||
|
if let Err(_) = new_user.validate() {
|
||||||
|
return Err(StatusCode::BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
let internal = InternalNewUser::try_from(new_user).map_err(|e| {
|
||||||
|
error!("Conversion to InternalUser failed: {e}");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
|
||||||
|
todo!("Hook up return once db setup ready");
|
||||||
|
}
|
||||||
|
|||||||
20
src/backend/src/infra/db/mod.rs
Normal file
20
src/backend/src/infra/db/mod.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
pub async fn connect(database_url: &str) -> Result<PgPool> {
|
||||||
|
let pool = PgPoolOptions::new()
|
||||||
|
.max_connections(10)
|
||||||
|
.acquire_timeout(Duration::from_secs(5))
|
||||||
|
.connect(database_url)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(pool)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn migrate(pool: &PgPool) -> Result<()> {
|
||||||
|
sqlx::migrate!().run(&pool).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
1
src/backend/src/infra/mod.rs
Normal file
1
src/backend/src/infra/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod db;
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod domain;
|
pub mod domain;
|
||||||
|
pub mod infra;
|
||||||
|
pub mod prelude;
|
||||||
pub mod router;
|
pub mod router;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|||||||
1
src/backend/src/prelude.rs
Normal file
1
src/backend/src/prelude.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub use tracing::{debug, error, info, warn};
|
||||||
@@ -23,7 +23,5 @@ pub async fn create(
|
|||||||
StatusCode::INTERNAL_SERVER_ERROR
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let user = User::from(internal);
|
todo!("Hook up return once db setup ready");
|
||||||
|
|
||||||
Ok(Json(user))
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user