Skip to content

Commit 097963f

Browse files
committed
new file: config/keys_sample.js mirroring ./config/keys.js
separate dotenv initialization: index.js new file: loadSecrets.js load secret keys as a export module modified: routes/currency-exchange-routes.js modified: routes/weatherbit-routes.js
1 parent 1fad149 commit 097963f

File tree

5 files changed

+97
-49
lines changed

5 files changed

+97
-49
lines changed

config/keys_sample.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// add this file to .gitignore
2+
3+
const keys = {
4+
google: {
5+
GOOGLECLIENT_ID: 'xxxxxxxxxx',
6+
GOOGLECLIENT_SECRET: 'xxxxxxxxxxx'
7+
},
8+
mongodb: {
9+
MONGODB_CONSTR: 'mongodb+srv://[userid]:[password]@cluster0.brf1j.mongodb.net/oautho?authSource=admin&replicaSet=Cluster0-shard-0&readPreference=primary&ssl=true'
10+
},
11+
session: {
12+
cookieKey: 'xxxxxxxxxxxxxxxx'
13+
},
14+
development: {
15+
port: '1111',
16+
},
17+
weatherbitapi: {
18+
WEATHERBIT_URI: 'https://api.weatherbit.io/v2.0/',
19+
WEATHERBIT_APIKEY: 'xxxxxxxxxxxxxxxx'
20+
},
21+
exchangerateapi: {
22+
EXCHANGERATE_URI: 'https://v6.exchangerate-api.com/v6/',
23+
EXCHANGERATE_APIKEY: 'xxxxxxxxxxxxxxxxxxxxx',
24+
}
25+
};
26+
27+
export { keys };

index.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,7 @@ import { compile } from 'ejs';
99
import { serialize, deserialize, googleStrategy } from './config/passport-setup.js';
1010
import {encryptAES, decryptAES} from './crypto.js';
1111

12-
// dev env
13-
dotenv.config();
14-
15-
// application secrets
16-
// import { keys } from './config/keys.js';
17-
18-
let EXCHANGE_RATE_APIKEY;
19-
let EXCHANGE_BASE_URI='https://v6.exchangerate-api.com/v6/';
20-
let WEATHERBIT_KEY;
21-
let WEATHERBIT_URI='https://api.weatherbit.io/v2.0/';
22-
let GoogleclientID;
23-
let GoogleclientSecret;
24-
let MongoDBConString;
25-
let CookieKey = 'c93da061ab7f984267e36c8431645035d611bc892c58f0e64614c68a4384a179126e7ed0b5829e460f292f72e9ef4facb68a0894cb2425ba046b82a3bae0b529';
26-
27-
if (process.env.NODE_ENV === "production") {
28-
EXCHANGE_RATE_APIKEY = process.env.EXCHANGE_RATE_APIKEY;
29-
WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
30-
GoogleclientID = process.env.GoogleclientID;
31-
GoogleclientSecret = process.env.GoogleclientSecret;
32-
MongoDBConString = process.env.MONGODB_URI;
33-
} else {
34-
// dynamically importing keys.js using promise:
35-
import('./config/keys.js').then((secrets) => {
36-
EXCHANGE_RATE_APIKEY = secrets.keys.exchangerateapi.EXCHANGE_APIKEY;
37-
WEATHERBIT_KEY = secrets.keys.weatherbitapi.WEATHERBIT_APIKEY;
38-
GoogleclientID = secrets.keys.google.clientID;
39-
GoogleclientSecret = secrets.keys.google.clientSecret;
40-
MongoDBConString = secrets.keys.mongodb.connectionString;
41-
});
42-
}
43-
12+
import { CookieKey, MongoDBConString } from './loadSecrets.js';
4413

4514
// Create network routing
4615
const app = express();
@@ -130,5 +99,3 @@ app.listen(port, () => {
13099
console.log('Sorry, failed to launch.');
131100
}
132101
});
133-
134-
export { EXCHANGE_RATE_APIKEY, EXCHANGE_BASE_URI, WEATHERBIT_KEY, WEATHERBIT_URI, GoogleclientID, GoogleclientSecret };

loadSecrets.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* IMPORTANT SECRETS */
2+
/*
3+
* To load environment variables, this file must be initialized
4+
* at the top of the application
5+
*
6+
* This file depends on ./config/keys.js in development mode!
7+
*/
8+
9+
import * as dotenv from 'dotenv';
10+
11+
// dev env
12+
dotenv.config();
13+
14+
let EXCHANGE_RATE_APIKEY;
15+
let WEATHERBIT_KEY;
16+
let GoogleclientID;
17+
let GoogleclientSecret;
18+
let MongoDBConString;
19+
let WEATHERBIT_URI='https://api.weatherbit.io/v2.0/';
20+
let EXCHANGE_RATE_URI='https://v6.exchangerate-api.com/v6/';
21+
let CookieKey = 'c93da061ab7f984267e36c8431645035d611bc892c58f0e64614c68a4384a179126e7ed0b5829e460f292f72e9ef4facb68a0894cb2425ba046b82a3bae0b529';
22+
23+
if (process.env.NODE_ENV === "production") {
24+
EXCHANGE_RATE_APIKEY = process.env.EXCHANGE_RATE_APIKEY;
25+
WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
26+
GoogleclientID = process.env.GOOGLECLIENT_ID;
27+
GoogleclientSecret = process.env.GOOGLECLIENT_SECRET;
28+
MongoDBConString = process.env.MONGODB_URI;
29+
console.log('Loaded keys from dotenv');
30+
} else {
31+
// dynamically importing keys.js using promise:
32+
let secrets = await getSecrets();
33+
// console.log(secrets);
34+
if(secrets) {
35+
EXCHANGE_RATE_APIKEY = secrets.keys.exchangerateapi.EXCHANGERATE_APIKEY;
36+
WEATHERBIT_KEY = secrets.keys.weatherbitapi.WEATHERBIT_APIKEY;
37+
GoogleclientID = secrets.keys.google.GOOGLECLIENT_ID;
38+
GoogleclientSecret = secrets.keys.google.GOOGLECLIENT_SECRET;
39+
MongoDBConString = secrets.keys.mongodb.MONGODB_CONSTR;
40+
};
41+
}
42+
43+
/*
44+
* Async function loading keys dynamically from ./config/keys.js
45+
* IMPORTANT NOTE: ./config/keys.js is not saved in the archive!
46+
* It is meant to be used in development environment only.
47+
* For `production` build, you must change NODE_ENV accordingly.
48+
*/
49+
function getSecrets(){
50+
return new Promise(resolve => {
51+
setTimeout(() => {
52+
try {
53+
import('./config/keys.js').then((secrets) => {
54+
resolve(secrets);
55+
});
56+
} catch (err) {
57+
console.log(err);
58+
resolve(null);
59+
}
60+
})
61+
}, 200);
62+
}
63+
64+
65+
export { EXCHANGE_RATE_APIKEY, EXCHANGE_RATE_URI, WEATHERBIT_KEY, WEATHERBIT_URI, GoogleclientID, GoogleclientSecret, CookieKey, MongoDBConString };

routes/currency-exchange-routes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { data as currencyCodes } from 'currency-codes';
22
import request from 'request';
33
import express from 'express';
4-
import { EXCHANGE_RATE_APIKEY, EXCHANGE_BASE_URI } from '../index.js';
54

5+
import { EXCHANGE_RATE_APIKEY, EXCHANGE_RATE_URI } from '../loadSecrets.js';
66

77
const router = express.Router();
88

@@ -34,11 +34,11 @@ router.post('/', (req, res) => {
3434
function getExchangeRateData(fromCurrency, toCurrency, amount) {
3535
return new Promise(resolve => {
3636
setTimeout(() => {
37-
let URLStr = EXCHANGE_BASE_URI + EXCHANGE_RATE_APIKEY + '/pair/' + fromCurrency + '/' + toCurrency + '/' + amount;
37+
let URLStr = EXCHANGE_RATE_URI + EXCHANGE_RATE_APIKEY + '/pair/' + fromCurrency + '/' + toCurrency + '/' + amount;
3838
console.log(URLStr);
3939
try {
4040
if (fromCurrency === undefined) {
41-
URLStr = EXCHANGE_BASE_URI + EXCHANGE_RATE_APIKEY + '/pair/EUR/GBP/1';
41+
URLStr = EXCHANGE_RATE_URI + EXCHANGE_RATE_APIKEY + '/pair/EUR/GBP/1';
4242
}
4343
console.log("Calling URL: " + URLStr);
4444
request(URLStr, async function (err, response, body) {

routes/weatherbit-routes.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
import express from 'express';
22
import request from 'request';
33

4-
import { WEATHERBIT_KEY, WEATHERBIT_URI } from '../index.js';
4+
import { WEATHERBIT_KEY, WEATHERBIT_URI } from '../loadSecrets.js';
55

66
const router = express.Router();
77

8-
// let WEATHERBIT_KEY;
9-
// let WEATHERBIT_URI;
10-
11-
// if (process.env.NODE_ENV === "production") {
12-
// WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
13-
// WEATHERBIT_URI = process.env.WEATHERBIT_URI;
14-
// } else {
15-
// WEATHERBIT_KEY = keys.weatherbitapi.APIKEY;
16-
// WEATHERBIT_URI = keys.weatherbitapi.BASE_URI;
17-
// }
18-
198
router.get('/', (req, res) => {
209
res.render('pages/weatherbit', {user: req.user});
2110
});

0 commit comments

Comments
 (0)