Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample App: Event Driven Architecture I #5928

Merged
merged 10 commits into from Apr 24, 2024
Merged
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -36,3 +36,4 @@ maven-wrapper.jar

# Ignore Python virtual environments
.venv
/code-samples/eventing/bookstore-sample-app/db/bookstore-eda/node_modules/*
@@ -0,0 +1,20 @@
# Use a base image with Node.js
FROM node:18

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the application code to the container
COPY . .

# Expose the port on which the application will run
EXPOSE 8000

# Specify the command to start the application
CMD [ "node", "index.js" ]
@@ -0,0 +1,4 @@
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: broker
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: eda
labels:
app: eda
spec:
replicas: 1
selector:
matchLabels:
app: eda
template:
metadata:
labels:
app: eda
spec:
containers:
- name: eda
image: quay.io/rh-ee-leoli/eda:latest
ports:
- containerPort: 8000
@@ -0,0 +1,9 @@
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
@@ -0,0 +1,53 @@
const express = require('express');
const { HTTP, CloudEvent } = require('cloudevents');

const app = express();
const port = 8000;


// Middleware to parse JSON bodies
app.use(express.json());


app.post('/add', async (req, res) => {
try {
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
const brokerURI = process.env.K_SINK;

if (receivedEvent.type === 'new-review-comment') {
// Forward the event to the broker with the necessary CloudEvent headers
const response = await fetch(brokerURI, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'ce-specversion': '1.0',
'ce-type': 'sentiment-analysis-request',
'ce-source': 'bookstore-eda',
'ce-id': receivedEvent.id,
},
body: JSON.stringify(receivedEvent.data),
});

if (!response.ok) { // If the response status code is not 2xx, consider it a failure
console.error('Failed to forward event:', receivedEvent);
return res.status(500).json({ error: 'Failed to forward event' });
}

// If forwarding was successful, acknowledge the receipt of the event
console.log('Event forwarded successfully:', receivedEvent);
return res.status(200).json({ success: true, message: 'Event forwarded successfully' });
} else {
// Handle unexpected event types
console.warn('Unexpected event type:', receivedEvent.type);
return res.status(400).json({ error: 'Unexpected event type' });
}
} catch (error) {
console.error('Error processing request:', error);
return res.status(500).json({ error: 'Internal server error' });
}
});

// Start the server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});