forked from devshelf/devshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
235 lines (175 loc) · 6.27 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Module dependencies */
var express = require('express')
, gzippo = require('gzippo')
, colors = require('colors')
, fs = require('fs')
, mustache = require('mustache')
, everyauth = require('everyauth')
, path = require('path')
, JSON5 = require('json5')
, MongoStore = require('connect-mongostore')(express)
;
/* /Module dependencies */
/* Global vars */
global.articlesData = {}; //all-data.json obj with articles by lang (articlesData.en/ru/etc)
global.articlesIDs = {}; //all-data.json ID lists by lang (articlesIDs.en/ru/etc)
global.tagLinks = {}; //global object with tag links
global.appDir = path.dirname(require.main.filename); //path to project dir
global.MODE = process.env.NODE_ENV || 'development';
global.app = express();
global.opts = require('./core/options/'); //Global options
global.commonOpts = JSON5.parse(fs.readFileSync(__dirname + '/core/options/common-options.json5', "utf8")); //Common options with Front-end
/* /Global vars */
/*
* Data
* */
global.indexData = {};
global.indexData[global.opts.l18n.defaultLang] = JSON5.parse(fs.readFileSync(__dirname + '/public/index.json5', "utf8"));
//filling lang properties
global.opts.l18n.additionalLangs.map(function(lang) {
global.indexData[lang] = JSON5.parse(fs.readFileSync(__dirname + '/public/'+lang+'/index.json5', "utf8"));
});
/*
* Update local information from git hub and regenerate all-data.json
* */
var articlesJson = require('./core/generate-data');
require('./core/updateData');
//Preparing initial data on start
articlesJson.generateData();
/**
* Session
*/
app.use(express.bodyParser())
.use(express.cookieParser(global.opts.cookieSecret));
app.use(express.session({
secret: global.opts.cookieSecret,
store: new MongoStore({
'db': 'sessions',
host: global.opts.remoteDBhost,
port: global.opts.remoteDBport
})
}));
app.use(function (req, res, next) {
res.cookie('app-mode', global.MODE, { maxAge: 3600000, httpOnly: false });
// keep executing the router middleware
next();
});
/**
* Localization & geo-ip service
*/
app.use(require('./core/lang'));
app.post('/lang', function (req, res, next) {
var currentLang = req.body.lang || global.opts.l18n.defaultLang;
res.cookie('lang', currentLang, { maxAge: 3600000, httpOnly: false });
res.send();
});
/*
* auth module
* */
require('./core/auth');
app.use(everyauth.middleware());
var authDoneTpl = fs.readFileSync(__dirname+'/views/auth-done.html', "utf8");
app.get('/auth/stub', function (req, res) {
var lang = req.cookies.lang || global.opts.l18n.defaultLang;
var indexJson = global.indexData[lang];
indexJson.authDone = false;
var htmlToSend = mustache.to_html(authDoneTpl, indexJson);
res.send(htmlToSend);
});
app.get('/auth/done', function (req, res) {
var lang = req.cookies.lang || global.opts.l18n.defaultLang;
//Creating cachedAuth for keeping auth after app restart
req.session.authCache = req.session.auth;
var indexJson = global.indexData[lang];
indexJson.user = JSON5.stringify(req.session.authCache.github.user);
indexJson.authDone = true;
var htmlToSend = mustache.to_html(authDoneTpl, indexJson);
res.send(htmlToSend);
});
/*
* git api form
* */
if (global.opts.form.enabled) {
var form = require('./core/form');
app.get('/post-article', form.postArticle); // preparing encoded data from changed file
}
/**
* Validation
*/
if (global.opts.validate.enabled) {
var validation = require('./core/check-title'),
checkUrl = require('./core/check-url-status'),
validate = require('./core/validate');
app.get('/check-title', validation.checkTitleService); //Check unique title
app.get('/check-url', checkUrl.checkURLService); //URL checker
app.get('/validate', validate.validateService); //Validate all
}
/*
* web routing
* */
// Route for static files
app.set('route', __dirname + '/public');
app
.use(gzippo.staticGzip(app.get('route')))
.use(gzippo.compress());
//main page
app.get('/', function(req, res) {
var lang = req.cookies.lang || global.opts.l18n.defaultLang;
//text data
var indexJson = {records: global.indexData[lang]};
//for dynamic options update
indexJson.commonOpts = global.commonOpts;
//link to tags catalogues for main page
indexJson.catalogue = global.tagLinks[lang];
//Auth data
if (req.session.authCache && typeof req.session.authCache.github.user === 'object' || typeof req.user === 'object') {
indexJson.auth = true;
indexJson.authToken = req.session.authCache.github.accessToken;
} else {
indexJson.auth = false;
}
//Production mode in templates
if (global.MODE === 'production') { indexJson.production = true; }
//Preparing for client
var clientIndexJson = {},
clientIndexJsonFields = ['commonOpts','auth', 'authToken','records'];
clientIndexJsonFields.map(function(item){
clientIndexJson[item] = indexJson[item];
});
indexJson.appData = JSON5.stringify(clientIndexJson);
var indexPage = fs.readFileSync(__dirname + '/public/build/index.html', "utf8");
var htmlToSend = mustache.to_html(indexPage, indexJson);
res.send(htmlToSend);
});
/*
* voting module (requiring place matters)
* */
var voting = require('./core/voting');
if (global.opts.voting.enabled) {
app.get('/plusVotes', voting.plusVotes); // post arguments: id, user
app.get('/minusVotes', voting.minusVotes); // post arguments: id, user
app.get('/getVotes', voting.getVotes); // post arguments: id
app.get('/getAllVotes', voting.getAllVotes);
}
// Preparing initial data on start
voting.generateVotingData(global.opts.l18n.defaultLang);
global.opts.l18n.additionalLangs.map(function(lang) {
voting.generateVotingData(lang);
});
/*
* error hadnling
* */
if (global.MODE === 'production') {
app.use(function(err, req, res, next) {
console.log(err);
res.send(404, '404');
});
app.use(function(err, req, res, next) {
console.log(err);
res.send(500, '500');
});
}
var appPort = global.MODE === 'development' ? global.opts.app.devPort : global.opts.app.port;
app.listen(appPort);
var appPortString = appPort.toString();
console.log('[DevShelf] is working on '.blue + appPortString.blue + ' port in '.blue + global.MODE.blue + ' mode...'.blue);