Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaisw13 authored May 24, 2024
0 parents commit 6301c79
Show file tree
Hide file tree
Showing 5 changed files with 1,511 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:12

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .

EXPOSE 80
CMD ["node", "server.js"]
Empty file added main.go
Empty file.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "my-backend-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@google-cloud/pubsub": "^3.3.0",
"express": "^4.18.2"
}
}
43 changes: 43 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// server.js
const express = require("express");
const app = express();
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
const data = JSON.stringify({foo: 'bar'});

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function publishMessage() {
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data);

try {
const messageId = await pubSubClient
.topic(test-topic)
.publishMessage({data: dataBuffer});
console.log(`Message ${messageId} published.`);
} catch (error) {
console.error(`Received error while publishing: ${error.message}`);
process.exitCode = 1;
}
}

app.get("/user/:id", (req, res) => {
const id = req.params.id;
res.json({
id,
name: `John Doe #${id}`
});

publishMessage();
});

app.listen(8080, () => {
console.log("Server running on port 8080");
});
Loading

0 comments on commit 6301c79

Please sign in to comment.