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 ==============*/
// 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`);

View File

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

View File

@ -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(() => {