-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
142 lines (124 loc) · 3.91 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
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
'use strict';
var xmpp = require('xmpp-ftw')
, express = require('express')
, app = express()
, engine = require('ejs-locals')
, Emitter = require('primus-emitter')
, Primus = require('primus')
, helmet = require('helmet')
, winston = require('winston')
, winstonConfig = require('winston-config')
, bodyParser = require('body-parser')
, methodOverride = require('method-override')
, morgan = require('morgan')
, errorHandler = require('errorhandler')
var port = process.env.PORT || process.argv[2] || 3000
helmet.defaults(app)
/* jshint -W098 */
try {
var config = require('./config/logging.json')
} catch (e) {
var config = require('./config/logger.default.json')
}
winstonConfig.fromJson(config, function(error, winston) {
if (error) {
console.error('Error configuring winston')
process.exit(1)
}
})
var server = require('http').createServer(app)
server.listen(port)
winston.info('Server started and listening on port ' + port)
var version = require('xmpp-ftw/package.json').version
var options = {
transformer: 'socket.io',
parser: 'JSON',
transports: [
'websocket',
'htmlfile',
'xhr-polling',
'jsonp-polling'
]
}
var primus = new Primus(server, options)
primus.use('emitter', Emitter)
primus.save(__dirname + '/public/scripts/primus.js')
var Muc = require('xmpp-ftw-muc')
var Disco = require('xmpp-ftw-disco')
var Pubsub = require('xmpp-ftw-pubsub')
var Register = require('xmpp-ftw-register')
var Superfeedr = require('xmpp-ftw-superfeedr')
var Buddycloud = require('xmpp-ftw-buddycloud')
var Avatar = require('xmpp-ftw-avatar')
var Search = require('xmpp-ftw-search')
var Rpc = require('xmpp-ftw-rpc')
var Fanout = require('xmpp-ftw-fanout')
var Jingle = require('xmpp-ftw-jingle')
var Mam = require('xmpp-ftw-mam')
var Command = require('xmpp-ftw-command')
var Ping = require('xmpp-ftw-ping')
primus.on('connection', function(socket) {
var xmppFtw = new xmpp.Xmpp(socket)
xmppFtw.setLogger(winston)
xmppFtw.addListener(new Muc())
xmppFtw.addListener(new Disco())
xmppFtw.addListener(new Pubsub())
xmppFtw.addListener(new Register())
xmppFtw.addListener(new Superfeedr())
xmppFtw.addListener(new Buddycloud())
xmppFtw.addListener(new Avatar())
xmppFtw.addListener(new Search())
xmppFtw.addListener(new Rpc())
xmppFtw.addListener(new Fanout())
xmppFtw.addListener(new Jingle())
xmppFtw.addListener(new Mam())
xmppFtw.addListener(new Command())
xmppFtw.addListener(new Ping())
socket.xmppFtw = xmppFtw
})
primus.on('disconnection', function(socket) {
console.log('Client disconnected, logging them out')
socket.xmppFtw.logout()
})
var readme = require('express-middleware-readme.md')
readme.setOptions({
htmlWrap: {
meta: [
{ charset: 'utf-8' }
],
title: 'XMPP-FTW Github README.md'
}
})
var addCorsHeaders = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With')
next()
}
app.disable('x-powered-by')
app.use(express.static(__dirname + '/public'))
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
app.use(bodyParser())
app.use(methodOverride())
app.use(readme.run)
app.use(addCorsHeaders)
app.use(morgan())
app.set('strict routing', false)
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}))
app.engine('ejs', engine)
var configuration = {
ga: process.env.GOOGLE_ANALYTICS_ID || null,
webmasterTools: process.env.GOOGLE_WEBMASTER_TOOLS || null,
body: {},
title: 'XMPP-FTW ⟫ ',
version: version
}
require('./lib/routes')(app, configuration)
process.on('uncaughtException', function(error) {
// Try and prevent issues crashing the whole system
// for other users too
console.error(error)
})