2022-02-09 11:56:34 +00:00
|
|
|
const { MongoClient } = require("mongodb");
|
|
|
|
const express = require('express');
|
|
|
|
const minio = require('minio');
|
|
|
|
|
2022-02-15 08:55:59 +00:00
|
|
|
/*============== VARIABLE DECLARATION ==============*/
|
2022-02-09 11:56:34 +00:00
|
|
|
// Mongo set-up variables
|
2022-02-11 09:36:53 +00:00
|
|
|
const mongoCollection = process.env.MONGO_COLLECTION || 'eventlog';
|
|
|
|
const mongoUri = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/default?replicaSet=rs0';
|
2022-02-09 11:56:34 +00:00
|
|
|
|
|
|
|
// Minio set-up variables
|
2022-08-25 05:31:08 +00:00
|
|
|
const minioAccessKey = process.env.MINIO_ACCESS_KEY || 'kspace-mugshot';
|
|
|
|
const minioSecretKey = process.env.MINIO_SECRET_KEY || '2mSI6HdbJ8';
|
|
|
|
const minioHostname = process.env.MINIO_HOSTNAME || '127.0.0.1';
|
|
|
|
const minioPort = process.env.MINIO_PORT || 9000;
|
|
|
|
const minioScheme = process.env.MINIO_SCHEME || 'http';
|
|
|
|
const minioBucket = process.env.MINIO_BUCKET || 'kspace-mugshot';
|
2022-02-15 21:16:38 +00:00
|
|
|
console.info("Using bucket:", minioBucket);
|
2022-03-06 06:18:01 +00:00
|
|
|
const historyNumber = parseInt(process.env.HISTORY_AMOUNT) || 10;
|
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-08-25 05:31:08 +00:00
|
|
|
console.log("Minio hostname:", minioHostname);
|
|
|
|
console.log("Minio port:", minioPort);
|
|
|
|
console.log("Minio protocol:", minioScheme);
|
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) {
|
|
|
|
let minioClient = new minio.Client({
|
2022-08-25 05:31:08 +00:00
|
|
|
endPoint: minioHostname,
|
|
|
|
port: parseInt(minioPort),
|
|
|
|
useSSL: minioScheme == 'https',
|
|
|
|
accessKey: minioAccessKey,
|
|
|
|
secretKey: minioSecretKey
|
2022-02-09 11:56:34 +00:00
|
|
|
});
|
|
|
|
|
2022-02-15 21:16:38 +00:00
|
|
|
async function wrapEvent(doc) {
|
2022-03-06 06:18:01 +00:00
|
|
|
let arr = [];
|
2022-02-17 08:25:36 +00:00
|
|
|
let blob;
|
2022-02-15 20:14:27 +00:00
|
|
|
if (doc && doc.screenshot_count) {
|
2022-02-15 19:34:59 +00:00
|
|
|
for (let i = 1; i <= doc.screenshot_count ; i++) {
|
2022-03-06 06:18:01 +00:00
|
|
|
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)
|
|
|
|
});
|
2022-02-15 19:34:59 +00:00
|
|
|
}
|
|
|
|
};
|
2022-03-06 06:18:01 +00:00
|
|
|
|
|
|
|
if (arr.length > 0) {
|
|
|
|
blob = JSON.stringify({...doc, screenshots: [...arr]});
|
|
|
|
} else {
|
|
|
|
blob = JSON.stringify({...doc});
|
|
|
|
}
|
2022-02-15 19:34:59 +00:00
|
|
|
return `event: log-entry\ndata: ${blob}\n\n`
|
|
|
|
}
|
|
|
|
|
2022-02-15 16:34:10 +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-02-15 21:16:38 +00:00
|
|
|
const historyCursor = collection.find().sort({$natural:-1}).limit(historyNumber);
|
2022-02-15 16:34:10 +00:00
|
|
|
|
2022-02-15 21:16:38 +00:00
|
|
|
historyCursor.forEach(async (document) => {
|
|
|
|
response.write(await wrapEvent(document));
|
2022-02-15 16:34:10 +00:00
|
|
|
})
|
|
|
|
|
2022-02-15 21:16:38 +00:00
|
|
|
changeStream.on("change", async(data) => {
|
|
|
|
response.write(await wrapEvent(data.fullDocument));
|
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);
|