Compare commits

..
2 Commits
Author SHA1 Message Date
rogier 61d2e9ea81 Fixed clippy warnings. 2026-09-21 22:10:21 +02:00
rogier 87c5e4bab1 Migrated to native Rust and containerized Postgres. 2026-09-21 22:10:11 +02:00
7 changed files with 124 additions and 59 deletions
+105
View File
@@ -0,0 +1,105 @@
version: "3"
vars:
SERVER_DIR: "{{.TASKFILE_DIR}}/server"
COMPOSE_CMD: docker compose -f "{{.TASKFILE_DIR}}/compose.yaml"
DATABASE_URL: '{{.DATABASE_URL | default "postgres://expenses:expenses@127.0.0.1:5432/expenses"}}'
tasks:
default:
cmds:
- task --list
setup:
internal: true
cmds:
- test -f "{{.SERVER_DIR}}/.env" || cp "{{.SERVER_DIR}}/.env.example" "{{.SERVER_DIR}}/.env"
db-up:
desc: Start PostgreSQL
cmds:
- "{{.COMPOSE_CMD}} up -d --wait postgres"
db-down:
desc: Stop PostgreSQL and remove containers
cmds:
- "{{.COMPOSE_CMD}} down"
db-logs:
desc: Follow PostgreSQL logs
cmds:
- "{{.COMPOSE_CMD}} logs -f postgres"
db-shell:
desc: Open a PostgreSQL shell
cmds:
- "{{.COMPOSE_CMD}} exec postgres psql -U expenses -d expenses"
db-reset:
desc: Stop PostgreSQL and delete its data volume
cmds:
- echo "Resetting PostgreSQL data volume..."
- "{{.COMPOSE_CMD}} down -v"
run:
desc: Run the API
deps: [db-up]
cmds:
- cd "{{.SERVER_DIR}}" && DATABASE_URL="{{.DATABASE_URL}}" cargo run --locked
dev:
desc: Run the API with cargo-watch
deps: [setup, db-up]
cmds:
- cd "{{.SERVER_DIR}}" && DATABASE_URL="{{.DATABASE_URL}}" cargo watch -w src -w migrations -w Cargo.toml -w Cargo.lock -x 'run --locked'
migrate:
desc: Run database migrations
deps: [db-up]
cmds:
- cd "{{.SERVER_DIR}}" && DATABASE_URL="{{.DATABASE_URL}}" SQLX_OFFLINE=false cargo sqlx migrate run
sqlx-prepare:
desc: Prepare SQLx query metadata
deps: [migrate]
cmds:
- cd "{{.SERVER_DIR}}" && DATABASE_URL="{{.DATABASE_URL}}" SQLX_OFFLINE=false cargo sqlx prepare --all
sqlx-check:
desc: Check SQLx query metadata
deps: [migrate]
cmds:
- cd "{{.SERVER_DIR}}" && DATABASE_URL="{{.DATABASE_URL}}" SQLX_OFFLINE=false cargo sqlx prepare --check --all
fmt:
desc: Format Rust code
cmds:
- cd "{{.SERVER_DIR}}" && cargo fmt --all
fmt-check:
desc: Check Rust formatting
cmds:
- cd "{{.SERVER_DIR}}" && cargo fmt --all -- --check
check:
desc: Run cargo check
cmds:
- cd "{{.SERVER_DIR}}" && SQLX_OFFLINE=true cargo check --locked
clippy:
desc: Run Clippy with warnings denied
cmds:
- cd "{{.SERVER_DIR}}" && SQLX_OFFLINE=true cargo clippy --locked --all-targets -- -D warnings
test:
desc: Run Rust tests
cmds:
- cd "{{.SERVER_DIR}}" && SQLX_OFFLINE=true cargo test --locked
ci-offline:
desc: Run offline CI checks
deps: [fmt-check, check, clippy, test]
ci:
desc: Run the full CI checks
deps: [db-up, sqlx-check, ci-offline]
+3 -30
View File
@@ -1,32 +1,12 @@
services: services:
server:
build:
context: ./server
dockerfile: Dockerfile
init: true
depends_on:
postgres:
condition: service_healthy
environment:
RUST_LOG: trace
HOST: 0.0.0.0
PORT: 3000
DATABASE_URL: postgres://expenses:expenses@postgres:5432/expenses
SQLX_OFFLINE: "true"
ports:
- "3000:3000"
volumes:
- server-target:/app/target
- ./server:/app
networks:
- expenses_net
postgres: postgres:
image: postgres:17-alpine image: postgres:17-alpine
environment: environment:
POSTGRES_DB: expenses POSTGRES_DB: expenses
POSTGRES_USER: expenses POSTGRES_USER: expenses
POSTGRES_PASSWORD: expenses POSTGRES_PASSWORD: expenses
ports:
- 5432:5432
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U expenses -d expenses"] test: ["CMD-SHELL", "pg_isready -U expenses -d expenses"]
interval: 5s interval: 5s
@@ -34,13 +14,6 @@ services:
retries: 5 retries: 5
volumes: volumes:
- postgres-data:/var/lib/postgresql/data - postgres-data:/var/lib/postgresql/data
networks:
- expenses_net
volumes: volumes:
server-target: postgres-data:
postgres-data:
networks:
expenses_net:
name: expenses_net
-5
View File
@@ -1,5 +0,0 @@
/target
.git
.env
.vscode
.DS_Store
+4
View File
@@ -0,0 +1,4 @@
RUST_LOG=info
HOST=127.0.0.1
PORT=3000
DATABASE_URL=postgres://expenses:expenses@localhost:5432/expenses
-8
View File
@@ -1,8 +0,0 @@
FROM rust:bookworm
RUN cargo install cargo-watch --locked \
&& cargo install sqlx-cli --version 0.8.6 --locked --no-default-features --features postgres,rustls
WORKDIR /app
CMD ["cargo", "watch", "--watch", "src", "--watch", "Cargo.toml", "--watch", "Cargo.lock", "--exec", "run --locked"]
+3 -3
View File
@@ -8,7 +8,7 @@ use crate::routes::dtos::expense::RecurringExpenseConversionError;
#[derive(Debug)] #[derive(Debug)]
pub enum AppError { pub enum AppError {
Database(sqlx::Error), Database(sqlx::Error),
InvalidRecurringExpense(RecurringExpenseConversionError), RecurringExpense(RecurringExpenseConversionError),
} }
impl From<sqlx::Error> for AppError { impl From<sqlx::Error> for AppError {
@@ -19,7 +19,7 @@ impl From<sqlx::Error> for AppError {
impl From<RecurringExpenseConversionError> for AppError { impl From<RecurringExpenseConversionError> for AppError {
fn from(error: RecurringExpenseConversionError) -> Self { fn from(error: RecurringExpenseConversionError) -> Self {
Self::InvalidRecurringExpense(error) Self::RecurringExpense(error)
} }
} }
@@ -27,7 +27,7 @@ impl IntoResponse for AppError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self { match self {
Self::Database(error) => tracing::error!(%error, "Database query failed"), Self::Database(error) => tracing::error!(%error, "Database query failed"),
Self::InvalidRecurringExpense(error) => { Self::RecurringExpense(error) => {
tracing::error!(%error, "Invalid recurring expense data") tracing::error!(%error, "Invalid recurring expense data")
} }
} }
+9 -13
View File
@@ -46,9 +46,9 @@ pub enum WeekendAdjustment {
#[derive(Debug)] #[derive(Debug)]
pub enum RecurringExpenseConversionError { pub enum RecurringExpenseConversionError {
InvalidRecurrenceEvery(i32), RecurrenceEvery(i32),
InvalidRecurrenceUnit(String), RecurrenceUnit(String),
InvalidWeekendAdjustment(String), WeekendAdjustment(String),
} }
impl From<RecurringExpense> for Expense { impl From<RecurringExpense> for Expense {
@@ -60,13 +60,13 @@ impl From<RecurringExpense> for Expense {
impl std::fmt::Display for RecurringExpenseConversionError { impl std::fmt::Display for RecurringExpenseConversionError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::InvalidRecurrenceEvery(value) => { Self::RecurrenceEvery(value) => {
write!(formatter, "invalid recurrence_every value: {value}") write!(formatter, "invalid recurrence_every value: {value}")
} }
Self::InvalidRecurrenceUnit(value) => { Self::RecurrenceUnit(value) => {
write!(formatter, "invalid recurrence_unit value: {value}") write!(formatter, "invalid recurrence_unit value: {value}")
} }
Self::InvalidWeekendAdjustment(value) => { Self::WeekendAdjustment(value) => {
write!(formatter, "invalid weekend_adjustment value: {value}") write!(formatter, "invalid weekend_adjustment value: {value}")
} }
} }
@@ -87,7 +87,7 @@ impl TryFrom<RecurringExpenseRow> for RecurringExpense {
end: row.ends_at, end: row.ends_at,
recurrence: Recurrence { recurrence: Recurrence {
every: row.recurrence_every.try_into().map_err(|_| { every: row.recurrence_every.try_into().map_err(|_| {
RecurringExpenseConversionError::InvalidRecurrenceEvery(row.recurrence_every) RecurringExpenseConversionError::RecurrenceEvery(row.recurrence_every)
})?, })?,
unit: RecurrenceUnit::try_from(row.recurrence_unit)?, unit: RecurrenceUnit::try_from(row.recurrence_unit)?,
weekend_adjustment: match row.weekend_adjustment { weekend_adjustment: match row.weekend_adjustment {
@@ -108,9 +108,7 @@ impl TryFrom<String> for RecurrenceUnit {
"week" => Ok(Self::Week), "week" => Ok(Self::Week),
"month" => Ok(Self::Month), "month" => Ok(Self::Month),
"year" => Ok(Self::Year), "year" => Ok(Self::Year),
_ => Err(RecurringExpenseConversionError::InvalidRecurrenceUnit( _ => Err(RecurringExpenseConversionError::RecurrenceUnit(value)),
value,
)),
} }
} }
} }
@@ -122,9 +120,7 @@ impl TryFrom<String> for WeekendAdjustment {
match value.as_str() { match value.as_str() {
"previous_business_day" => Ok(Self::PreviousBusinessDay), "previous_business_day" => Ok(Self::PreviousBusinessDay),
"next_business_day" => Ok(Self::NextBusinessDay), "next_business_day" => Ok(Self::NextBusinessDay),
_ => Err(RecurringExpenseConversionError::InvalidWeekendAdjustment( _ => Err(RecurringExpenseConversionError::WeekendAdjustment(value)),
value,
)),
} }
} }
} }