forked from silexlabs/Silex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
240 lines (215 loc) · 7.06 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
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
236
237
238
239
//////////////////////////////////////////////////
// Silex, live web creation
// http://projects.silexlabs.org/?/silex/
//
// Copyright (c) 2012 Silex Labs
// http://www.silexlabs.org/
//
// Silex is available under the GPL license
// http://www.silexlabs.org/silex/silex-licensing/
//////////////////////////////////////////////////
// node modules
var pathModule = require('path');
var unifile = require('unifile');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var multipart = require('connect-multiparty');
var FSStore = require('connect-fs2')(session);
var http = require('http');
var https = require('https');
var fs = require('fs');
// init express
var app = express();
// gzip/deflate outgoing responses
var compression = require('compression');
app.use(compression());
// parse data for file upload
app.use('/', multipart({limit: '100mb'}));
// parse data for post and get requests
app.use('/', bodyParser.urlencoded({
extended: true,
limit: '10mb'
}));
app.use('/', bodyParser.json({limit: '10mb'}));
app.use('/', cookieParser());
// get silex config
var silexConfig = unifile.defaultConfig;
if (fs.existsSync(pathModule.resolve(__dirname, 'config.js'))) {
var obj = require(pathModule.resolve(__dirname, 'config.js'));
for (var prop in obj) {
silexConfig[prop] = obj[prop];
}
}
// session management
var sessionFolder = process.env.SILEX_SESSION_FOLDER || pathModule.resolve(__dirname, '../sessions');
app.use('/', session({
secret: silexConfig.sessionSecret,
name: silexConfig.cookieName,
resave: true,
saveUninitialized: false,
store: new FSStore({
dir: sessionFolder
}),
cookie: {
maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
}
}));
// ********************************
// production / debug
// ********************************
/**
* catch all errors to prevent nodejs server crash
*/
function onCatchError(err) {
console.log('---------------------');
console.error('---------------------', 'Caught exception: ', err, err.stack, '---------------------');
console.log('---------------------');
}
if(process.env.SILEX_DEBUG) {
// DEBUG ONLY
console.warn('Running server in debug mode');
// define users (login/password) wich will be authorized to access the www folder (read and write)
silexConfig.www.USERS = {
'admin': 'admin'
};
silexConfig.openPages.ENABLED = true;
}
else {
// PRODUCTION ONLY
console.warn('Running server in production mode');
// catch all errors and prevent nodejs to crash, production mode
process.on('uncaughtException', onCatchError);
// reset debug
silexConfig.www.USERS = {};
silexConfig.openPages.ENABLED = false;
}
// ********************************
// unifile server
// ********************************
// force ssl if the env var SILEX_FORCE_HTTPS is set
if(process.env.SILEX_FORCE_HTTPS) {
console.log('force SSL is active (env var SILEX_FORCE_HTTPS is set)');
var forceSSL = require('express-force-ssl');
app.set('forceSSLOptions', {
trustXFPHeader: !!process.env.SILEX_FORCE_HTTPS_TRUST_XFP_HEADER
});
app.use(forceSSL);
}
else {
console.log('force SSL NOT active (env var SILEX_FORCE_HTTPS is NOT set)');
}
// change www root
silexConfig.www.ROOT = pathModule.resolve(__dirname, '../../dist/client');
// add static folders
silexConfig.staticFolders.push(
// silex main site
{
path: pathModule.resolve(__dirname, '../../dist/client')
},
// debug silex, for js source map
{
name: '/js/src',
path: pathModule.resolve(__dirname, '../../src')
},
// the scripts which have to be available in all versions (v2.1, v2.2, v2.3, ...)
{
name: '/static',
path: pathModule.resolve(__dirname, '../../static')
}
);
// open pages if defined
if(silexConfig.openPages) {
silexConfig.openPages.ROOT = pathModule.resolve(__dirname, '../../open-pages/');
silexConfig.openPages.SQLLITE_FILE = pathModule.resolve(__dirname, '../../open-pages/websites.sql');
fs.mkdir(silexConfig.openPages.ROOT, null, function (error) {});
}
// github service
if(process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
console.log(process.env.GITHUB_CLIENT_ID);
silexConfig.github.client_id = process.env.GITHUB_CLIENT_ID;
silexConfig.github.client_secret = process.env.GITHUB_CLIENT_SECRET;
}
// SSL certificate
try {
var privateKey = fs.readFileSync(process.env.SILEX_SSL_PRIVATE_KEY || __dirname + '/../../privatekey.pem').toString();
var certificate = fs.readFileSync(process.env.SILEX_SSL_CERTIFICATE || __dirname + '/../../certificate.pem').toString();
var options = {
key: privatekey,
cert: certificate
};
}
catch(e) {
console.warn('SSL certificate failed.')
}
// use unifile as a middleware
app.use(silexConfig.apiRoot, unifile.middleware(express, app, silexConfig));
var port = process.env.PORT || 6805; // 6805 is the date of sexual revolution started in paris france 8-)
http.createServer(app).listen(port, function() {
console.log('listening on port ', port);
});
// SSL certificate
if(process.env.SILEX_SSL_PRIVATE_KEY && process.env.SILEX_SSL_CERTIFICATE) {
try {
var privateKey = fs.readFileSync(process.env.SILEX_SSL_PRIVATE_KEY).toString();
var certificate = fs.readFileSync(process.env.SILEX_SSL_CERTIFICATE).toString();
var options = {
key: privateKey,
cert: certificate,
requestCert: true,
rejectUnauthorized: false
};
var sslPort = process.env.SSL_PORT || 443;
https.createServer(options, app).listen(sslPort, function() {
console.log('listening on port ', sslPort);
});
}
catch(e) {
console.warn('SSL certificate failed.', e)
}
}
// ********************************
// silex tasks
// ********************************
var silexTasks = require('./silex-tasks.js');
app.use('/tasks/:task', function(req, res, next){
try{
silexTasks.route(function(result){
if (!result) {
result = {success: true};
}
try{
res.send(result);
}
catch(e){
console.error('Error: header have been sent?', e, result, e.stack);
}
}, req, res, next, req.params.task);
}
catch(e){
console.error('Error while executing task', e, e.stack);
}
});
// ********************************
// list templates
// ********************************
var dirToJson = require('dir-to-json');
app.use('/get/:folder', function(req, res, next){
switch(req.params.folder) {
case 'silex-templates':
case 'silex-blank-templates':
break;
default:
res.send({success: false, error: 'Error while trying to get the json representation of the folder ' + req.params.folder + ' - folder does not exist'});
return;
}
dirToJson( pathModule.resolve(__dirname, '../../dist/client/libs/templates/', req.params.folder), function( err, result ){
if( err ){
console.error('Error while trying to get the json representation of the folder ' + req.params.folder, err);
res.send({success: false, error: 'Error while trying to get the json representation of the folder ' + req.params.folder + ' - ' + err});
}else{
res.send(result);
}
});
});