From 132e0e2ef303a4420b0c24c416c039bb78134cbb Mon Sep 17 00:00:00 2001 From: Amy Fromandi Date: Mon, 23 Sep 2024 11:34:06 -0500 Subject: [PATCH] Changing to use environment variables --- .dockerignore | 1 - .gitignore | 2 -- README.md | 3 +++ v2/column-cache-refresh.ts | 3 +-- v2/definitions/sources.ts | 3 +-- v2/larkin.ts | 19 +++++++++++++------ 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.dockerignore b/.dockerignore index a70e4542..3c3629e6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1 @@ node_modules -credentials.ts diff --git a/.gitignore b/.gitignore index b319fe55..5cd2d286 100755 --- a/.gitignore +++ b/.gitignore @@ -16,8 +16,6 @@ v1/utilities/scripts/credentials.py venv .env -!v2/credentials.ts -credentials.ts !v1/credentials.ts v2/utilities/scripts/credentials.py diff --git a/README.md b/README.md index 1b2aca03..f9568d45 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ NOTE: Postgres connections are completely broken in node v14 and v15 (as of This will also run the script `postinstall.sh` which copies credentials files into place. + + +**Old config below. We now use env variables specifying which db and environment to connect to.** For `credentials.ts` fill in your MariaDB and PostgreSQL user information, update the port for Redis if necessary, and follow the inline instructions for generating a cache refresh key. The cache refresh key is used as a secret diff --git a/v2/column-cache-refresh.ts b/v2/column-cache-refresh.ts index 46238918..533815b7 100644 --- a/v2/column-cache-refresh.ts +++ b/v2/column-cache-refresh.ts @@ -1,11 +1,10 @@ const larkin = require("./larkin"); -const credentials = require("./credentials"); module.exports = (req, res, next) => { if ( req.query && req.query.cacheRefreshKey && - req.query.cacheRefreshKey === credentials.cacheRefreshKey + req.query.cacheRefreshKey === process.env.CACHE_REFRESH_KEY ) { larkin.setupCache(); res.json({ success: "cache refreshed" }); diff --git a/v2/definitions/sources.ts b/v2/definitions/sources.ts index 4f06c120..81f32155 100644 --- a/v2/definitions/sources.ts +++ b/v2/definitions/sources.ts @@ -4,7 +4,6 @@ var api = require("../api"), dbgeo = require("dbgeo"), gp = require("geojson-precision"); -const credentials = require("../credentials"); module.exports = function (req, res, next, cb) { if (Object.keys(req.query).length < 1) { @@ -80,7 +79,7 @@ module.exports = function (req, res, next, cb) { ${"sample" in req.query ? "LIMIT 5" : ""} `; larkin.queryPg( - credentials.pg_macrostrat_database, + "burwell", sql, params, function (error, result) { diff --git a/v2/larkin.ts b/v2/larkin.ts index 7117043f..f2ed3f2a 100644 --- a/v2/larkin.ts +++ b/v2/larkin.ts @@ -1,7 +1,6 @@ var //mysql = require("mysql"), async = require("async"), _ = require("underscore"), - credentials = require("./credentials"), csv = require("csv-express"), api = require("./api"), defs = require("./defs"), @@ -11,6 +10,13 @@ var //mysql = require("mysql"), const named = require("yesql").pg; const { Client, Pool } = require("pg"); +require('dotenv').config(); + +const postgresDatabases = { + burwell: "macrostrat", + geomacro: "geomacro", + elevation: "elevation", +}; (function () { var larkin = {}; @@ -72,7 +78,7 @@ const { Client, Pool } = require("pg"); //added new method to query from Maria data in the new PG database after migration larkin.queryPg = function (db, sql, params, callback) { //add console.logs for debug mode in the future - const nameMapping = credentials.postgresDatabases ?? {}; + const nameMapping = postgresDatabases ?? {}; const dbName = nameMapping[db] ?? db; if (dbName == "geomacro") { @@ -80,15 +86,16 @@ const { Client, Pool } = require("pg"); "In Macrostrat v2, 'geomacro' is merged with 'burwell' into the 'macrostrat' database.", ); } - let connectionDetails = {...process.env.MACROSTRAT_DEV_URL}; + let connectionDetails = process.env.MACROSTRAT_DEV_URL; if (dbName == "elevation") { /* Special case for elevation database (temporary) */ - connectionDetails.database = 'elevation' + connectionDetails = process.env.ELEVATION_DEV_URL; } - const pool = new Pool(connectionDetails); - console.log(connectionDetails) + const pool = new Pool({ + connectionString: connectionDetails + }); pool.connect(function (err, client, done) { if (err) {