forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
160 lines (132 loc) · 3.87 KB
/
web.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
'use strict';
var winston = require('winston');
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var path = require('path');
var less = require('less');
var async = require('async');
var uglify = require('uglify-js');
var nconf = require('nconf');
var app = express();
var server;
winston.add(winston.transports.File, {
filename: 'logs/webinstall.log',
colorize: true,
timestamp: function () {
var date = new Date();
return date.getDate() + '/' + (date.getMonth() + 1) + ' ' + date.toTimeString().substr(0, 5) + ' [' + global.process.pid + ']';
},
level: 'verbose',
});
var web = {};
var scripts = [
'public/vendor/xregexp/xregexp.js',
'public/vendor/xregexp/unicode/unicode-base.js',
'public/src/utils.js',
'public/src/installer/install.js',
];
web.install = function (port) {
port = port || 4567;
winston.info('Launching web installer on port', port);
app.use(express.static('public', {}));
app.engine('tpl', require('templates.js').__express);
app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, '../src/views'));
app.use(bodyParser.urlencoded({
extended: true,
}));
async.parallel([compileLess, compileJS], function () {
setupRoutes();
launchExpress(port);
});
};
function launchExpress(port) {
server = app.listen(port, function () {
winston.info('Web installer listening on http://%s:%s', '0.0.0.0', port);
});
}
function setupRoutes() {
app.get('/', welcome);
app.post('/', install);
app.post('/launch', launch);
}
function welcome(req, res) {
var dbs = ['redis', 'mongo'];
var databases = dbs.map(function (el) {
return {
name: el,
questions: require('../src/database/' + el).questions,
};
});
var defaults = require('./data/defaults');
res.render('install/index', {
databases: databases,
skipDatabaseSetup: !!nconf.get('database'),
error: !!res.locals.error,
success: !!res.locals.success,
values: req.body,
minimumPasswordLength: defaults.minimumPasswordLength,
});
}
function install(req, res) {
for (var i in req.body) {
if (req.body.hasOwnProperty(i) && !process.env.hasOwnProperty(i)) {
process.env[i.replace(':', '__')] = req.body[i];
}
}
var child = require('child_process').fork('app', ['--setup'], {
env: process.env,
});
child.on('close', function (data) {
if (data === 0) {
res.locals.success = true;
} else {
res.locals.error = true;
}
welcome(req, res);
});
}
function launch(req, res) {
res.json({});
server.close();
var child = require('child_process').spawn('node', ['loader.js'], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});
process.stdout.write('\nStarting NodeBB\n');
process.stdout.write(' "./nodebb stop" to stop the NodeBB server\n');
process.stdout.write(' "./nodebb log" to view server output\n');
process.stdout.write(' "./nodebb restart" to restart NodeBB\n');
async.parallel([
async.apply(fs.unlink(path.join(__dirname, '../public/installer.css'))),
async.apply(fs.unlink(path.join(__dirname, '../public/installer.min.js'))),
], function (err) {
if (err) {
winston.warn('Unable to remove installer files');
}
child.unref();
process.exit(0);
});
}
function compileLess(callback) {
fs.readFile(path.join(__dirname, '../public/less/install.less'), function (err, style) {
if (err) {
return winston.error('Unable to read LESS install file: ', err);
}
less.render(style.toString(), function (err, css) {
if (err) {
return winston.error('Unable to compile LESS: ', err);
}
fs.writeFile(path.join(__dirname, '../public/installer.css'), css.css, callback);
});
});
}
function compileJS(callback) {
var scriptPath = path.join(__dirname, '..');
var result = uglify.minify(scripts.map(function (script) {
return path.join(scriptPath, script);
}));
fs.writeFile(path.join(__dirname, '../public/installer.min.js'), result.code, callback);
}
module.exports = web;