Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.91.0-trixie
FROM rust:1.92.0-trixie

# Install additional tools for Rust development
RUN apt-get update && apt-get install -y \
Expand All @@ -12,6 +12,8 @@ RUN apt-get update && apt-get install -y \
wget \
pkg-config \
libssl-dev \
libpq-dev \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*

# Install rust components
Expand All @@ -20,12 +22,11 @@ RUN rustup component add rustfmt clippy rust-src llvm-tools
# Install cargo-binstall
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash

# Install cargo-make using cargo-binstall
RUN cargo binstall cargo-make cargo-edit -y
# Install cargo tools using cargo-binstall
RUN cargo binstall cargo-make cargo-edit sqlx-cli -y

# Set the working directory
WORKDIR /workspace

# Keep container running
CMD ["/bin/bash"]

13 changes: 6 additions & 7 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "Rust Development",
"build": {
"dockerfile": "Dockerfile"
},
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
"customizations": {
"vscode": {
"extensions": [
"fill-labs.dependi",
"github.vscode-github-actions",
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml",
"fill-labs.dependi",
"vadimcn.vscode-lldb"
],
"settings": {
Expand All @@ -30,7 +31,5 @@
}
},
"remoteUser": "root",
"mounts": [
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.cargo/registry,target=/usr/local/cargo/registry,type=bind,consistency=cached"
]
"postStartCommand": "cd /workspace/crates/database && sqlx migrate run"
}
36 changes: 36 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- ..:/workspace:cached
- cargo-registry:/usr/local/cargo/registry
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
depends_on:
db:
condition: service_healthy
command: sleep infinity

db:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5

volumes:
cargo-registry:
postgres-data:

2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
components: rustfmt, clippy

- name: Rust Cache
uses: Swatinem/rust-cache@2.8.1
uses: Swatinem/rust-cache@v2

- name: Cargo Fmt
run: cargo fmt --all -- --check
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ license = "MIT OR Apache-2.0"

[workspace.dependencies]
# Internal workspace dependencies
project_lib = { path = "crates/project_lib" }
database = { path = "crates/database" }

# External workspace dependencies
parking_lot = { version = "0.12.5", default-features = false }
anyhow = { version = "1.0.100" }
sqlx = { version = "0.8.6", features = ["runtime-tokio", "tls-rustls", "postgres", "migrate"] }
tokio = { version = "1.48.0", features = ["rt-multi-thread", "macros"] }

[workspace.lints.rust]
future_incompatible = { level = "warn", priority = -1 }
Expand Down
5 changes: 4 additions & 1 deletion crates/project_lib/Cargo.toml → crates/database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "project_lib"
name = "database"
version = "0.1.0"
edition.workspace = true
license.workspace = true
Expand All @@ -8,3 +8,6 @@ license.workspace = true
workspace = true

[dependencies]
sqlx.workspace = true
tokio.workspace = true

10 changes: 10 additions & 0 deletions crates/database/migrations/20241220000000_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Initial migration
-- Add your initial schema here

CREATE TABLE IF NOT EXISTS example (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

23 changes: 23 additions & 0 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;

/// Creates a connection pool to the `PostgreSQL` database.
///
/// # Errors
///
/// Returns an error if the connection to the database fails.
pub async fn create_pool(database_url: &str) -> Result<PgPool, sqlx::Error> {
PgPoolOptions::new()
.max_connections(5)
.connect(database_url)
.await
}

/// Runs all pending migrations.
///
/// # Errors
///
/// Returns an error if running migrations fails.
pub async fn run_migrations(pool: &PgPool) -> Result<(), sqlx::migrate::MigrateError> {
sqlx::migrate!("./migrations").run(pool).await
}
6 changes: 4 additions & 2 deletions crates/project_exe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ workspace = true

[dependencies]
# Internal dependencies
project_lib.workspace = true
database.workspace = true

# External dependencies
parking_lot.workspace = true
anyhow.workspace = true
sqlx.workspace = true
tokio.workspace = true
25 changes: 18 additions & 7 deletions crates/project_exe/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use parking_lot::Mutex;
use project_lib::add;
use database::{create_pool, run_migrations};
use sqlx::query;

#[derive(Default, Copy, Clone)]
struct Abc;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let database_url = std::env::var("DATABASE_URL")?;
let pool = create_pool(&database_url).await?;
run_migrations(&pool).await?;

fn main() {
Mutex::new(Abc);
println!("Hello, world! {}", add(2, 4));
query!("INSERT INTO example (name) VALUES ($1)", "Hello, world!")
.execute(&pool)
.await?;

let results = query!("SELECT name FROM example").fetch_all(&pool).await?;

for row in results {
println!("Result: {:?}", row.name);
}

Ok(())
}
13 changes: 0 additions & 13 deletions crates/project_lib/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.91.0"
channel = "1.92.0"
profile = "default"
components = ["llvm-tools"]
Loading