98 lines
3.1 KiB
JavaScript
98 lines
3.1 KiB
JavaScript
const { MongoClient } = require("mongodb");
|
|
const express = require('express');
|
|
const minio = require('minio');
|
|
|
|
/*============== VARIABLE DECLARATION ==============*/
|
|
// Mongo set-up variables
|
|
const mongoCollection = process.env.MONGO_COLLECTION || 'eventlog';
|
|
const mongoUri = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/default?replicaSet=rs0';
|
|
|
|
// Minio set-up variables
|
|
const minioURI = new URL(process.env.MINIO_URI || 'http://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot');
|
|
const minioBucket = minioURI.pathname.substring(1);
|
|
console.info("Using bucket:", minioBucket);
|
|
const historyNumber = parseInt(process.env.HISTORY_AMOUNT) || 10;
|
|
|
|
// Stream set-up variables
|
|
let changeStream;
|
|
const options = { fullDocument: "updateLookup" };
|
|
const pipeline = [];
|
|
const PORT = process.env.PORT || 3002;
|
|
|
|
// Create Mongo client
|
|
const mongoClient = new MongoClient(mongoUri);
|
|
|
|
|
|
/*============== CODE ==============*/
|
|
async function run() {
|
|
|
|
console.log('server.js has been launched');
|
|
const app = express();
|
|
|
|
await mongoClient.connect();
|
|
const collection = mongoClient.db().collection(mongoCollection);
|
|
|
|
changeStream = collection.watch(pipeline, options);
|
|
console.log("Started watching changes in database");
|
|
|
|
// Triggers on GET at /event route
|
|
app.get('/events', async function (request, response) {
|
|
let minioClient = new minio.Client({
|
|
endPoint: minioURI.hostname,
|
|
port: parseInt(minioURI.port) || (minioURI.protocol == 'https:' ? 443 : 80),
|
|
useSSL: minioURI.protocol == 'https:',
|
|
accessKey: minioURI.username,
|
|
secretKey: minioURI.password
|
|
});
|
|
|
|
async function wrapEvent(doc) {
|
|
let arr = [];
|
|
let blob;
|
|
if (doc && doc.screenshot_count) {
|
|
for (let i = 1; i <= doc.screenshot_count ; i++) {
|
|
arr.push({
|
|
url: await minioClient.presignedUrl('GET', minioBucket,
|
|
`${doc.camera}/${doc._id}/${i}.jpg`, 60 * 60)
|
|
});
|
|
}
|
|
} else if (doc && doc.screenshots) {
|
|
for (let j = 0; j < doc.screenshots.length; j++) {
|
|
let path = doc.screenshots[j];
|
|
arr.push({
|
|
url: await minioClient.presignedUrl('GET', minioBucket,
|
|
`thumb/${path}`, 60 * 60),
|
|
orig: await minioClient.presignedUrl('GET', minioBucket,
|
|
`${path}`, 60 * 60)
|
|
});
|
|
}
|
|
};
|
|
|
|
if (arr.length > 0) {
|
|
blob = JSON.stringify({...doc, screenshots: [...arr]});
|
|
} else {
|
|
blob = JSON.stringify({...doc});
|
|
}
|
|
return `event: log-entry\ndata: ${blob}\n\n`
|
|
}
|
|
|
|
// Notify SSE to React
|
|
const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' };
|
|
response.writeHead(200, "OK", header);
|
|
|
|
const historyCursor = collection.find().sort({$natural:-1}).limit(historyNumber);
|
|
|
|
historyCursor.forEach(async (document) => {
|
|
response.write(await wrapEvent(document));
|
|
})
|
|
|
|
changeStream.on("change", async(data) => {
|
|
response.write(await wrapEvent(data.fullDocument));
|
|
});
|
|
});
|
|
|
|
app.listen(PORT);
|
|
console.log(`Server listening at 127.0.0.1:${PORT}`);
|
|
}
|
|
|
|
run().catch(console.dir);
|