-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
164 lines (140 loc) · 4.28 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
/**
* Module dependencies.
*/
var express = require('express')
, domain = require('domain')
, crypto = require('crypto')
, connect = require('connect')
, fs = require('fs')
, path = require('path')
, S = require('string')
, http = require('http')
, https = require('https')
, _mysql = require('mysql')
, moment = require('moment')
, geo = require('geo')
, geoip = require('geoip-lite')
, janrain = require('janrain-api')
, check = require('validator').check
, sanitize = require('validator').sanitize
, expressWinston = require('express-winston')
, winston = require('winston')
, config = require('config')
, dbConn = require('./lib/mysqlConn')
, errMsg = require('./lib/errMsg')
, rmysql = dbConn.rmysql
, wmysql = dbConn.wmysql
, amysql = dbConn.amysql;
/**
* Logging setup
*/
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: 'logs/access.log', json: true, timestamp: true })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: 'logs/exceptions.log', json: true, timestamp: true })
],
exitOnError: false
});
// API config settings
var engageAPI = janrain(config.Janrain.Key);
var salt = config.Hash.salt;
var stripeKey = config.Stripe.stripeKey;
var stripe = require('stripe')(stripeKey);
// ## CORS middleware
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, token, ltype');
res.header('Access-Control-Max-Age', '300');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
};
// SSL Certificate values
var options = {
key: fs.readFileSync('ssl/api.fitstew.com/api.fitstew.com-key.pem'),
ca: fs.readFileSync('ssl/api.fitstew.com/gd_bundle.crt'),
cert: fs.readFileSync('ssl/api.fitstew.com/api.fitstew.com.crt'),
requestCert: true
}
// Build initial express Server
var app = module.exports = express();
// Set express server options
app.configure(function(){
app.use(allowCrossDomain);
app.use(express.favicon());
//app.use(connect.compress());
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(expressWinston.logger({
transports: [
new winston.transports.File({
filename: 'logs/requests.log',
json: true,
colorize: true
})
]
}));
app.use(app.router);
app.use(function(req, res, next) {
var createDomain = domain.create();
createDomain.on('error', function(err) {
res.statusCode = 500;
res.end(err.message + '\n');
console.log("Req Domain Error: " + err);
process.exit();
createDomain.dispose();
});
createDomain.enter()
next();
});
});
var routePath=__dirname + "/routes/";
fs.readdirSync(routePath).forEach(function(file) {
if(path.extname(file) == '.js') {
var route=routePath+file;
require(route)(app);
}
});
// Default Route
app.get('/', function(req, res){
res.send('FitStew API')
});
// Health Check Route
app.get('/api/healthMe/', function(req, res){
rmysql.query('SELECT id FROM transactions LIMIT 1', function(err, result) {
if(err || result.length < 1) {
res.send('"status": "failed"');
} else {
res.end('"status": "success');
}
});
});
//Set Cluster setting for workers
var cluster = require('cluster');
var http = require('http');
var numCPUs = config.Node.workers;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
});
} else {
// Set Server to listen on specified ports
http.createServer(app).listen(config.Express.http);
console.log("started server on " + config.Express.http);
https.createServer(options, app).listen(config.Express.https);
console.log("started server on " + config.Express.https);
}