feat: add sqlx basic config

This commit is contained in:
Alexander Navarro 2025-02-21 12:55:23 -03:00
parent e0e8700baa
commit 48afdaccea
6 changed files with 1598 additions and 17 deletions

View file

@ -15,6 +15,7 @@ start-dev-services: (docker-compose "up --remove-orphans")
dev: dev:
watchexec --restart --clear --watch src --watch templates cargo run watchexec --restart --clear --watch src --watch templates cargo run
watchexec --restart --clear --watch src --watch dist cargo run
migrate: (docker-compose "run dbmate migrate") migrate: (docker-compose "run dbmate migrate")

1566
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,18 @@ edition = "2021"
[dependencies] [dependencies]
axum = "0.8.1" axum = "0.8.1"
axum-sqlx-tx = "0.10.0"
minijinja = { version = "2.7.0", features = ["loader"] } minijinja = { version = "2.7.0", features = ["loader"] }
minijinja-embed = "2.7.0" minijinja-embed = "2.7.0"
notify = "8.0.0" notify = "8.0.0"
serde = { version = "1.0.217", features = ["derive"] } serde = { version = "1.0.217", features = ["derive"] }
sqlx = { version = "0.8.3", features = [
"chrono",
"derive",
"json",
"postgres",
"runtime-tokio",
] }
thiserror = "2.0.11" thiserror = "2.0.11"
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.6.2", features = ["fs", "trace"] } tower-http = { version = "0.6.2", features = ["fs", "trace"] }

View file

@ -19,13 +19,17 @@ pub enum Error {
#[error(transparent)] #[error(transparent)]
IO(#[from] std::io::Error), IO(#[from] std::io::Error),
#[error(transparent)]
Env(#[from] std::env::VarError),
#[error(transparent)] #[error(transparent)]
Template(#[from] minijinja::Error), Template(#[from] minijinja::Error),
#[error("Error in runtime execution: {0}")] #[error("Error in runtime execution: {0}")]
Runtime(&'static str), Runtime(&'static str),
// #[error(transparent)]
// DatabaseOperation(#[from] sqlx::Error), #[error(transparent)]
DatabaseOperation(#[from] sqlx::Error),
} }
impl IntoResponse for Error { impl IntoResponse for Error {

View file

@ -6,6 +6,8 @@ use minijinja::{Environment, Template};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
pub type Tx = axum_sqlx_tx::Tx<sqlx::Postgres>;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Link { pub struct Link {
pub path: String, pub path: String,

View file

@ -1,8 +1,11 @@
#![allow(unused)] #![allow(unused)]
#![allow(dead_code)] #![allow(dead_code)]
use compendium::{router, AppState, Error, Link, Result}; use compendium::{router, AppState, Error, Link, Result, Tx};
use minijinja::{Environment, Value}; use minijinja::{Environment, Value};
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use std::env;
use std::sync::Arc; use std::sync::Arc;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::info; use tracing::info;
@ -37,6 +40,23 @@ fn load_templates() -> Result<Environment<'static>> {
Ok(tmpl_env) Ok(tmpl_env)
} }
async fn init_db() -> Result<PgPool> {
let db_string = format!(
"postgres://{}:{}@{}/{}",
env::var("CPD_DB_USER")?,
env::var("CPD_DB_PASSWORD")?,
env::var("CPD_DB_HOST")?,
env::var("CPD_DB_NAME")?
);
info!("Connecting to database {}", db_string);
Ok(PgPoolOptions::new()
.max_connections(5)
.connect(&db_string)
.await?)
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
// Logs // Logs
@ -51,9 +71,15 @@ async fn main() -> Result<()> {
let mut tmpl_env = load_templates()?; let mut tmpl_env = load_templates()?;
let pool = init_db().await?;
let (tx_state, tx_layer) = Tx::setup(pool);
let app = router::new() let app = router::new()
.layer(TraceLayer::new_for_http().on_request(())) .layer(TraceLayer::new_for_http().on_request(()))
.with_state(AppState::new(tmpl_env)); .with_state(AppState::new(tmpl_env))
.layer(tx_layer)
.with_state(tx_state);
// Add hot reload only on dev mode // Add hot reload only on dev mode
#[cfg(debug_assertions)] #[cfg(debug_assertions)]