Add frontend and backend files
This commit is contained in:
5
backend/.dockerignore
Normal file
5
backend/.dockerignore
Normal file
@@ -0,0 +1,5 @@
|
||||
./node_modules
|
||||
./build
|
||||
.git
|
||||
*.md
|
||||
.gitignore
|
@@ -1 +1,16 @@
|
||||
# pull official node image
|
||||
FROM node
|
||||
|
||||
# define /app as working directory
|
||||
WORKDIR /app
|
||||
|
||||
# copy package.json and package-lock.json to /app
|
||||
COPY package.json /app
|
||||
COPY package-lock.json /app
|
||||
|
||||
# install node dependencies
|
||||
RUN npm install
|
||||
COPY . /app
|
||||
|
||||
# launch node server
|
||||
ENTRYPOINT node server.js
|
3181
backend/package-lock.json
generated
Normal file
3181
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
backend/package.json
Normal file
20
backend/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js",
|
||||
"build": "cd frontend && npm install && npm run build"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.2",
|
||||
"minio": "^7.0.26",
|
||||
"mongodb": "^4.3.1"
|
||||
}
|
||||
}
|
93
backend/server.js
Normal file
93
backend/server.js
Normal file
@@ -0,0 +1,93 @@
|
||||
const { MongoClient } = require("mongodb");
|
||||
const express = require('express');
|
||||
const minio = require('minio');
|
||||
|
||||
/*============== VARIABLE DECLARATION ==============*/
|
||||
// Mongo set-up variables
|
||||
const mongoEndpoint = process.env.ME_CONFIG_MONGODB_SERVER;
|
||||
const mongoPort = parseInt(process.env.ME_CONFIG_MONGODB_PORT);
|
||||
const mongoDatabase = process.env.ME_CONFIG_MONGODB_DB;
|
||||
const mongoCollection = process.env.ME_CONFIG_MONGODB_COLLECTION;
|
||||
|
||||
// Minio set-up variables
|
||||
const minioEndpoint = process.env.MINIO_ENDPOINT;
|
||||
const minioBucket = process.env.MINIO_BUCKET;
|
||||
const minioPort = parseInt(process.env.MINIO_SERVING_PORT);
|
||||
const minioKey = process.env.MINIO_ACCESS_KEY;
|
||||
const minioSecret = process.env.MINIO_SECRET_KEY;
|
||||
|
||||
// Stream set-up variables
|
||||
let changeStream;
|
||||
const options = { fullDocument: "updateLookup" };
|
||||
const pipeline = [];
|
||||
const PORT = process.env.PORT || 3002;
|
||||
|
||||
// Create Mongo client
|
||||
const MONGO_URI = `mongodb://${mongoEndpoint}:${mongoPort}`;
|
||||
const mongoClient = new MongoClient(MONGO_URI);
|
||||
|
||||
|
||||
/*============== CODE ==============*/
|
||||
async function run() {
|
||||
|
||||
console.log('server.js has been launched')
|
||||
|
||||
const app = express();
|
||||
|
||||
// Configuring mongoDB connection
|
||||
await mongoClient.connect();
|
||||
const database = mongoClient.db(mongoDatabase);
|
||||
const collection = database.collection(mongoCollection);
|
||||
|
||||
// Notify connection
|
||||
console.log(`Connection established with Mongo Database at ${MONGO_URI}`);
|
||||
|
||||
// Opening event listener on the database
|
||||
changeStream = collection.watch(pipeline, options);
|
||||
console.log("Started watching changes in database");
|
||||
|
||||
app.get('/events', async function (request, response) {
|
||||
let eventArray = [];
|
||||
|
||||
let minioClient = new minio.Client({
|
||||
endPoint: minioEndpoint,
|
||||
port: minioPort,
|
||||
useSSL: false,
|
||||
accessKey: minioKey,
|
||||
secretKey: minioSecret
|
||||
});
|
||||
|
||||
// notifies GET requests
|
||||
console.log('/events was requested');
|
||||
|
||||
// Setting the header to event-stream for Server Sent Events (Eventsource)
|
||||
const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' }
|
||||
response.writeHead(200, "OK", header);
|
||||
response.write('Connection established \n\n');
|
||||
|
||||
// Triggers callback on every change in collection set up
|
||||
changeStream.on("change", data => {
|
||||
|
||||
// Retrieves modified document on the db and stores it
|
||||
let document = JSON.stringify(data.fullDocument);
|
||||
eventArray = [document, ...eventArray];
|
||||
|
||||
// Fetch screenshot if there is one
|
||||
if (data.fullDocument.screenshot_count) {
|
||||
minioClient.presignedUrl('GET', minioBucket, `${data.fullDocument.camera}/${data.fullDocument._id}/1.jpg`, 60 * 60, (err, presignedUrl) => {
|
||||
if (err) { return console.log(err) };
|
||||
const realURL = JSON.stringify({ picUrl: presignedUrl });
|
||||
eventArray = [realURL, ...eventArray];
|
||||
})
|
||||
}
|
||||
|
||||
// sends updated array
|
||||
response.write(`data: [${[...eventArray]}]\n\n`);
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(PORT);
|
||||
console.log(`Server listening at 127.0.0.1:${PORT}`);
|
||||
}
|
||||
|
||||
run().catch(console.dir);
|
Reference in New Issue
Block a user