-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
170 lines (143 loc) · 5.21 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require('dotenv').config();
var cluster = require('cluster');
if (process.env.NODE_ENV === 'production') {
require('newrelic');
require('@newrelic/aws-sdk');
}
var distanceUtils = require('./utils/distance-utils');
var siteUtils = require('./utils/site-utils');
function saveState() {
sites = [];
available = [];
base('MaVaccSites_Today')
.select({
// Selecting the first 3 records in Grid view:
view: 'Default all site view',
})
.eachPage(
function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
records.forEach(function (record) {
const data = siteUtils.getDataFromRecord(record);
sites.push(data);
if (data.availability) {
available.push(data);
}
});
// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
},
function done(err) {
if (err) {
console.error(err);
return;
}
}
);
setTimeout(saveState, 60000);
}
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for terminating workers
cluster.on('exit', function (worker) {
// Replace the terminated workers
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var Airtable = require('airtable');
var base;
if (process.env.AIRTABLE_API_KEY) {
base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(
'applrO42eyJ3rUQyb'
);
} else {
console.error('AIRTABLE_API_KEY should be set in .env');
}
var sites = [];
var available = [];
if (process.env.AIRTABLE_API_KEY) {
saveState();
}
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
var next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();
AWS.config.region = process.env.REGION;
app.prepare().then(() => {
var server = express();
server.enable('trust proxy');
server.use(bodyParser.urlencoded({extended: false}));
server.use(bodyParser.json());
server.use(function (req, res, next) {
if (req.secure || process.env.NODE_ENV === 'development') {
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
}
});
server.use('/robots.txt', function (req, res) {
res.type('text/plain');
res.send('User-agent: *\nAllow: /');
});
server.get('/initmap', function (req, res) {
res.send(sites);
});
server.get('/volunteer:/volunteer/updater', (req, res) => {
return app.render(req, res, '/volunteer', req.query);
});
server.use(express.static('static'));
server.post('/search_query_location', async function (req, res) {
var locations = [];
if (req.body.availability === 'Sites with reported doses') {
locations = available;
} else {
locations = sites;
}
try {
const {lat, lng} = await distanceUtils.getLatLngFromRequest(
req
);
const siteData = distanceUtils.getClosestLocations(
locations,
lat,
lng,
req.body.maxMiles
);
res.send({siteData, lat, lng});
} catch (exception) {
console.error('Failed to geocode', exception);
res.status(500).send('Failed geocoding request.');
}
});
// THE API ROUTES WE HAVE DEFINED NEED TO BE ADDED HERE:
const locationApi = require('./routes/locations');
server.use('/locations', locationApi);
const volunteersApi = require('./routes/volunteers');
server.use('/volunteers', volunteersApi);
const volunteersLocationsApi = require('./routes/volunteerLocations');
server.use('/volunteers-locations', volunteersLocationsApi);
/**
* Next.js (React) pages defined in the /pages folder will automatically be routed
* without the need to define each route in this Nodejs file.
*/
server.all('*', handle);
// Start the server.
var port = process.env.PORT || 3002;
server.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
});
}