-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs.js
342 lines (273 loc) · 9.16 KB
/
cs.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
process.title = 'CrowdSearcher';
Error.stackTraceLimit = process.env.PRODUCTION ? 15 : Infinity;
// Cluster related
var cluster = require( 'cluster' );
if ( cluster.isWorker ) {
var REPORT_STATS_INTERVAL = 5000;
var reportStats = function() {
process.send( {
cmd: 'stats',
id: cluster.worker.id,
memory: process.memoryUsage()
} );
};
setInterval( reportStats, REPORT_STATS_INTERVAL );
}
// Get the Configurator class
var Configurator = require( './config' );
// # Create the Express Object
var express = require( 'express' );
// Create the express application
var app = express();
// Load libraries
var _ = require( 'underscore' );
var fs = require( 'fs' );
var url = require( 'url' );
var path = require( 'path' );
var flash = require( 'connect-flash' );
var nconf = require( 'nconf' );
var domain = require( 'domain' );
var passport = require( 'passport' );
var moment = require( 'moment' );
var markdown = require( 'markdown' ).markdown;
var CS = require( './core' );
// Configure the Express app
// ---
// Directories
var publicPath = path.join( __dirname, 'public' );
var viewsPath = path.join( __dirname, 'views' );
var uploadPath = path.join( __dirname, 'uploads' );
// Create upload path if not exists
if ( !fs.existsSync( uploadPath ) )
fs.mkdirSync( uploadPath );
// Use JADE as template engine
app.set( 'views', viewsPath );
app.set( 'view engine', 'jade' );
// Export some properities to the views
app.locals( {
// Like the title
title: 'CrowdSearcher',
md: markdown.toHTML,
moment: moment,
_: _
} );
// Create a Domain to handle errors properly *for each request*
app.use( function( req, res, next ) {
var requestDomain = domain.create();
// Add the domain to the request, so ca be `exited` later
req.domain = requestDomain;
// Utility function used to wrap an async call to the api domain
var wrapInDomain = function( fn, ctx ) {
if ( _.isString( fn ) && !_.isUndefined( ctx ) )
fn = ctx[ fn ];
if ( ctx )
fn = fn.bind( ctx );
return requestDomain.bind( fn );
};
// Can be invoked using either `req.wrap` or `req.wrapInDomain`.
req.wrapInDomain = req.wrap = wrapInDomain;
// Bind Emitters
requestDomain.add( req );
requestDomain.add( res );
// On Domain error call the error middleware
requestDomain.on( 'error', next );
// Enter the domain to manage request unhandled exceptions
requestDomain.enter();
return next();
} );
// Use icon
app.use( express.favicon() );
// Log all the requests?
//app.use(express.logger('dev'));
// Used to handle uploaded files
app.use( express.bodyParser( {
uploadDir: uploadPath
} ) );
app.use( express.methodOverride() );
// ## CORS middleware
//
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
function allowCrossDomain( req, res, next ) {
if ( req.path.split( '/' )[ 1 ] === 'api' ) {
res.header( 'Access-Control-Allow-Origin', '*' );
res.header( 'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE' );
res.header( 'Access-Control-Allow-Headers', '*' );
//res.header('Access-Control-Allow-Headers', 'Accepts, Content-Type, Authorization, X-Requested-With');
res.header( 'Access-Control-Allow-Headers', req.get( 'Access-Control-Request-Headers' ) );
// intercept OPTIONS method
if ( 'OPTIONS' === req.method ) {
return res.send( 200 );
} else {
return next();
}
} else {
return next();
}
}
app.use( allowCrossDomain );
// Add session support
app.use( express.cookieParser() );
app.use( express.session( {
secret: 'CrowdSearcherJS'
} ) );
// Used with PassportJS to create *request scoped* messages
app.use( flash() );
// Complie on the fly stylus files
app.use( require( 'stylus' ).middleware( publicPath ) );
// Serve static contents from **publicPath**
app.use( express.static( publicPath ) );
// ### PassportJS
app.use( passport.initialize() );
app.use( passport.session() );
// Pass user to the views
app.use( function( req, res, next ) {
if ( req.path.split( '/' )[ 1 ] !== 'api' ) {
// Add th user to the views object
app.locals( {
user: req.user
} );
}
return next();
} );
// Manage app routes
app.use( app.router );
// Server
var serverError = function( error ) {
// Import the logger
var log = CS.log;
log.error( error );
if ( error.code === 'EADDRINUSE' ) {
log.error( 'Address is in use, try to change the PORT parameter in the configuration' );
}
if ( error.code === 'EACCESS' ) {
log.error( 'The user does not have the required privileges to run the application\nTry changind the PORT parameter in the configuration file or run as a different user' );
}
log.error( 'The server will be stopped and the process will exit' );
setTimeout( function() {
process.exit( 1 );
}, 1000 );
};
app.on( 'error', serverError );
// Load the configuration
var config = new Configurator( app );
// If error while configuring, then exit
config.on( 'error', function configError( err ) {
console.error( 'Error while configuring the app' );
console.error( err );
console.error( err.stack );
process.exit( 1 );
} );
// Once the configuration is ready
config.once( 'ready', function configReady() {
// Load the routers
var routes = require( './routes' );
var errorRoutes = require( './routes/error' );
var accountRoutes = require( './routes/account' );
// Import the logger
var log = CS.log;
var baseURL = nconf.get( 'webserver:externalAddress' );
// Pass the application external url to all the views
app.locals( {
appBase: baseURL
} );
// Configure the API's
try {
require( './routes/api' )( app );
} catch ( err ) {
log.error( err, 'API binding error' );
return config.emit( 'error', err );
}
// Error handling middleware
app.use( errorRoutes.error );
app.use( function( req, res, next ) {
// Exit from the domain.
req.domain.exit();
return next();
} );
// Auth middlewares
var authMiddleware = [
passport.authenticate( 'local', {
failureRedirect: baseURL + 'login',
failureFlash: true
} )
];
// Home page
app.get( '/', routes.index );
// CS Manager
var manager = require( './routes/manager' );
app.get( '/manage', routes.checkAuth, manager.index );
// Jobs
app.get( '/manage/jobs', routes.checkAuth, manager.jobs );
app.get( '/manage/job/new', routes.checkAuth, manager.newJob );
//app.post( '/manage/job/new', routes.checkAuth, manager.postJob );
app.get( '/manage/job/:id', routes.checkAuth, manager.job );
app.get( '/manage/job/:id/flows', routes.checkAuth, manager.flows );
// Tasks
app.get( '/manage/task/new', routes.checkAuth, manager.newTask );
//app.post( '/manage/task/new', routes.checkAuth, manager.postTask );
app.get( '/manage/task/:id', routes.checkAuth, manager.task );
// Microtasks
app.get( '/manage/microtask/:id', routes.checkAuth, manager.microtask );
// Objects
app.get( '/manage/object/:id', routes.checkAuth, manager.object );
// Answers
app.get( '/manage/answers', routes.checkAuth, manager.answers );
// Dashboard
app.get( '/manage/:entity/:id/dashboard', routes.checkAuth, manager.dashboard );
// Control Mart
app.get( '/manage/:entity/:id/controlmart', routes.checkAuth, manager.controlmart );
app.get( '/manage/:entity/:id/mart', routes.checkAuth, manager.controlmart );
var accountRedirect = function( req, res ) {
log.trace( 'Session destination: %s', req.session.destination );
var destination = req.session.destination;
// Remove the session parameter
req.session.destination = null;
// Redirect
res.redirect( url.resolve( baseURL, destination || 'account' ) );
};
// User related
app.get( '/register', routes.register );
app.post( '/register', routes.postRegister, accountRedirect );
app.get( '/login', routes.login );
app.post( '/login', authMiddleware, accountRedirect );
app.get( '/logout', routes.logout );
app.get( '/account', routes.checkAuth, accountRoutes.index );
// Autentication endpoints based on configured socual networks
var socialMap = CS.social;
_.each( socialMap, function( config, name ) {
var authConfig = _.defaults( {
failureRedirect: baseURL + 'login',
failureFlash: true
}, config.authConfig );
var passportAuth = passport.authorize( name, authConfig );
app.get( '/connect/' + name, passportAuth, accountRedirect );
app.get( '/connect/' + name + '/callback', passportAuth, accountRedirect );
} );
// Handle random request
app.all( '*', function randomUrlHandler( req, res ) {
log.warn( 'Requested "%s %s" by %s', req.method, req.originalUrl, req.ip );
res.status( 404 );
res.format( {
html: function() {
res.render( 'YUHERE', {
title: 'Missing resource'
} );
},
json: function() {
res.json( {
message: 'Missing resource',
method: req.method,
path: req.originalUrl,
ip: req.ip
} );
}
} );
} );
// Eventually START SERVER!
log.debug( 'Starting server on port ' + config.getPort() );
app.listen( config.getPort(), function runningServer() {
log.info( 'Server running on port ' + config.getPort() );
} );
} );
// Load configuration, and trigger the associated events.
config.load();