Skip to content

Commit 18f633c

Browse files
authored
Setup dynamic client variables (#32)
* Created env.js * Removed env.js for security reasons * Successfully substituted in env variables in main.js * Added new fields to configure.sh
1 parent f1006e1 commit 18f633c

File tree

7 files changed

+28
-21
lines changed

7 files changed

+28
-21
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
.env
22
node_modules
3-

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Populate ```.env``` file in project root.
66
```
77
PORT=XXXX
88
HOSTNAME=[0.0.0.0|127.0.0.X|localhost|etc.]
9+
MAILROOM_PORT=XXXX
10+
MAILROOM_HOSTNAME=[0.0.0.0|127.0.0.X|localhost|etc.]
911
```
1012

1113
Install dependencies.
@@ -19,4 +21,3 @@ $ node app.js
1921
```
2022

2123
(Or you can just run `deploy.sh` :wink:)
22-

app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
var express = require('express');
44
var app = express();
55
var dotenv = require('dotenv').config({path: __dirname+'/.env'});
6+
var fs = require('fs');
7+
8+
// Substitute special environment variables
9+
app.get("/js/main.js", function (req, res) {
10+
res.header("Content-Type",'application/javascript');
11+
12+
fs.readFile(__dirname+"/static/js/main.js", "utf-8", function (e, data) {
13+
if (e) throw e;
14+
15+
var load = data.replace(/{HOSTNAME}/g, process.env.MAILROOM_HOSTNAME).replace(/{PORT}/g, process.env.MAILROOM_PORT);
16+
res.send(load);
17+
});
18+
});
619

720
app.use('/', express.static(__dirname+'/static'));
821

@@ -11,4 +24,3 @@ app.listen(process.env.PORT, process.env.HOSTNAME, function () {
1124
console.log('Copilot Concierge service successfully running at '
1225
+ process.env.HOSTNAME + ':' + process.env.PORT);
1326
});
14-

configure.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ echo "What hostname do you want to run the app on?"
88
read HOST
99
echo "What port do you want to run the listener on?"
1010
read PORT
11+
echo "What is the mailroom's hostname?"
12+
read MAILROOM_HOSTNAME
13+
echo "What is the mailroom's port?"
14+
read MAILROOM_PORT
1115

1216
cat <<EOF > $CONFIG
1317
HOSTNAME=$HOST
1418
PORT=$PORT
19+
MAILROOM_HOSTNAME=$MAILROOM_HOSTNAME
20+
MAILROOM_PORT=$MAILROOM_PORT
1521
EOF
16-

static/index.html

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@ <h3 id="helper">Are you referring someone or is this personal?</h3>
2525

2626
<button class="inputButton" id="mainFieldSubmit">OK</button>
2727

28-
<!-- <input type="text" name="age" placeholder="Age"/>
29-
<select name="gender">
30-
<option value="" disabled selected>Gender</option>
31-
<option value="female">Female</option>
32-
<option value="male">Male</option>
33-
<option value="non-binary">Non-binary</option>
34-
</select>
35-
<select name="preferredComm">
36-
<option value="" disabled selected>Preferred Contact</option>
37-
<option value="email">Email</option>
38-
<option value="sms">SMS</option>
39-
</select>
40-
<input type="text" name="contact" placeholder="Contact" /><br />
41-
</div>
42-
<textarea name="situation" placeholder="Describe your situation..."></textarea> -->
4328
</div>
4429
</div>
4530
<div class="row">
@@ -62,5 +47,6 @@ <h3 id="helper">Are you referring someone or is this personal?</h3>
6247
<script src="//cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
6348
<script src="js/validate.js"></script>
6449
<script src="js/main.js"></script>
50+
<script src="js/env.js"></script>
6551
</body>
6652
</html>

static/js/env.js

Whitespace-only changes.

static/js/main.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
"use strict";
1+
"use strict";
2+
23
// Project Copilot Concierge Client
34
// Copyright 2016 Project Copilot
45

6+
var HOSTNAME = "{HOSTNAME}";
7+
var PORT = "{PORT}";
8+
59
// Load questions
610
$.getJSON("data/questions.json", function (questionList) {
711

@@ -242,7 +246,7 @@
242246
// make the call
243247
$.ajax({
244248
type: "POST",
245-
url: "http://localhost:3000/api/addUserRequest",
249+
url: "http://"+HOSTNAME+":"+PORT+"/api/addUserRequest",
246250
data: inputJSON,
247251
error: function(err) { // Something went wrong
248252
console.log(err);

0 commit comments

Comments
 (0)