forked from luisjesuspellicer/JajaTweets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (68 loc) · 2.56 KB
/
index.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
/**
* Authors: Diego Ceresuela, Luis Jesús Pellicer, Raúl Piracés.
* Date: 16-05-2016
* Name file: index.js
* Description: This file contains the main behaviour of the web app.
*
* Launches all the web services listed in webservices,js
* PORT = 3000
* STATIC_CONTENT = ./app
*/
(function() {
'use strict';
var express = require('express');
var session = require('express-session');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var methodOverride = require('method-override');
var _ = require('lodash');
var passport = require('passport');
var Cron = require('./common/cron');
// Create the application.
var app = express();
var request = require('request');
// Add Middleware necessary for REST API's
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Override'));
var OAuth = require('oauth').OAuth;
// Connect to MongoDB, selects between environment variable (Heroku DB) or other (custom DB)
mongoose.connect(process.env.MONGODB_URI || 'mongodb://admin:mongomongo1@ds013981.mlab.com:13981/heroku_vw307h03');
mongoose.connection.once('open', function() {
// Load all the models
var webservices = require('./webservices');
webservices.forEach(function(ws){
app.models = _.extend(app.models,require('./'+ws+'/models/index'));
});
// Load passport module
require('./config/passport.config');
// Authentication configuration
app.use(session({
resave: false,
saveUninitialized: true,
name: "id",
jwt: null,
secret: process.env.MY_SECRET
}));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
// Load all the routes
var routes = {};
webservices.forEach(function(ws){
routes = _.extend(routes,require('./'+ws+'/routes'));
});
// Configure all the endpoints
_.each(routes, function(controller, route){
app.use(route,controller(app, route));
});
// Static content
app.use(express.static('app'));
// Listen in the port specified, environment variable for Heroku, or custom on localhost
app.listen(process.env.PORT || 3000);
console.log('Server has started...');
// Starts cron process for pending tweets
Cron.startCron();
});
})();