forked from gmalysa/ld2l
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
184 lines (159 loc) · 5.08 KB
/
server.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Main server entry point, this configures express and attaches the relevant pathes,
* most of which are defined in other files.
*/
// Configuration options
var mod_name = 'ld2l';
var mod_version = '0.1.0';
var config = require('./config.js');
// node.js and other libraries
require('colors');
var express = require('express');
var _ = require('underscore');
var db = require('db-filters');
var fl = require('flux-link');
var dotaconstants = require('dotaconstants');
var dust = require('dustjs-linkedin');
require('dustjs-helpers');
const child_process = require('child_process');
// Express middleware
var bodyParser = require('body-parser');
var expressSession = require('express-session');
var sessionFileStore = require('session-file-store')(expressSession);
var passport = require('passport');
var siosession = require('express-socket.io-session');
var siopassport = require('passport.socketio');
// local modules
var logger = require('./logger');
var mysql = require('./mysql');
var common = require('./common');
var version = {};
/**
* Get version from git as the current commit hash
*/
child_process.exec("git log -n 1 --pretty=oneline", function(err, out, stderr) {
version.v = out.split(' ')[0];
});
mysql.init();
/**
* Create the cache objects that are used within one request to reduce how much we hit the db
* while requesting multiple of the same thing (i.e. teams, users, etc.)
*/
function init_cache(env, after) {
env._teamCache = {};
after();
}
// Initialize showdown, the markdown converter for various content on the site
// These settings should match those specified in the makefile
var showdown = require('showdown');
var showdown_converter = new showdown.Converter({
parseImgDimensions : true,
simplifiedAutoLink : true,
excludeTrailingPunctuationFromURLs : true,
strikethrough : true,
tables : true,
tasklists : true,
emoji : true
});
function dust_markdown_filter(value) {
return showdown_converter.makeHtml(value);
}
// Initialize express
var server = express();
_.each(config.set, function(v, k) {
server.set(v, k);
});
_.each(config.enable, function(v, k) {
server.enable(v, k);
});
// Add current time as early as possible in requests
server.use(function(req, res, next) {
res.start = process.hrtime();
next();
});
var sessionStorage = new sessionFileStore({
ttl : 25 * 24 * 60 * 60
});
var sessionMiddleware = expressSession({
secret : config.session_secret,
resave : false,
saveUninitialized : true,
cookie : {
maxAge : 25 * 24 * 60 * 60 * 1000,
},
store : sessionStorage,
rolling : true
});
// Configure middleware that runs before route handlers
server.use(config.static_path, express.static(config.static_dir));
server.use(sessionMiddleware);
server.use(bodyParser.urlencoded({extended : false}));
server.use(bodyParser.json({limit : '1mb'}));
server.use(passport.initialize());
server.use(passport.session());
var ci = new common.init(server, {
shutdown : [{fn : mysql.cleanup}],
base_url : config.base_url,
template_dir : config.template_dir,
client_prefix : config.client_prefix,
both_prefix : config.both_prefix,
client_path : config.client_path,
route_dir : config.route_dir,
port : config.port
});
// Connect socket.io middleware to interface with express middleware
ci.io.use(siosession(sessionMiddleware));
ci.io.use(siopassport.authorize({
key : 'connect.sid',
secret : config.session_secret,
store : sessionStorage
}));
// Send server version to client on startup
ci.io.on('connect', function(socket) {
var timer = {
timer : null
};
timer.timer = setInterval(function() {
socket.emit('ping', '');
}, 20000);
if (undefined !== version.v) {
socket.emit('version', version.v);
}
// Track some extra events and print them
socket.on('error', function(err) {
logger.var_dump(err, 'sio error');
});
socket.on('disconnect', function(reason) {
// Skip normal disconnect reasons
if ('transport close' != reason)
logger.var_dump(reason, 'sio disconnect');
clearInterval(timer);
});
});
// Add helpers and hooks
ci.add_pre_hook(fl.mkfn(mysql.init_db, 0));
ci.add_pre_hook(fl.mkfn(init_cache, 0));
ci.add_finally_hook(fl.mkfn(mysql.cleanup_db, 0));
ci.add_dust_filters({md : dust_markdown_filter});
ci.add_dust_helpers({
dota_hero_icon : function(chunk, context, bodies, params) {
var hero = dust.helpers.tap(params.hero, chunk, context);
if (undefined !== dotaconstants.heroes[hero]) {
var img = "http://cdn.dota2.com/" + dotaconstants.heroes[hero].img;
var alt = dotaconstants.heroes[hero].localized_name;
chunk.write('<img title="'+alt+'" class="ld2l-dota-hero" src="'+img+'" />');
}
return chunk;
},
dota_item_icon : function(chunk, context, bodies, params) {
var item = dust.helpers.tap(params.item, chunk, context);
if (undefined !== dotaconstants.items[dotaconstants.item_ids[item]]) {
var img = "http://cdn.dota2.com/" + dotaconstants.items[dotaconstants.item_ids[item]].img;
var alt = dotaconstants.items[dotaconstants.item_ids[item]].dname;
chunk.write('<img title="'+alt+'" class="ld2l-dota-item" src="'+img+'" />');
}
return chunk;
}
});
// Finally!
logger.module_init(mod_name, mod_version, 'ld2l online');