forked from teaDesign/VotingInfoApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (87 loc) · 3.48 KB
/
app.js
File metadata and controls
105 lines (87 loc) · 3.48 KB
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
/**
* Module dependencies.
*/
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
var logger = (require('./logging/vip-winston')).Logger;
var config = require('./config');
var compress = require('compression');
var express = require('express');
var favicon = require('express-favicon');
var morgan = require('morgan');
var methodOverride = require('method-override');
var cookieParser = require('cookie-parser');
var serveIndex = require('serve-index');
var errorhandler = require('errorhandler');
var http = require('http');
var path = require('path');
var fs = require('fs');
var sqs = require('./aws/sqs');
var notificationServices = require('./notifications/services');
var pgServices = require('./pg/services');
var dataVerificationServices = require('./data-verification/services');
var authServices = require('./authentication/services');
var dataUploadServices = require('./data-upload/services');
var earlyVoteServices = require('./early-vote/services');
var dasherServices = require('./dasher/services');
var app = express();
logger.info('=========================================================');
logger.info('VIP App Started');
logger.info('=========================================================');
var redirectHttps = function(req, res, next) {
if (req.header("X-Forwarded-Proto") === "http" && req.path !== "/ping") {
res.redirect("https://" + req.headers.host);
} else {
next();
}
};
// all environments
app.use(compress());
app.use(favicon(config.web.favicon));
app.use(morgan('combined'));
app.use(express.json());
app.use(express.urlencoded());
app.use(methodOverride());
app.use(cookieParser());
// Redirect non-https load balanced clients to https
app.use(redirectHttps);
// /ping for load balancer health checking
app.get('/ping', function (req, res, next) { res.send('pong'); });
app.use(express.static(path.join(__dirname, 'public')));
app.use('/feeds', serveIndex(path.join(__dirname, 'feeds')));
app.use('/feeds', express.static(path.join(__dirname, 'feeds')));
// development only
if ('development' == app.get('env')) {
app.use(errorhandler());
logger.info('Running in Development Mode.');
}
//register REST services
notificationServices.registerNotificationServices(app);
pgServices.registerPostgresServices(app);
dataVerificationServices.registerDataVerificationServices(app);
authServices.registerAuthServices(app);
dataUploadServices.registerDataUploadServices(app);
dataVerificationServices.registerDataVerificationServices(app);
earlyVoteServices.registerEarlyVoteServices(app);
dasherServices.registerDasherServices(app);
app.get ('/config/vit', authServices.checkJwt, function (req, res, next) {
res.send(config.vit.apiKey);
});
http.createServer(app).listen(config.web.port, function () {
logger.info('Express server listening on port ' + config.web.port);
});
var pg = require('pg');
var connString = "postgres://" + process.env.DB_ENV_POSTGRES_USER +
":" + process.env.DB_ENV_POSTGRES_PASSWORD +
"@" + process.env.DB_PORT_5432_TCP_ADDR +
":" + process.env.DB_PORT_5432_TCP_PORT +
"/" + process.env.DB_ENV_POSTGRES_DATABASE +
"?application_name=dashboard";
process.env.DATABASE_URL = connString;
pg.connect(connString, function(err, client, done) {
if(err) {
return logger.error('Could not connect to PostgreSQL. Error fetching client from pool: ', err);
}
logger.info('Connected to PostgreSQL. May your queries terminate before a 3 minute timeout.');
});