-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (123 loc) · 4.72 KB
/
app.js
File metadata and controls
142 lines (123 loc) · 4.72 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
// define vars
var startInDevMode = process.argv[2] !== 'prod',
application_root = __dirname,
express = require("express"),
path = require("path"),
mongoose = require('mongoose'),
fs = require('fs'),
configs = JSON.parse(fs.readFileSync('package.json', 'utf8')),
faye = require('faye'),
util = require('./rest/utils'),
minfier = require('./utils/minifier'),
bayeux = new faye.NodeAdapter({mount: '/rest', timeout: 120}),
client = new faye.Client(configs.server.faye.host + ':' + configs.server.faye.port + '/rest'),
app = express.createServer();
// connect to mongodb
mongoose.connect(configs.server.mongo.host);
// configure express server
app.configure(function(){
app.use(express.bodyParser());
app.use(express.cookieParser('teabo'));
app.use(express.methodOverride());
app.use(express.session({secret:'teabo'}));
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('views', path.join(application_root, "views"));
app.set('view engine', 'jade');
});
process.on('uncaughtException', function(err) {
console.log("\n\n ##############\n\n");
console.log(err);
console.log("\n\n ##############\n\n");
});
// load teabo modules
var moduleTemplates = [],
moduleStyles = [],
moduleNames = [];
fs.readdirSync('./modules').forEach(function(file) {
var name = file,
module = require('./modules/'+name+'/module.js'),
cssDir = application_root+'/modules/'+name+'/public/css';
moduleTemplates = moduleTemplates.filter(function(e){return e;});
moduleTemplates.push(module.template);
if(startInDevMode) {
moduleStyles = moduleStyles.filter(function(e){return e;});
// get all css files
if(path.existsSync(cssDir)) {
fs.readdirSync(cssDir).forEach(function(file) {
moduleStyles.push('<link rel="stylesheet" href="/'+name+'/css/'+file+'">\n');
});
}
} else {
// read file content of all css files and compress it
if(path.existsSync(cssDir)) {
fs.readdirSync(cssDir).forEach(function(file) {
fs.readFile(cssDir+'/'+file, 'utf8', function (err,data) {
if (err) {
console.log(err);
return false;
}
moduleStyles.push(minfier.compressCSS(data));
});
});
}
}
moduleNames.push(name);
app.use('/'+name,express.static(application_root+'/modules/'+name+'/public'));
if(typeof module.init === 'function') {
module.init();
}
registerRestServices(module.rest);
registerIOServices(module.io);
console.log(file + ' module loaded');
});
// generate index.html
app.get('/', function(req,res) {
var header = fs.readFileSync('./templates/header.tpl', 'utf8'),
script = '<script>var modules = [\'' + moduleNames.join('\',\'') + '\'], fayeURL = \'' + configs.server.faye.host + ':' + configs.server.faye.port + '/rest\', appVersion = \'' + configs.version + '\';</script>',
styles = startInDevMode ? moduleStyles.join('') : '<style type="text/css">' + moduleStyles.join('') + '</style>';
res.send(header + script + '\n' + styles + '\n\n' + moduleTemplates.join('\n'));
});
// register rest services
registerRestServices(util.rest);
function registerRestServices(rest) {
if(rest) {
console.log(rest);
for(var i = 0; i < rest.length; ++i) {
var url = rest[i].url;
var method = rest[i].type;
var callback = rest[i].callback;
switch(method){
case 'get':
app.get(url,callback);
break;
case 'post':
app.post(url,callback);
break;
case 'delete':
app.delete(url,callback);
break;
case 'put':
app.put(url,callback);
break;
}
}
}
}
// register IO services
function registerIOServices(io) {
if(io) {
bayeux.bind('publish', function(client_id,channel,obj) {
if(io[channel]) {
io[channel](bayeux,channel,obj);
}
});
}
}
bayeux.bind('handshake', function(clientId) {
console.log('user connected to faye');
client.publish(clientId,'meta/handshake',true);
});
bayeux.listen(configs.server.faye.port);
app.listen(configs.server.express.port);