From 08a261e491750d9f1eed1d55103b10ad066aa547 Mon Sep 17 00:00:00 2001 From: rakbaal Date: Fri, 11 Feb 2022 11:36:53 +0200 Subject: [PATCH] Reduce environment variables number + optimise code --- backend/server.js | 39 +++++++++++------------------- frontend/default.conf | 2 ++ frontend/src/components/app/App.js | 2 +- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/backend/server.js b/backend/server.js index 75ae373..2813d86 100644 --- a/backend/server.js +++ b/backend/server.js @@ -4,17 +4,12 @@ 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; +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 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; +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); // Stream set-up variables let changeStream; @@ -23,24 +18,18 @@ const pipeline = []; const PORT = process.env.PORT || 3002; // Create Mongo client -const MONGO_URI = `mongodb://${mongoEndpoint}:${mongoPort}`; -const mongoClient = new MongoClient(MONGO_URI); +const mongoClient = new MongoClient(mongoUri); /*============== CODE ==============*/ async function run() { - console.log('server.js has been launched') - + 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}`); + const collection = mongoClient.db().collection(mongoCollection); // Opening event listener on the database changeStream = collection.watch(pipeline, options); @@ -50,18 +39,18 @@ async function run() { let eventArray = []; let minioClient = new minio.Client({ - endPoint: minioEndpoint, - port: minioPort, - useSSL: false, - accessKey: minioKey, - secretKey: minioSecret + endPoint: minioURI.hostname, + port: parseInt(minioURI.port) || (minioURI.protocol == 'https' ? 443 : 80), + useSSL: minioURI.protocol == 'https', + accessKey: minioURI.username, + secretKey: minioURI.password }); // 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' } + const header = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive' }; response.writeHead(200, "OK", header); response.write('Connection established \n\n'); @@ -79,7 +68,7 @@ async function run() { const realURL = JSON.stringify({ picUrl: presignedUrl }); eventArray = [realURL, ...eventArray]; }) - } + }; // sends updated array response.write(`data: [${[...eventArray]}]\n\n`); diff --git a/frontend/default.conf b/frontend/default.conf index 1a19b73..ce1ca1e 100644 --- a/frontend/default.conf +++ b/frontend/default.conf @@ -11,4 +11,6 @@ server { proxy_buffering off; proxy_pass http://127.0.0.1:3002/events; } + + } diff --git a/frontend/src/components/app/App.js b/frontend/src/components/app/App.js index dc5343c..f5a4061 100644 --- a/frontend/src/components/app/App.js +++ b/frontend/src/components/app/App.js @@ -4,7 +4,7 @@ import EventList from '../eventList/EventList.js'; function App(props) { const [events, setEvents] = useState(['EventLog']); // initialises Event state - const [sse, setSse] = useState(new EventSource('/events', { withCredentials: true})) // creates eventSource listener + const [sse, setSse] = useState(new EventSource('/events', { withCredentials: true})) // creates eventSource listener in state useEffect(() => {