forked from knative/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample App: Event Driven Architecture I (knative#5928)
* 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
Showing
11 changed files
with
1,122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
code-samples/eventing/bookstore-sample-app/db/bookstore-eda/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
4 changes: 4 additions & 0 deletions
4
code-samples/eventing/bookstore-sample-app/db/bookstore-eda/broker.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: eventing.knative.dev/v1 | ||
kind: Broker | ||
metadata: | ||
name: broker |
21 changes: 21 additions & 0 deletions
21
code-samples/eventing/bookstore-sample-app/db/bookstore-eda/deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
code-samples/eventing/bookstore-sample-app/db/bookstore-eda/event-display.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
53 changes: 53 additions & 0 deletions
53
code-samples/eventing/bookstore-sample-app/db/bookstore-eda/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
Oops, something went wrong.