forked from onepiecejs/nodejs-cantas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (134 loc) · 3.93 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
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
143
144
145
146
147
148
149
var express = require('express');
var mongoose = require('mongoose');
var card = require('./models/card');
var list = require('./models/list');
var board = require('./models/board');
var routes = require('./routes');
var sockets = require('./sockets');
var settings = require('./settings');
var utils = require('./services/utils');
var connect = require('express/node_modules/connect');
var redis = require('socket.io/node_modules/redis');
var RedisStore = require('socket.io/lib/stores/redis');
var RedisSessionStore = require('connect-redis')(express);
var sessionStore = new RedisSessionStore({
port: settings.redis.port,
host: settings.redis.host,
ttl: settings.redis.ttl,
pass: settings.redis.password,
});
var app = express.createServer();
var sio;
var passport = require('./services/auth');
var redisClients = {
redisPub: redis.createClient(
settings.redis.port,
settings.redis.host
),
redisSub: redis.createClient(
settings.redis.port,
settings.redis.host
),
redisClient: redis.createClient(
settings.redis.port,
settings.redis.host
)
};
if (settings.redis.password) {
redisClients.redisPub.auth(settings.redis.password);
redisClients.redisSub.auth(settings.redis.password);
redisClients.redisClient.auth(settings.redis.password);
}
var siteUrl;
var siteId = 0;
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({
secret: 'keyboard cat',
key: 'express.sid',
store: sessionStore
}));
app.use(express.bodyParser({uploadDir: __dirname + '/uploads'}));
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
app.configure('development', function() {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}
));
app.set("production", false);
});
app.configure('production', function() {
app.use(express.errorHandler());
app.set("production", true);
});
if (app.settings.production) {
siteId = settings.piwikSiteId.product;
siteUrl = settings.piwikSiteId.productUrl;
} else {
siteId = settings.piwikSiteId.stage;
siteUrl = settings.piwikSiteId.stageUrl;
}
app.helpers({
links: settings.links,
version: utils.get_version(),
siteId: siteId,
siteUrl: siteUrl
});
routes.init(app, passport, sessionStore);
if (settings.mongodb.url) {
mongoose.connect(settings.mongodb.url);
} else {
mongoose.connect(
settings.mongodb.host,
settings.mongodb.name,
settings.mongodb.port,
{
user: settings.mongodb.user,
pass: settings.mongodb.pass
}
);
}
// Ignore validate SSL CA at global scope
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
app.listen(settings.app.port, settings.app.host, function() {
//FIXME keep it here,when i understand the concept, i can remove it totally
// if (app.settings.production) {
// We have to limit node to run under non-privilege user account
// to ensure security in production environment.
// User with name ``username`` should exist at this time.
// process.setuid(settings.management.service.username);
// }
});
sio = require('socket.io').listen(app);
sockets.init(sio, sessionStore);
sio.configure(function () {
// Set store for socket.io to use RedisStore instead of MemoryStore
sio.set('store', new RedisStore(redisClients));
// enable debugging mode:
// https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
// 0 - error
// 1 - warn
// 2 - info
// 3 - debug
if (app.settings.production) {
sio.set('log level', 0);
} else {
sio.set('log level', 3);
}
sio.set('transports', [
'websocket',
'xhr-polling'
]);
});
console.log("Express server listening on port %s", settings.app.port);