-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
126 lines (109 loc) · 3.8 KB
/
main.js
File metadata and controls
126 lines (109 loc) · 3.8 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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var cors = require('cors');
var fs = require('fs');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var _ = require('underscore');
var q = require('q');
var MongoClient = require('mongodb').MongoClient;
var lingerDB = require('./db');
//app.use(favicon(__dirname + '/public/favicon.ico'));
//app.use(logger('dev'));
app.use(cors());
app.use(bodyParser.json({ limit: "2mb" }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
key: 'sid'
}));
var PUBLIC_DIR = __dirname + "/www";
var CORDOVA_WWW = __dirname + "/platforms/browser/www";
// expose BACKEND_SERVER_URL globally
var env = (process.env['ENVIRONMENT'] || "local").toLocaleLowerCase();
eval(fs.readFileSync(__dirname + "/config/config-" + env + ".js", "utf8"));
GLOBAL.BACKEND_SERVER_URL = BACKEND_SERVER_URL;
app.use('/js', express.static(PUBLIC_DIR + '/js'));
app.use('/css', express.static(PUBLIC_DIR + '/css'));
app.use('/html', express.static(PUBLIC_DIR + '/html'));
app.use('/fonts', express.static(PUBLIC_DIR + '/fonts'));
app.use('/images', express.static(PUBLIC_DIR + '/images'));
app.use('/plugins', express.static(CORDOVA_WWW + '/plugins')); // cordova
function initializeDB(db) {
db.collection('groups').createIndex({"location": "2dsphere"}, function(err) {
if(err) {
console.log(err);
}
});
db.collection('Messages').createIndex( { timestamp : 1 } ), function(err) {
if(err) {
console.log(err);
}
};
function empty(col) {
var deferred = q.defer();
db.collection(col).remove({}, function() {
deferred.resolve();
});
return deferred.promise;
}
if(fs.existsSync(__dirname + "/db/dump")) {
console.log(">> Please wait, importing data...");
var collections = ["users","groups","messages"];
var all = [];
_.each(collections, function(col) {
all.push(empty(col));
});
q.all(all).then(function() {
// import
var exec = require('child_process').exec;
exec(/^win/.test(process.platform) ? 'mongorestore.exe' : "mongorestore", {
cwd: __dirname + '/db'
}, function(error, stdout, stderr) {
if(error) {
console.log(error);
console.log(">> ERROR IMPORTING DATA");
}
else {
console.log(">> Import success");
}
});
})
}
}
MongoClient.connect('mongodb://127.0.0.1:27017/lingerdb', function(err, db) {
if (err) throw err;
console.log(">> Connected to Database");
initializeDB(db);
var mongo = lingerDB.getAdapter('mongodb')(db);
app.use(function(req, res, next) {
req.db = mongo;
req.io = io;
next();
});
require('./api/chat/Listener')(io,mongo);
//initializeDB(db);
app.use("/api", require('./api'));
app.all('/cordova.js', function(req, res) {
res.sendFile(CORDOVA_WWW + '/cordova.js');
});
app.all('/cordova_plugins.js', function(req, res) {
res.sendFile(CORDOVA_WWW + '/cordova_plugins.js');
});
app.all('/*', function(req, res) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile(PUBLIC_DIR + "/index.html");
});
});
module.exports = server;
//module.exports = app;