forked from Superjisan/Informr.US
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (57 loc) · 1.89 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
if (!process.env.OPEN_STATES_API_KEY) {
require('dotenv').config();
}
const _ = require('lodash');
const OpenStates = require('openstates');
const express = require('express');
const Promise = require('promise');
const rp = require('request-promise');
const heroku = require("heroku-ping");
const apiKey = process.env.OPEN_STATES_API_KEY;
const openstates = new OpenStates(apiKey);
var app = express();
/* At the top, with other redirect methods before other routes */
app.use(function(req,res,next) {
if(process.env.PORT && req.headers['x-forwarded-proto'] !== 'https') {
//needed for heroku redirects
res.redirect('https://informr.us'+req.url)
} else {
next()
}
});
app.use(express.static('src'));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index.html')
});
app.get('/geolookup/:lat/:lon', (req, res) => {
const lat = req.params.lat;
const lon = req.params.lon;
const statesPromise = new Promise(function(resolve, reject) {
openstates.geoLookup(lat, lon, function(err, res) {
if (err) reject(err);
else resolve(res);
});
});
const congressUrl = `https://congress.api.sunlightfoundation.com/legislators/locate?latitude=${lat}&longitude=${lon}&apikey=${apiKey}`;
const congressPromise = rp(congressUrl).then(body => JSON.parse(body).results);
Promise.all([statesPromise, congressPromise])
.then(results => res.send(_.flatten(results)))
.catch(err => {
throw err;
})
});
app.listen(process.env.PORT || 3002, () => {
console.log(`Server started at ${process.env.PORT || 3002}`);
});
//ONLY do this on prod
if (process.env.PORT) {
heroku.ping({
interval: 300000, // milliseconds, defaults to 30 minutes
silent: false, // logging (default: false)
apps: [{
name: 'inform-r-us', // heroku app name - required
secure: true // requires https (defaults: false)
}]
});
}