From e712800cc853e45c6365b3e3f0c5c7db6f9eb4b3 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Thu, 23 Mar 2023 22:35:50 +0200 Subject: freq_GCF: freq implementation as Google Cloud Function; added security options --- javascript/freq.js | 66 +++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) (limited to 'javascript/freq.js') diff --git a/javascript/freq.js b/javascript/freq.js index 19c42f5..0bbdb5b 100644 --- a/javascript/freq.js +++ b/javascript/freq.js @@ -9,6 +9,10 @@ * #3 Supporting functions * #4 Output functions * #5 Entry-point function main() + * + * Requirements + * npm install dotenv --save + * */ // #1 @@ -24,7 +28,7 @@ const os = require('os'); const {Storage} = require('@google-cloud/storage'); // import Google Cloud client library - +require('dotenv').config({ path: '../auth/.env' }) // #2 @@ -38,19 +42,9 @@ const projectId = 'pythia-251711'; const keyFilename = '../auth/pythia-251711-047e3d5e6608.json'; -// cloud_environment (boolean) -// set value to: -// true : if running on Google Cloud Functions -// false : if running localy -// --- -- -- - - - -const cloud_environment = false; // ** check - - // temp file of frequencies (json) -const saves_dir = '../results'; // ** check -const temp_file = 'freq.json'; // ** check +const temp_file = '../results/freq.json'; -const local_dir = (cloud_environment) ? os.tmpdir() : saves_dir; // db connection parametres @@ -58,20 +52,13 @@ const local_dir = (cloud_environment) ? os.tmpdir() : saves_dir; // ----------------------------------------------------------------------------- // while on local machine using cloud_sql_proxy -var con = mysql.createConnection({ // ** check - host: "127.0.0.1", - user: "pythia_db_user_dev", - password: "VnEP0eysjiXDHcfM", - database: "dev_pythia_db" +var con = mysql.createConnection({ + host: process.env.DB_HOST; + user: process.env.DB_USER; + password: process.env.DB_PASS; + database: process.env.DB_NAME; }); -// // while on Google Cloud Functions -// var con = mysql.createConnection({ // ** check -// socketPath: "/cloudsql/pythia-251711:europe-west4:pythia-db-eu", -// user: "pythia_services", -// password: process.env.DB_PASSWORD, -// database: "pythia_db" -// }); // MAIN OUTPUT OF THE SCRIPT // arrays that need to be constructed and filled with data @@ -163,11 +150,8 @@ async function upload_file( bucketName, srcFilePath, trgFilePath ) { /** main() * - * nodejs's exported function - * used as entry-point function (in case of Google Cloud function) + * the entry-point function */ - -// exports.main = () => { // google cloud function entry point var main = () => { // connect; @@ -176,12 +160,12 @@ var main = () => { // save dictionary; // end script; // ----------------------------------------------------------------------------- - con.connect(function(err) { // comment this out for 'Cannot enqueue Handshake after already enqueuing' error - // connect; // comment this out for 'Cannot enqueue Handshake after already enqueuing' error - if (err) throw err; // comment this out for 'Cannot enqueue Handshake after already enqueuing' error - console.log("Connected!"); // comment this out for 'Cannot enqueue Handshake after already enqueuing' error + con.connect(function(err) { + // connect; + if (err) throw err; + console.log("Connected!"); - // SQL to get needed data + // SQL to get needed data var sql = "SELECT count(pl.eys_code) as FREQuency,\ pl.product_id as product_id,\ pb.brand_name,\ @@ -216,15 +200,15 @@ var main = () => { process.exit(1); }); - /// // echo memory stats - /// const used = process.memoryUsage(); - /// for (let key in used) { - /// console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`); - /// } + // echo memory stats + const used = process.memoryUsage(); + for (let key in used) { + console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`); + } - // process.exit(1); // exit + // process.exit(); // exit }); - }); // comment this out for 'Cannot enqueue Handshake after already enqueuing' error + }); } // NOTE: @@ -232,4 +216,4 @@ var main = () => { // if running through google cloud-functions you do NOT need to call main() // (you need define main() as the entry-point function instead) // --- -- -- - - - -main(); // ** check \ No newline at end of file +main(); \ No newline at end of file -- cgit v1.2.3