From 07abda811e5b8d1ecae719ae7eb2fd5bfd35afd3 Mon Sep 17 00:00:00 2001 From: Fotis Paschos Date: Fri, 28 Jul 2023 17:16:36 +0300 Subject: [PATCH] Initial commit Trying sqlx postgres with json --- .gitignore | 20 +++++++ Cargo.toml | 3 + USAGE.md | 37 ++++++++++++ app-claims-service/Cargo.toml | 13 +++++ app-claims-service/README.md | 0 app-claims-service/migrations/1_claim.sql | 5 ++ app-claims-service/src/main.rs | 68 +++++++++++++++++++++++ docker-compose.yml | 43 ++++++++++++++ 8 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 USAGE.md create mode 100644 app-claims-service/Cargo.toml create mode 100644 app-claims-service/README.md create mode 100644 app-claims-service/migrations/1_claim.sql create mode 100644 app-claims-service/src/main.rs create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bb2aee --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +*.iml +.idea/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +### Rust +target/ +Cargo.lock + + + + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7f07af6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] + +members = ["app-claims-service"] \ No newline at end of file diff --git a/USAGE.md b/USAGE.md new file mode 100644 index 0000000..796a130 --- /dev/null +++ b/USAGE.md @@ -0,0 +1,37 @@ +#### Instructions/ Usage: +Export debezium version (execute once per open terminal session) +```bash +export DEBEZIUM_VERSION=2.3 +``` + +Start up +```bash +docker compose up -d +``` + +Tear down +```bash +docker compose down +``` + +#### Utility actions: +Check everything up and running +```bash +docker ps +``` + +List all available kafka topics +```bash +docker-compose exec kafka /kafka/bin/kafka-topics.sh --bootstrap-server kafka:9092 --list +``` + +#### Documentation/Resources +For more information see: +- [debezium/kafka](https://hub.docker.com/r/debezium/kafka) + +Reference projects: +- [cschaible/rust-microservices-kafka](https://github.com/cschaible/rust-microservices-kafka/blob/master/README.md) +- [debezium-examples/distributed-caching](https://github.com/debezium/debezium-examples/blob/main/distributed-caching/README.md) + +Rust libraries: +- [sqlx](https://github.com/launchbadge/sqlx/blob/main/README.md) \ No newline at end of file diff --git a/app-claims-service/Cargo.toml b/app-claims-service/Cargo.toml new file mode 100644 index 0000000..e1ee86b --- /dev/null +++ b/app-claims-service/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "app-claims-service" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.72" +serde = { version = "1.0.177", features = ["derive"] } +serde_json = "1.0.104" +sqlx = { version = "0.7.1", features = ["postgres", "json", "runtime-tokio"] } +tokio = { version = "1.29.1", features = ["rt-multi-thread", "macros"] } diff --git a/app-claims-service/README.md b/app-claims-service/README.md new file mode 100644 index 0000000..e69de29 diff --git a/app-claims-service/migrations/1_claim.sql b/app-claims-service/migrations/1_claim.sql new file mode 100644 index 0000000..5618f34 --- /dev/null +++ b/app-claims-service/migrations/1_claim.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS claim +( + id BIGSERIAL PRIMARY KEY, + involved JSONB NOT NULL +); \ No newline at end of file diff --git a/app-claims-service/src/main.rs b/app-claims-service/src/main.rs new file mode 100644 index 0000000..f06d128 --- /dev/null +++ b/app-claims-service/src/main.rs @@ -0,0 +1,68 @@ +use sqlx::postgres::PgPoolOptions; +use anyhow::Context; +use serde::{Deserialize, Serialize}; +use sqlx::PgPool; +use sqlx::types::Json; + +#[derive(sqlx::FromRow)] +struct ClaimDb { + id: i64, + involved: Json, +} + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct Party { + first_name: String, + last_name: String, +} + +async fn insert_claim(db: &PgPool, involved: Party) -> anyhow::Result { + let c: (i64,) = sqlx::query_as(r#"INSERT INTO claim (involved) VALUES ($1) RETURNING id"#) + .bind(Json(involved)) + .fetch_one(db) + .await?; + + Ok(c.0) +} + +async fn list_claims(db: &PgPool) -> anyhow::Result<()> { + Ok(()) +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + + // TODO 1) config and parameters + let db = PgPoolOptions::new() + .max_connections(5) + .connect("postgres://postgres:postgres@localhost:5432/claimsdb") + .await + .context("Unable to connect to database")?; + + sqlx::migrate!() + .run(&db) + .await + .context("Unable to exec db migrations")?; + + // Just check the database connection + // Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL) + let row: (i64, ) = sqlx::query_as("SELECT $1") + .bind(150_i64) + .fetch_one(&db) + .await?; + + assert_eq!(row.0, 150); + + let involved = Party { + first_name: "Foo".into(), + last_name: "Bar".into(), + }; + + + let new_id = insert_claim(&db, involved).await?; + println!("Claim with id = {new_id} inserted"); + list_claims(&db).await?; + + Ok(()) +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..60e65b5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,43 @@ +# example taken from: +# https://github.com/debezium/debezium-examples/blob/main/auditlog/docker-compose.yaml + +version: '3.8' +services: + zookeeper: + container_name: kd-zookeeper + image: quay.io/debezium/zookeeper:${DEBEZIUM_VERSION} + ports: + - "2181:2181" + - "2888:2888" + - "3888:3888" + networks: + - kd-demo + kafka: + container_name: kd-kafka + image: quay.io/debezium/kafka:${DEBEZIUM_VERSION} + ports: + - "9092:9092" + depends_on: + - zookeeper + environment: + - ZOOKEEPER_CONNECT=zookeeper:2181 + # What is this for???? + # For local development of auditlog-enricher +# - ADVERTISED_HOST_NAME=192.168.1.6 + - KAFKA_GROUP_MIN_SESSION_TIMEOUT_MS=250 + networks: + - kd-demo + postgres: + image: quay.io/debezium/example-postgres:${DEBEZIUM_VERSION} + container_name: kd-postgres + ports: + - "5432:5432" + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=claimsdb + networks: + - kd-demo +networks: + kd-demo: + name: kd-demo \ No newline at end of file