Reduce environment variables number + optimise code

This commit is contained in:
Léo Carpentier 2022-02-11 11:36:53 +02:00
parent 9b73c08fc8
commit 08a261e491
3 changed files with 17 additions and 26 deletions

View File

@ -4,17 +4,12 @@ const minio = require('minio');
/*============== VARIABLE DECLARATION ==============*/ /*============== VARIABLE DECLARATION ==============*/
// Mongo set-up variables // Mongo set-up variables
const mongoEndpoint = process.env.ME_CONFIG_MONGODB_SERVER; const mongoCollection = process.env.MONGO_COLLECTION || 'eventlog';
const mongoPort = parseInt(process.env.ME_CONFIG_MONGODB_PORT); const mongoUri = process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/default?replicaSet=rs0';
const mongoDatabase = process.env.ME_CONFIG_MONGODB_DB;
const mongoCollection = process.env.ME_CONFIG_MONGODB_COLLECTION;
// Minio set-up variables // Minio set-up variables
const minioEndpoint = process.env.MINIO_ENDPOINT; const minioURI = new URL(process.env.MINIO_URI || 'http://kspace-mugshot:2mSI6HdbJ8@127.0.0.1:9000/kspace-mugshot');
const minioBucket = process.env.MINIO_BUCKET; const minioBucket = minioURI.pathname.substring(1);
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 // Stream set-up variables
let changeStream; let changeStream;
@ -23,24 +18,18 @@ const pipeline = [];
const PORT = process.env.PORT || 3002; const PORT = process.env.PORT || 3002;
// Create Mongo client // Create Mongo client
const MONGO_URI = `mongodb://${mongoEndpoint}:${mongoPort}`; const mongoClient = new MongoClient(mongoUri);
const mongoClient = new MongoClient(MONGO_URI);
/*============== CODE ==============*/ /*============== CODE ==============*/
async function run() { async function run() {
console.log('server.js has been launched') console.log('server.js has been launched');
const app = express(); const app = express();
// Configuring mongoDB connection // Configuring mongoDB connection
await mongoClient.connect(); await mongoClient.connect();
const database = mongoClient.db(mongoDatabase); const collection = mongoClient.db().collection(mongoCollection);
const collection = database.collection(mongoCollection);
// Notify connection
console.log(`Connection established with Mongo Database at ${MONGO_URI}`);
// Opening event listener on the database // Opening event listener on the database
changeStream = collection.watch(pipeline, options); changeStream = collection.watch(pipeline, options);
@ -50,18 +39,18 @@ async function run() {
let eventArray = []; let eventArray = [];
let minioClient = new minio.Client({ let minioClient = new minio.Client({
endPoint: minioEndpoint, endPoint: minioURI.hostname,
port: minioPort, port: parseInt(minioURI.port) || (minioURI.protocol == 'https' ? 443 : 80),
useSSL: false, useSSL: minioURI.protocol == 'https',
accessKey: minioKey, accessKey: minioURI.username,
secretKey: minioSecret secretKey: minioURI.password
}); });
// notifies GET requests // notifies GET requests
console.log('/events was requested'); console.log('/events was requested');
// Setting the header to event-stream for Server Sent Events (Eventsource) // 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.writeHead(200, "OK", header);
response.write('Connection established \n\n'); response.write('Connection established \n\n');
@ -79,7 +68,7 @@ async function run() {
const realURL = JSON.stringify({ picUrl: presignedUrl }); const realURL = JSON.stringify({ picUrl: presignedUrl });
eventArray = [realURL, ...eventArray]; eventArray = [realURL, ...eventArray];
}) })
} };
// sends updated array // sends updated array
response.write(`data: [${[...eventArray]}]\n\n`); response.write(`data: [${[...eventArray]}]\n\n`);

View File

@ -11,4 +11,6 @@ server {
proxy_buffering off; proxy_buffering off;
proxy_pass http://127.0.0.1:3002/events; proxy_pass http://127.0.0.1:3002/events;
} }
} }

View File

@ -4,7 +4,7 @@ import EventList from '../eventList/EventList.js';
function App(props) { function App(props) {
const [events, setEvents] = useState(['EventLog']); // initialises Event state 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(() => { useEffect(() => {