-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.js
More file actions
257 lines (210 loc) · 6.13 KB
/
server.js
File metadata and controls
257 lines (210 loc) · 6.13 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var express = require('express');
var exphbs = require('express-handlebars');
var fs = require('fs');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var _ = require('lodash');
var moment = require('moment');
// In case of uncaught exception, print the full-stack
process.on('uncaughtException', function(err) {
console.error((err && err.stack) ? err.stack : err);
});
var app = express();
GLOBAL.app = app;
app.config = require('./config');
app.helpers = require('./helpers');
app.locals.default_email = app.config.default_email;
// view engine setup
app.engine('hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
helpers: {
toJSON : function(object) {
return JSON.stringify(object);
},
ifCond : function(obj1, sign, obj2, options) {
switch(sign){
case '==':
return obj1 == obj2 ? options.fn(this):options.inverse(this);
case '===':
return obj1 === obj2 ? options.fn(this):options.inverse(this);
case '!=':
return obj1 != obj2 ? options.fn(this):options.inverse(this);
case '>=':
return obj1 >= obj2 ? options.fn(this):options.inverse(this);
case '<=':
return obj1 <= obj2 ? options.fn(this):options.inverse(this);
case '>':
return obj1 > obj2 ? options.fn(this):options.inverse(this);
case '<':
return obj1 < obj2 ? options.fn(this):options.inverse(this);
default:
console.error('no sign match:', sign);
break;
}
},
moment: function(context, block){
if (context && context.hash) {
block = _.cloneDeep(context);
context = undefined;
}
var date = moment(context);
var hasFormat = false;
// Reset the language back to default before doing anything else
date.lang('en');
for (var i in block.hash) {
if (i === 'format') {
hasFormat = true;
}
else if (date[i]) {
date = date[i](block.hash[i]);
} else {
console.log('moment.js does not support "' + i + '"');
}
}
if (hasFormat) {
date = date.format(block.hash.format);
}
return date;
}
},
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
// // uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.models = require('./models');
var session = require('express-session')
app.use(session({
secret: 'kjh2398sh2nlsd',
resave: false,
saveUninitialized: false
}));
// set up a route to redirect http to https (in case dns not setup)
app.get('*',function(req,res,next){
if(app.config.force_https && !req.secure){
var domain = req.host;
return res.redirect('https://' + domain + req.url);
}
next();
})
app.use('/', function(req, res, next){
// setup session id if not already set
if(!req.session.id){
req.session.id = require('guid').raw();
}
// update session settings
req.session.config = req.session.config || {};
var defaultsToUse = [
'signing_location',
'authentication',
'access_code'
];
_.each(defaultsToUse, function(key){
req.session.config[key] = (key in req.session.config) ? req.session.config[key] : app.config[key];
});
next();
});
app.use('/', require('./routes/index'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err.stack
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var privateKey = fs.readFileSync('sslcerts/server.key');
var certificate = fs.readFileSync('sslcerts/server.crt');
var credentials = {
key: privateKey,
cert: certificate
};
// Create the HTTP and HTTPS servers
var server = require('http').Server(app);
var httpsServer = require('https').Server(credentials, app);
app.setup = require('./setup');
// Start listening after signing in to DocuSign and retrieving our AccountID
app.config.loginToDocuSign(function(err){
if(err){
console.error('loginToDocuSign failure');
return console.error(err);
}
// Check for template existance
app.setup.Templates(function(err){
if(err){
console.log('Templates Error');
console.error(err);
// server.listen(port);
return;
// return console.error(err);
}
// app.setup.Brands(function(err){
// if(err){
// return console.error(err);
// }
// server.listen(port);
////////////////////////////////////////////////
// Start the server
////////////////////////////////////////////////
server.listen(3801, function() {
console.log('HTTP being served');
});
httpsServer.listen(8443, function() {
console.log('HTTPS being served');
});
// });
});
});
server.on('error', onError);
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
module.exports = app;