This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
FROM node:12.17 AS builder
WORKDIR /src
COPY package.json .
RUN npm install
FROM node:12.17-alpine3.11
CMD ["node", "/app/server.js"]
ENV PORT=80
EXPOSE 80
WORKDIR /app
COPY --from=builder /src/node_modules/ /app/node_modules/
COPY *.js ./

View File

@@ -0,0 +1,24 @@
const NATS = require('nats')
const nc = NATS.connect({url: 'nats://message-queue:4222', json: true})
const { v4: uuidv4 } = require('uuid');
function post(req, res, next) {
console.log('** todo-api handler called');
var newItemEvent = {
Subject: "events.todo.newitem",
Item: {
Item: Object.keys(req.body)[0],
DateAdded: new Date().toISOString()
},
CorrelationId: uuidv4()
}
nc.publish('events.todo.newitem', newItemEvent)
console.log(`** New item published, event ID: ${newItemEvent.CorrelationId}`);
res.send(201);
next();
}
module.exports = { post }

View File

@@ -0,0 +1,12 @@
{
"name": "todo-api",
"version": "0.1.0",
"main": "server.js",
"author": "kiamol",
"dependencies": {
"nats" : "1.4.9",
"restify" : "8.5.1",
"uuid" : "8.2.0"
}
}

View File

@@ -0,0 +1,10 @@
const restify = require("restify");
const handler = require('./handler');
var server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.post("/todos", handler.post);
server.listen(process.env.PORT, function() {
console.log(`${server.name} listening at ${server.url}`);
});