2022-02-09 11:56:34 +00:00
|
|
|
const { MongoClient } = require("mongodb");
|
|
|
|
const express = require('express');
|
|
|
|
|
2022-02-15 08:55:59 +00:00
|
|
|
/*============== VARIABLE DECLARATION ==============*/
|
2022-02-09 11:56:34 +00:00
|
|
|
// Mongo set-up variables
|
2022-10-05 14:38:34 +00:00
|
|
|
const mongoCollection = process.env.MONGO_COLLECTION || 'log';
|
|
|
|
const mongoUri = process.env.MONGODB_HOST || 'mongodb://127.0.0.1:27017/default?replicaSet=rs0';
|
|
|
|
|
|
|
|
const historyNumber = parseInt(process.env.HISTORY_AMOUNT) || 50;
|
2022-02-09 11:56:34 +00:00
|
|
|
|
|
|
|
// Stream set-up variables
|
|
|
|
let changeStream;
|
|
|
|
const options = { fullDocument: "updateLookup" };
|
|
|
|
const pipeline = [];
|
|
|
|
const PORT = process.env.PORT || 3002;
|
|
|
|
|
|
|
|
// Create Mongo client
|
2022-02-11 09:36:53 +00:00
|
|
|
const mongoClient = new MongoClient(mongoUri);
|
2022-02-09 11:56:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*============== CODE ==============*/
|
|
|
|
async function run() {
|
|
|
|
|
2022-02-11 09:36:53 +00:00
|
|
|
console.log('server.js has been launched');
|
2022-02-09 11:56:34 +00:00
|
|
|
const app = express();
|
|
|
|
|
|
|
|
await mongoClient.connect();
|
2022-02-11 09:36:53 +00:00
|
|
|
const collection = mongoClient.db().collection(mongoCollection);
|
2022-02-09 11:56:34 +00:00
|
|
|
|
|
|
|
changeStream = collection.watch(pipeline, options);
|
|
|
|
console.log("Started watching changes in database");
|
2022-10-05 14:38:34 +00:00
|
|
|
|
|
|
|
const writeMessage = (response, blob) => {
|
|
|
|
const message = `id: ${blob._id}\nevent: message\ndata: ${JSON.stringify(blob)}\n\n`
|
|
|
|
response.write(message)
|
|
|
|
}
|
2022-02-09 11:56:34 +00:00
|
|
|
|
2022-02-15 16:34:10 +00:00
|
|
|
// Triggers on GET at /event route
|
2022-02-09 11:56:34 +00:00
|
|
|
app.get('/events', async function (request, response) {
|
2022-02-15 19:34:59 +00:00
|
|
|
|
2022-10-05 14:38:34 +00:00
|
|
|
// Notify SSE to React
|
2022-02-11 09:36:53 +00:00
|
|
|
const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' };
|
2022-02-09 11:56:34 +00:00
|
|
|
response.writeHead(200, "OK", header);
|
|
|
|
|
2022-10-05 14:38:34 +00:00
|
|
|
const historyCursor = collection.find()
|
|
|
|
.sort({$natural:-1})
|
|
|
|
.limit(historyNumber).toArray().then((res) => {
|
|
|
|
res.reverse().forEach((document) => {
|
|
|
|
writeMessage(response, document)
|
|
|
|
})
|
|
|
|
});
|
2022-02-15 16:34:10 +00:00
|
|
|
|
2022-10-05 14:38:34 +00:00
|
|
|
const changeListener = async (change) => {
|
|
|
|
writeMessage(response, change.fullDocument)
|
|
|
|
}
|
|
|
|
changeStream.on("change", changeListener);
|
|
|
|
response.on('close', () => {
|
|
|
|
changeStream.removeListener("change", changeListener)
|
2022-02-15 16:34:10 +00:00
|
|
|
})
|
2022-02-09 11:56:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(PORT);
|
|
|
|
console.log(`Server listening at 127.0.0.1:${PORT}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
run().catch(console.dir);
|