add again

isekai-main
Barış Soner Uşaklı 2 years ago
parent a7375a85ba
commit 9198a95173

@ -6,7 +6,6 @@
body { body {
background: #00A9EA; background: #00A9EA;
color: white; color: white;
/* see public/less/admin/vars.less for documentation on system font family */
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Helvetica, Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
text-align: center; text-align: center;
-webkit-transform-style: preserve-3d; -webkit-transform-style: preserve-3d;

@ -1,5 +1,3 @@
@import "./admin/vars";
.hidden, .hide { .hidden, .hide {
display: none!important; display: none!important;
} }
@ -49,19 +47,11 @@
} }
} }
.btn, .form-control, .navbar {
border-radius: 0;
}
.container { .container {
font-size: 18px; font-size: 18px;
margin-bottom: 100px; margin-bottom: 100px;
} }
body, small, p, div {
font-family: $font-family-sans-serif;
}
.input-row { .input-row {
margin-bottom: 20px; margin-bottom: 20px;
@ -69,14 +59,6 @@ body, small, p, div {
margin-bottom: 5px; margin-bottom: 5px;
} }
.help-text {
pointer-events: none;
line-height: 20px;
color: #888;
font-size: 85%;
display: none;
}
.input-field { .input-field {
border-right: 5px solid #FFF; border-right: 5px solid #FFF;
} }

@ -14,12 +14,38 @@ $('document').ready(function () {
activate('database', $('[name="database"]')); activate('database', $('[name="database"]'));
if ($('#database-error').length) { $('#test-database').on('click', function () {
$('[name="database"]').parents('.input-row').addClass('error'); const conf = {};
$('html, body').animate({ $('#database-config input[name]').each((i, el) => {
scrollTop: ($('#database-error').offset().top + 100) + 'px', conf[$(el).attr('name')] = $(el).val();
}, 400); });
} $('#test-database-spinner').removeClass('hidden');
$('#database-success').addClass('hidden');
$('#database-error').addClass('hidden');
$('#database-full').addClass('hidden');
const qs = new URLSearchParams(conf).toString();
$.ajax({
url: `/testdb?${qs}`,
success: function (res) {
$('#test-database-spinner').addClass('hidden');
if (res.success) {
$('#database-success').removeClass('hidden');
if (res.dbfull) {
$('#database-full').removeClass('hidden')
.text('Found existing install in this database!');
}
} else if (res.error) {
$('#database-error').removeClass('hidden').text(res.error);
}
},
error: function (jqXHR, textStatus) {
$('#test-database-spinner').addClass('hidden');
$('#database-error').removeClass('hidden').text(textStatus);
},
});
return false;
});
function checkIfReady() { function checkIfReady() {
let successCount = 0; let successCount = 0;
@ -127,6 +153,9 @@ $('document').ready(function () {
function switchDatabase(field) { function switchDatabase(field) {
$('#database-config').html($('[data-database="' + field + '"]').html()); $('#database-config').html($('[data-database="' + field + '"]').html());
$('#database-success').addClass('hidden');
$('#database-error').addClass('hidden');
$('#database-full').addClass('hidden');
} }
switch (type) { switch (type) {

@ -59,8 +59,8 @@ mongoModule.questions = [
}, },
]; ];
mongoModule.init = async function () { mongoModule.init = async function (opts) {
client = await connection.connect(nconf.get('mongo')); client = await connection.connect(opts || nconf.get('mongo'));
mongoModule.client = client.db(); mongoModule.client = client.db();
}; };
@ -175,6 +175,9 @@ async function getCollectionStats(db) {
mongoModule.close = async function () { mongoModule.close = async function () {
await client.close(); await client.close();
if (mongoModule.objectCache) {
mongoModule.objectCache.reset();
}
}; };
require('./mongo/main')(mongoModule); require('./mongo/main')(mongoModule);

@ -45,9 +45,9 @@ postgresModule.questions = [
}, },
]; ];
postgresModule.init = async function () { postgresModule.init = async function (opts) {
const { Pool } = require('pg'); const { Pool } = require('pg');
const connOptions = connection.getConnectionOptions(); const connOptions = connection.getConnectionOptions(opts);
const pool = new Pool(connOptions); const pool = new Pool(connOptions);
postgresModule.pool = pool; postgresModule.pool = pool;
postgresModule.client = pool; postgresModule.client = pool;

@ -33,8 +33,8 @@ redisModule.questions = [
]; ];
redisModule.init = async function () { redisModule.init = async function (opts) {
redisModule.client = await connection.connect(nconf.get('redis')); redisModule.client = await connection.connect(opts || nconf.get('redis'));
}; };
redisModule.createSessionStore = async function (options) { redisModule.createSessionStore = async function (options) {
@ -62,6 +62,9 @@ redisModule.checkCompatibilityVersion = function (version, callback) {
redisModule.close = async function () { redisModule.close = async function () {
await redisModule.client.quit(); await redisModule.client.quit();
if (redisModule.objectCache) {
redisModule.objectCache.reset();
}
}; };
redisModule.info = async function (cxn) { redisModule.info = async function (cxn) {

@ -87,12 +87,10 @@
</div> </div>
</div> </div>
{{{ if error }}}
<a id="database-error"></a>
{{{ end }}}
{{{ if !skipDatabaseSetup }}} {{{ if !skipDatabaseSetup }}}
<div class="database"> <div class="database mb-3">
<p> <p>
<h2><small>Configure your database</small></h2> <h2><small>Configure your database</small></h2>
<hr /> <hr />
@ -113,8 +111,22 @@
<div id="database-config"></div> <div id="database-config"></div>
</div> </div>
{{{ end }}} {{{ end }}}
<div class="row">
<button id="submit" type="submit" class="btn btn btn-success">Install NodeBB <i class="working hide"></i></button> <div class="col-sm-7 col-12">
<div class="d-flex gap-2 mb-3">
{{{ if !skipDatabaseSetup }}}
<button id="test-database" class="btn btn-light">
<div id="test-database-spinner" class="spinner-border spinner-border-sm text-primary hidden" role="status"></div>
<span>Test Database</span>
</button>
{{{ end }}}
<button id="submit" type="submit" class="btn btn-primary">Install NodeBB <i class="working hide"></i></button>
</div>
<div id="database-success" class="alert alert-success hidden" role="alert">Database connection successful!</div>
<div id="database-error" class="alert alert-danger hidden" role="alert"></div>
<div id="database-full" class="alert alert-warning hidden" role="alert"><pre></pre></div>
</div>
</div>
</form> </form>
</div> </div>
{{{ end }}} {{{ end }}}

317
web.js

@ -0,0 +1,317 @@
'use strict';
const winston = require('winston');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const webpack = require('webpack');
const nconf = require('nconf');
const Benchpress = require('benchpressjs');
const { mkdirp } = require('mkdirp');
const { paths } = require('../src/constants');
const sass = require('../src/utils').getSass();
const app = express();
let server;
const formats = [
winston.format.colorize(),
];
const timestampFormat = winston.format((info) => {
const dateString = `${new Date().toISOString()} [${global.process.pid}]`;
info.level = `${dateString} - ${info.level}`;
return info;
});
formats.push(timestampFormat());
formats.push(winston.format.splat());
formats.push(winston.format.simple());
winston.configure({
level: 'verbose',
format: winston.format.combine.apply(null, formats),
transports: [
new winston.transports.Console({
handleExceptions: true,
}),
new winston.transports.File({
filename: 'logs/webinstall.log',
handleExceptions: true,
}),
],
});
const web = module.exports;
let installing = false;
let success = false;
let error = false;
let launchUrl;
let timeStart = 0;
const totalTime = 1000 * 60 * 3;
const viewsDir = path.join(paths.baseDir, 'build/public/templates');
web.install = async function (port) {
port = port || 4567;
winston.info(`Launching web installer on port ${port}`);
app.use(express.static('public', {}));
app.use('/assets', express.static(path.join(__dirname, '../build/public'), {}));
app.engine('tpl', (filepath, options, callback) => {
filepath = filepath.replace(/\.tpl$/, '.js');
Benchpress.__express(filepath, options, callback);
});
app.set('view engine', 'tpl');
app.set('views', viewsDir);
app.use(bodyParser.urlencoded({
extended: true,
}));
try {
await Promise.all([
compileTemplate(),
compileSass(),
runWebpack(),
copyCSS(),
loadDefaults(),
]);
setupRoutes();
launchExpress(port);
} catch (err) {
winston.error(err.stack);
}
};
async function runWebpack() {
const util = require('util');
const webpackCfg = require('../webpack.installer');
const compiler = webpack(webpackCfg);
const webpackRun = util.promisify(compiler.run).bind(compiler);
await webpackRun();
}
function launchExpress(port) {
server = app.listen(port, () => {
winston.info('Web installer listening on http://%s:%s', '0.0.0.0', port);
});
}
function setupRoutes() {
app.get('/', welcome);
app.post('/', install);
app.get('/testdb', testDatabase);
app.get('/ping', ping);
app.get('/sping', ping);
}
async function testDatabase(req, res) {
let db;
try {
const keys = Object.keys(req.query);
const dbName = keys[0].split(':')[0];
db = require(`../src/database/${dbName}`);
const opts = {};
keys.forEach((key) => {
opts[key.replace(`${dbName}:`, '')] = req.query[key];
});
await db.init(opts);
const global = await db.getObject('global');
await db.close();
res.json({ success: 1, dbfull: !!global });
} catch (err) {
res.json({ error: err.stack });
}
}
function ping(req, res) {
res.status(200).send(req.path === '/sping' ? 'healthy' : '200');
}
function welcome(req, res) {
const dbs = ['mongo', 'redis', 'postgres'];
const databases = dbs.map((databaseName) => {
const questions = require(`../src/database/${databaseName}`).questions.filter(question => question && !question.hideOnWebInstall);
return {
name: databaseName,
questions: questions,
};
});
const defaults = require('./data/defaults.json');
res.render('install/index', {
url: nconf.get('url') || (`${req.protocol}://${req.get('host')}`),
launchUrl: launchUrl,
skipGeneralSetup: !!nconf.get('url'),
databases: databases,
skipDatabaseSetup: !!nconf.get('database'),
error: error,
success: success,
values: req.body,
minimumPasswordLength: defaults.minimumPasswordLength,
minimumPasswordStrength: defaults.minimumPasswordStrength,
installing: installing,
percentInstalled: installing ? ((Date.now() - timeStart) / totalTime * 100).toFixed(2) : 0,
});
}
function install(req, res) {
if (installing) {
return welcome(req, res);
}
timeStart = Date.now();
req.setTimeout(0);
installing = true;
const database = nconf.get('database') || req.body.database || 'mongo';
const setupEnvVars = {
...process.env,
NODEBB_URL: nconf.get('url') || req.body.url || (`${req.protocol}://${req.get('host')}`),
NODEBB_PORT: nconf.get('port') || 4567,
NODEBB_ADMIN_USERNAME: nconf.get('admin:username') || req.body['admin:username'],
NODEBB_ADMIN_PASSWORD: nconf.get('admin:password') || req.body['admin:password'],
NODEBB_ADMIN_EMAIL: nconf.get('admin:email') || req.body['admin:email'],
NODEBB_DB: database,
NODEBB_DB_HOST: nconf.get(`${database}:host`) || req.body[`${database}:host`],
NODEBB_DB_PORT: nconf.get(`${database}:port`) || req.body[`${database}:port`],
NODEBB_DB_USER: nconf.get(`${database}:username`) || req.body[`${database}:username`],
NODEBB_DB_PASSWORD: nconf.get(`${database}:password`) || req.body[`${database}:password`],
NODEBB_DB_NAME: nconf.get(`${database}:database`) || req.body[`${database}:database`],
NODEBB_DB_SSL: nconf.get(`${database}:ssl`) || req.body[`${database}:ssl`],
defaultPlugins: JSON.stringify(nconf.get('defaultplugins') || nconf.get('defaultPlugins') || []),
};
winston.info('Starting setup process');
launchUrl = setupEnvVars.NODEBB_URL;
const child = require('child_process').fork('app', ['--setup'], {
env: setupEnvVars,
});
child.on('error', (err) => {
error = true;
success = false;
winston.error(err.stack);
});
child.on('close', (data) => {
success = data === 0;
error = data !== 0;
launch();
});
welcome(req, res);
}
async function launch() {
try {
server.close();
let child;
if (!nconf.get('launchCmd')) {
child = childProcess.spawn('node', ['loader.js'], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});
console.log('\nStarting NodeBB');
console.log(' "./nodebb stop" to stop the NodeBB server');
console.log(' "./nodebb log" to view server output');
console.log(' "./nodebb restart" to restart NodeBB');
} else {
// Use launchCmd instead, if specified
child = childProcess.exec(nconf.get('launchCmd'), {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});
}
const filesToDelete = [
path.join(__dirname, '../public', 'installer.css'),
path.join(__dirname, '../public', 'bootstrap.min.css'),
path.join(__dirname, '../build/public', 'installer.min.js'),
];
try {
await Promise.all(
filesToDelete.map(
filename => fs.promises.unlink(filename)
)
);
} catch (err) {
console.log(err.stack);
}
child.unref();
process.exit(0);
} catch (err) {
winston.error(err.stack);
throw err;
}
}
// this is necessary because otherwise the compiled templates won't be available on a clean install
async function compileTemplate() {
const sourceFile = path.join(__dirname, '../src/views/install/index.tpl');
const destTpl = path.join(viewsDir, 'install/index.tpl');
const destJs = path.join(viewsDir, 'install/index.js');
const source = await fs.promises.readFile(sourceFile, 'utf8');
const [compiled] = await Promise.all([
Benchpress.precompile(source, { filename: 'install/index.tpl' }),
mkdirp(path.dirname(destJs)),
]);
await Promise.all([
fs.promises.writeFile(destJs, compiled),
fs.promises.writeFile(destTpl, source),
]);
}
async function compileSass() {
try {
const installSrc = path.join(__dirname, '../public/scss/install.scss');
const style = await fs.promises.readFile(installSrc);
const scssOutput = sass.compileString(String(style), {
loadPaths: [
path.join(__dirname, '../public/scss'),
],
});
await fs.promises.writeFile(path.join(__dirname, '../public/installer.css'), scssOutput.css.toString());
} catch (err) {
winston.error(`Unable to compile SASS: \n${err.stack}`);
throw err;
}
}
async function copyCSS() {
await fs.promises.copyFile(
path.join(__dirname, '../node_modules/bootstrap/dist/css/bootstrap.min.css'),
path.join(__dirname, '../public/bootstrap.min.css'),
);
}
async function loadDefaults() {
const setupDefaultsPath = path.join(__dirname, '../setup.json');
try {
// eslint-disable-next-line no-bitwise
await fs.promises.access(setupDefaultsPath, fs.constants.F_OK | fs.constants.R_OK);
} catch (err) {
// setup.json not found or inaccessible, proceed with no defaults
if (err.code !== 'ENOENT') {
throw err;
}
return;
}
winston.info('[installer] Found setup.json, populating default values');
nconf.file({
file: setupDefaultsPath,
});
}
Loading…
Cancel
Save