Skip to content

Commit

Permalink
Merge pull request #8 from redbadger/rust-wasmcloud
Browse files Browse the repository at this point in the history
Wasm Components in wasmCloud
  • Loading branch information
StuartHarris authored Jan 20, 2024
2 parents 33d09ae + 8697d27 commit 31e7016
Show file tree
Hide file tree
Showing 60 changed files with 4,564 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/java-containers-k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ on:
- "java-containers-k8s/**"

pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- "main"
paths:
- "java-containers-k8s/**"

jobs:
build-and-push:
if: github.event.pull_request.draft == false
name: Build, push and deploy 🏗️🚀
runs-on: ubuntu-latest
defaults:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/rust-containers-k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ on:
- "rust-containers-k8s/**"

pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- "main"
paths:
- "rust-containers-k8s/**"

jobs:
build-and-push:
if: github.event.pull_request.draft == false
name: Build, push and deploy 🏗️🚀
runs-on: ubuntu-latest
defaults:
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
.env
build/
target/
keys/
**/wit/deps/*
sentinel
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"rust-containers-k8s/inventory-service/Cargo.toml",
"rust-containers-k8s/notification-service/Cargo.toml",
"rust-containers-k8s/order-service/Cargo.toml",
"rust-containers-k8s/product-service/Cargo.toml"
"rust-containers-k8s/product-service/Cargo.toml",
"rust-wasmcloud/products/Cargo.toml",
"rust-wasmcloud/inventory/Cargo.toml",
"rust-wasmcloud/products-http/Cargo.toml",
"rust-wasmcloud/platform-wasmcloud/Cargo.toml"
],
"sqltools.connections": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String placeOrder(OrderRequest orderRequest) {

// Call Inventory Service, and place order if product is in
// stock
InventoryResponse[] inventoryResponsArray = webClientBuilder
InventoryResponse[] inventoryResponseArray = webClientBuilder
.build()
.get()
.uri("http://inventory-service/api/inventory", uriBuilder -> uriBuilder.queryParam("skuCode", skuCodes)
Expand All @@ -61,8 +61,8 @@ public String placeOrder(OrderRequest orderRequest) {
.bodyToMono(InventoryResponse[].class)
.block();

boolean allProductsInStock = Arrays.stream(inventoryResponsArray)
.allMatch(InventoryResponse::isInStock) && inventoryResponsArray.length > 0;
boolean allProductsInStock = Arrays.stream(inventoryResponseArray)
.allMatch(InventoryResponse::isInStock) && inventoryResponseArray.length > 0;

if (allProductsInStock) {
log.info("All requested products are in stock");
Expand Down
18 changes: 9 additions & 9 deletions rust-containers-k8s/notification-service/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions rust-containers-k8s/notification-service/src/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]

pub struct OrderPlacedEvent {
pub order_number: Uuid,
}

pub trait Logger {
fn info(&self, msg: &str);
}

pub struct Service<Logger> {
pub logger: Logger,
}

impl<L> Service<L>
where
L: Logger,
{
pub fn new(logger: L) -> Self {
Self { logger }
}

pub fn recv(&self, event: OrderPlacedEvent) {
self.logger.info(&format!(
"Received Notification for Order - {}",
event.order_number
));
}
}
2 changes: 1 addition & 1 deletion rust-containers-k8s/notification-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod config;
pub mod types;
pub mod core;
17 changes: 15 additions & 2 deletions rust-containers-k8s/notification-service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use backoff::{retry, ExponentialBackoff};
use dotenv::dotenv;
use notification_service::{config::Config, types::OrderPlacedEvent};
use notification_service::{
config::Config,
core::{Logger, OrderPlacedEvent, Service},
};
use rdkafka::{
admin::{AdminClient, AdminOptions, NewTopic, TopicReplication},
client::DefaultClientContext,
Expand All @@ -13,6 +16,14 @@ use std::time::Duration;
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

struct TracingLogger;

impl Logger for TracingLogger {
fn info(&self, msg: &str) {
info!("{}", msg);
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::registry()
Expand Down Expand Up @@ -53,6 +64,8 @@ async fn main() -> anyhow::Result<()> {
let consumer = create_consumer(&kafka)?;
consumer.subscribe(&[&config.kafka_topic])?;

let service = Service::new(TracingLogger);

loop {
let message = consumer.recv().await?;
let payload = message.payload();
Expand All @@ -61,7 +74,7 @@ async fn main() -> anyhow::Result<()> {
};

if let Ok(order) = serde_json::from_slice::<OrderPlacedEvent>(message) {
info!("Received Notification for Order - {}", order.order_number);
service.recv(order);
} else {
info!(
"Error decoding notification order: {:?}",
Expand Down
9 changes: 0 additions & 9 deletions rust-containers-k8s/notification-service/src/types.rs

This file was deleted.

Loading

0 comments on commit 31e7016

Please sign in to comment.