Proyek: Aplikasi Rust dan PostgreSQL
REST API katalog item yang ditulis dengan Rust (axum 0.8) dan PostgreSQL
lewat driver tokio-postgres dengan pool koneksi deadpool-postgres.
Aplikasi menjalankan migrasi skema secara otomatis saat startup, menerima
SIGTERM dengan graceful shutdown, dan diuji lewat integration test black-box
yang berjalan sebagai service Compose terpisah.
Struktur Berkas #
| Berkas | Peran |
|---|---|
src/main.rs | Konfigurasi environment, routing, migrasi, graceful shutdown |
Cargo.toml / Cargo.lock | Definisi dan checksum dependensi |
migrations/001_create_items.sql | Skema awal tabel items |
tests/integration_test.rs | Test black-box HTTP dengan reqwest |
Dockerfile | Build multi-stage (builder Rust → runtime Alpine) |
compose.yaml | Service app, db, dan test (profile) |
.dockerignore | Membatasi build context |
Endpoint #
| Metode | Path | Keterangan |
|---|---|---|
| GET | /healthz | Cek kesehatan aplikasi dan database |
| GET | /api/items | Daftar seluruh item |
| POST | /api/items | Buat item baru (body: name, note) |
| GET | /api/items/{id} | Ambil satu item (404 jika tidak ada) |
Menjalankan #
Password database diambil dari environment; paling mudah lewat berkas .env:
echo 'POSTGRES_PASSWORD=rahasia123' > .env
docker compose up -d --buildMenguji #
Aplikasi dipublikasikan di port 8081 host agar tidak bentrok dengan proyek Go yang memakai 8080:
curl -s http://localhost:8081/healthz
curl -s -X POST http://localhost:8081/api/items \
-H 'Content-Type: application/json' \
-d '{"name":"Kopi Torabika","note":"favorit pelanggan"}'
curl -s http://localhost:8081/api/items
curl -s http://localhost:8081/api/items/1Hasil yang diharapkan: healthz mengembalikan {"status":"ok"}, POST
mengembalikan 201 dengan item ber-id 1, daftar memuat item tersebut, dan
GET /api/items/999 mengembalikan 404.
Integration Test #
Test menembak HTTP aplikasi dari container rust terpisah dengan profile
test (aplikasi dan database tetap berjalan):
docker compose --profile test run --rm testMembersihkan #
docker compose down # volume db-data tetap ada
docker compose down -v # sekaligus menghapus data