-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
72 lines (56 loc) · 1.72 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
// External dependencies
var express = require( 'express' ),
bodyParser = require( 'body-parser' ),
passport = require( 'passport' ),
cors = require( 'cors' );
// Dependencies
var logger = require( './app/lib/logger' ),
environment = require( './app/lib/environment' ),
router = require( './app/lib/router' ),
middleware = require( './app/lib/routing-middleware' ),
db = require( './data' ),
schema = require( './data/schema' ),
migrations = require( './data/migrations' );
// Local variables
var app = express();
/**
* Initialise
*/
var init = function init() {
// Setup middleware
app.use( middleware.logRequest );
app.use( middleware.removeTrailingSlash );
app.use( cors() );
app.use( bodyParser.urlencoded( { extended: true } ) );
app.use( bodyParser.json() );
app.use( passport.initialize() );
app.use( router );
app.use( middleware.routeNotFound );
// Setup models
require( './app/models' );
// Setup controllers
require( './app/controllers' );
// Log the environment
logger.debug( JSON.stringify( environment ) );
// Ensure the database is set up correctly and then start the server
return db.setupDatabase( schema, migrations ).then( function() {
var server = app.listen( environment.port, function () {
logger.debug( 'ribot API listening at %s', environment.baseUrl );
// Start the workers
require( './app/workers' );
} );
} )
.catch( function( error ) {
logger.error( 'Cannot start server due to error', error.stack || error );
process.exit( 1 );
} );
};
// Get the party started, unless we're running tests
if ( environment.name != 'test' ) {
init();
}
// Exports
module.exports = {
init: init,
app: app
};