Skip to content

Commit

Permalink
Sample App: Event Driven Architecture I (knative#5928)
Browse files Browse the repository at this point in the history
* Add the node.js server

* Setting up the infrastructure

* Update the port of the node application

* Add the sinkBinding

* Adding the reply feedback loop

* Adding the response into the nodejs server

* Change the naming convention

* Update the index.js to remove the uncessary comments
  • Loading branch information
Leo6Leo authored and prushh committed Apr 30, 2024
1 parent 6766f84 commit 5e763f0
Show file tree
Hide file tree
Showing 11 changed files with 1,122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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/*
Original file line number Diff line number Diff line change
@@ -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" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: broker
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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}`);
});
Loading

0 comments on commit 5e763f0

Please sign in to comment.