Skip to content

Commit 66042c8

Browse files
committed
Use dotenv for secrets in game backend
1 parent 0bc6120 commit 66042c8

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,5 @@ MigrationBackup/
351351
# Node modules
352352
*node_modules/
353353

354-
# Game client secrets
355-
*secrets.js
354+
# Game backend env file
355+
*.env

GameBackendSocketIO/package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GameBackendSocketIO/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"axios": "^0.19.2",
10+
"dotenv": "^8.2.0",
1011
"express": "^4.17.1",
1112
"mongodb": "^3.5.5",
1213
"nodemon": "^2.0.2",

GameBackendSocketIO/server.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

2+
if (process.env.NODE_ENV !== 'production') {
3+
require('dotenv').config();
4+
}
5+
26
const app = require('express')();
37
const http = require('http').createServer(app);
48
const io = require('socket.io')(http);
59
const mongoClient = require("mongodb").MongoClient;
610
const axios = require('axios');
711

8-
// TODO: use env variables instead
9-
const SECRETS = require('./secrets');
10-
const CONNECTION_STRING = SECRETS.MONGO_CONNECTION_STRING;
12+
const PORT = process.env.PORT || 3000;
1113

1214

1315
// For testing purposes only, will be creating a client in separate repo
@@ -22,15 +24,15 @@ app.get('/test2', function (req, res) {
2224

2325
var database, collection;
2426

25-
http.listen(3000, function () {
26-
console.log('Listening on *:3000');
27-
mongoClient.connect(CONNECTION_STRING, { useNewUrlParser: true, useUnifiedTopology: true }, (error, client) => {
27+
http.listen(PORT, function () {
28+
console.log(`Listening on *:${PORT}`);
29+
mongoClient.connect(process.env.MONGO_CONNECTION_STRING, { useNewUrlParser: true, useUnifiedTopology: true }, (error, client) => {
2830
if (error) {
2931
throw error;
3032
}
31-
database = client.db(SECRETS.MONGO_DB_NAME);
32-
collection = database.collection(SECRETS.MONGO_COLLECTION_NAME);
33-
console.log(`Connected to database`);
33+
database = client.db(process.env.MONGO_DB_NAME);
34+
collection = database.collection(process.env.MONGO_COLLECTION_NAME);
35+
console.log('Connected to database');
3436
});
3537
});
3638

@@ -80,8 +82,8 @@ io.on('connection', function (socket) {
8082

8183
// fetch auth token and save to DB
8284
axios.post('http://qbquestionsapi.azurewebsites.net/api/authenticate', {
83-
"username": SECRETS.QBQUESTIONS_API_USERNAME,
84-
"password": SECRETS.QBQUESTIONS_API_PASSWORD
85+
"username": process.env.QBQUESTIONS_API_USERNAME,
86+
"password": process.env.QBQUESTIONS_API_PASSWORD
8587
}).then(response => {
8688
console.log(response.data);
8789

0 commit comments

Comments
 (0)