2017-10-08 21:21:36 +00:00
|
|
|
const path = require('path')
|
2017-02-26 23:06:20 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-02-26 23:06:20 +00:00
|
|
|
|
2017-10-08 21:21:36 +00:00
|
|
|
module.exports = () => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.config.site = {
|
2017-10-21 22:01:37 +00:00
|
|
|
path: '',
|
2018-05-19 20:40:07 +00:00
|
|
|
title: 'Wiki.js'
|
2017-10-21 22:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.system = require('./core/system')
|
2017-11-19 22:08:23 +00:00
|
|
|
|
2017-02-26 23:06:20 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Load modules
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const compression = require('compression')
|
|
|
|
const express = require('express')
|
|
|
|
const favicon = require('serve-favicon')
|
|
|
|
const http = require('http')
|
2017-03-05 04:20:22 +00:00
|
|
|
const Promise = require('bluebird')
|
2017-11-21 04:08:30 +00:00
|
|
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
2017-03-25 21:03:09 +00:00
|
|
|
const yaml = require('js-yaml')
|
2017-03-05 04:20:22 +00:00
|
|
|
const _ = require('lodash')
|
2017-05-15 00:17:08 +00:00
|
|
|
const cfgHelper = require('./helpers/config')
|
2017-10-23 01:58:07 +00:00
|
|
|
const filesize = require('filesize.js')
|
2017-11-19 22:08:23 +00:00
|
|
|
const crypto = Promise.promisifyAll(require('crypto'))
|
2017-02-26 23:06:20 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-10-08 21:21:36 +00:00
|
|
|
let app = express()
|
2017-02-26 23:06:20 +00:00
|
|
|
app.use(compression())
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Public Assets
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.use(favicon(path.join(WIKI.ROOTPATH, 'assets', 'favicon.ico')))
|
|
|
|
app.use(express.static(path.join(WIKI.ROOTPATH, 'assets')))
|
2017-02-26 23:06:20 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View Engine Setup
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.set('views', path.join(WIKI.SERVERPATH, 'views'))
|
2017-02-26 23:06:20 +00:00
|
|
|
app.set('view engine', 'pug')
|
|
|
|
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.locals.config = WIKI.config
|
|
|
|
app.locals.data = WIKI.data
|
2017-02-26 23:06:20 +00:00
|
|
|
app.locals._ = require('lodash')
|
|
|
|
|
2018-01-28 02:40:51 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// HMR (Dev Mode Only)
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
if (global.DEV) {
|
2018-02-04 05:53:13 +00:00
|
|
|
app.use(global.WP_DEV.devMiddleware)
|
|
|
|
app.use(global.WP_DEV.hotMiddleware)
|
2018-01-28 02:40:51 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 23:06:20 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Controllers
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-11-19 22:08:23 +00:00
|
|
|
app.get('*', async (req, res) => {
|
2018-03-05 20:49:36 +00:00
|
|
|
let packageObj = await fs.readJson(path.join(WIKI.ROOTPATH, 'package.json'))
|
2018-02-04 05:53:13 +00:00
|
|
|
res.render('main/setup', {
|
2017-11-19 22:08:23 +00:00
|
|
|
packageObj,
|
2018-03-05 20:49:36 +00:00
|
|
|
telemetryClientID: WIKI.telemetry.cid
|
2017-10-09 04:16:08 +00:00
|
|
|
})
|
2017-02-26 23:06:20 +00:00
|
|
|
})
|
|
|
|
|
2017-03-25 21:03:09 +00:00
|
|
|
/**
|
|
|
|
* Perform basic system checks
|
|
|
|
*/
|
2017-03-05 04:20:22 +00:00
|
|
|
app.post('/syscheck', (req, res) => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.telemetry.enabled = (req.body.telemetry === true)
|
|
|
|
WIKI.telemetry.sendEvent('setup', 'start')
|
2017-10-23 01:58:07 +00:00
|
|
|
|
2017-03-05 04:20:22 +00:00
|
|
|
Promise.mapSeries([
|
|
|
|
() => {
|
|
|
|
const semver = require('semver')
|
2017-11-05 05:36:07 +00:00
|
|
|
if (!semver.satisfies(semver.clean(process.version), '>=8.9.0')) {
|
|
|
|
throw new Error('Node.js version is too old. Minimum is 8.9.0.')
|
2017-03-05 04:20:22 +00:00
|
|
|
}
|
2018-03-11 05:11:32 +00:00
|
|
|
return {
|
|
|
|
title: 'Node.js ' + process.version + ' detected.',
|
|
|
|
subtitle: ' Minimum is 8.9.0.'
|
|
|
|
}
|
2017-03-05 04:20:22 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return Promise.try(() => {
|
|
|
|
require('crypto')
|
2017-10-08 21:21:36 +00:00
|
|
|
}).catch(err => {
|
2017-03-05 04:20:22 +00:00
|
|
|
throw new Error('Crypto Node.js module is not available.')
|
2018-03-11 05:11:32 +00:00
|
|
|
}).return({
|
|
|
|
title: 'Node.js Crypto module is available.',
|
|
|
|
subtitle: 'Crypto module is required.'
|
|
|
|
})
|
2017-03-05 04:20:22 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
const exec = require('child_process').exec
|
|
|
|
const semver = require('semver')
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
exec('git --version', (err, stdout, stderr) => {
|
|
|
|
if (err || stdout.length < 3) {
|
|
|
|
reject(new Error('Git is not installed or not reachable from PATH.'))
|
|
|
|
}
|
2017-06-30 20:41:51 +00:00
|
|
|
let gitver = _.head(stdout.match(/[\d]+\.[\d]+(\.[\d]+)?/gi))
|
|
|
|
if (!gitver || !semver.satisfies(semver.clean(gitver), '>=2.7.4')) {
|
2017-10-08 21:21:36 +00:00
|
|
|
reject(new Error('Git version is too old. Minimum is 2.7.4.'))
|
2017-03-05 04:20:22 +00:00
|
|
|
}
|
2018-03-11 05:11:32 +00:00
|
|
|
resolve({
|
|
|
|
title: 'Git ' + gitver + ' detected.',
|
|
|
|
subtitle: 'Minimum is 2.7.4.'
|
|
|
|
})
|
2017-03-05 04:20:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2017-03-15 01:23:14 +00:00
|
|
|
() => {
|
|
|
|
const os = require('os')
|
2018-03-11 05:11:32 +00:00
|
|
|
if (os.totalmem() < 1000 * 1000 * 768) {
|
|
|
|
throw new Error('Not enough memory. Minimum is 768 MB.')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
title: filesize(os.totalmem()) + ' of system memory available.',
|
|
|
|
subtitle: 'Minimum is 768 MB.'
|
2017-03-15 01:23:14 +00:00
|
|
|
}
|
|
|
|
},
|
2017-03-05 04:20:22 +00:00
|
|
|
() => {
|
|
|
|
let fs = require('fs')
|
|
|
|
return Promise.try(() => {
|
2018-03-05 20:49:36 +00:00
|
|
|
fs.accessSync(path.join(WIKI.ROOTPATH, 'config.yml'), (fs.constants || fs).W_OK)
|
2017-10-08 21:21:36 +00:00
|
|
|
}).catch(err => {
|
2017-03-05 04:20:22 +00:00
|
|
|
throw new Error('config.yml file is not writable by Node.js process or was not created properly.')
|
2018-03-11 05:11:32 +00:00
|
|
|
}).return({
|
|
|
|
title: 'config.yml is writable by the setup process.',
|
|
|
|
subtitle: 'Setup will write to this file.'
|
|
|
|
})
|
2017-03-05 04:20:22 +00:00
|
|
|
}
|
2017-11-19 22:08:23 +00:00
|
|
|
], test => test()).then(results => {
|
2017-03-15 01:23:14 +00:00
|
|
|
res.json({ ok: true, results })
|
2017-03-05 04:20:22 +00:00
|
|
|
}).catch(err => {
|
|
|
|
res.json({ ok: false, error: err.message })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-03-25 21:03:09 +00:00
|
|
|
/**
|
|
|
|
* Check the Git connection
|
|
|
|
*/
|
|
|
|
app.post('/gitcheck', (req, res) => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.telemetry.sendEvent('setup', 'gitcheck')
|
2017-10-23 01:58:07 +00:00
|
|
|
|
2017-03-25 21:03:09 +00:00
|
|
|
const exec = require('execa')
|
2017-03-25 22:21:14 +00:00
|
|
|
const url = require('url')
|
2017-03-25 21:03:09 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
const dataDir = path.resolve(WIKI.ROOTPATH, cfgHelper.parseConfigValue(req.body.pathData))
|
|
|
|
const gitDir = path.resolve(WIKI.ROOTPATH, cfgHelper.parseConfigValue(req.body.pathRepo))
|
2017-03-25 22:21:14 +00:00
|
|
|
|
2017-03-26 03:17:05 +00:00
|
|
|
let gitRemoteUrl = ''
|
|
|
|
|
|
|
|
if (req.body.gitUseRemote === true) {
|
2017-05-15 00:17:08 +00:00
|
|
|
let urlObj = url.parse(cfgHelper.parseConfigValue(req.body.gitUrl))
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitAuthType === 'basic') {
|
|
|
|
urlObj.auth = req.body.gitAuthUser + ':' + req.body.gitAuthPass
|
|
|
|
}
|
|
|
|
gitRemoteUrl = url.format(urlObj)
|
2017-03-25 22:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Promise.mapSeries([
|
|
|
|
() => {
|
2017-11-19 22:08:23 +00:00
|
|
|
return fs.ensureDir(dataDir).then(() => 'Data directory path is valid.')
|
2017-03-25 22:21:14 +00:00
|
|
|
},
|
|
|
|
() => {
|
2017-11-19 22:08:23 +00:00
|
|
|
return fs.ensureDir(gitDir).then(() => 'Git directory path is valid.')
|
2017-03-25 22:21:14 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return exec.stdout('git', ['init'], { cwd: gitDir }).then(result => {
|
|
|
|
return 'Local git repository has been initialized.'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitUseRemote === false) { return false }
|
2017-04-29 22:34:00 +00:00
|
|
|
return exec.stdout('git', ['config', '--local', 'user.name', 'Wiki'], { cwd: gitDir }).then(result => {
|
2017-03-25 22:21:14 +00:00
|
|
|
return 'Git Signature Name has been set successfully.'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitUseRemote === false) { return false }
|
2017-04-29 22:34:00 +00:00
|
|
|
return exec.stdout('git', ['config', '--local', 'user.email', req.body.gitServerEmail], { cwd: gitDir }).then(result => {
|
2017-03-25 22:21:14 +00:00
|
|
|
return 'Git Signature Name has been set successfully.'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitUseRemote === false) { return false }
|
2017-03-25 22:21:14 +00:00
|
|
|
return exec.stdout('git', ['config', '--local', '--bool', 'http.sslVerify', req.body.gitAuthSSL], { cwd: gitDir }).then(result => {
|
|
|
|
return 'Git SSL Verify flag has been set successfully.'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitUseRemote === false) { return false }
|
2017-11-19 22:08:23 +00:00
|
|
|
if (_.includes(['sshenv', 'sshdb'], req.body.gitAuthType)) {
|
|
|
|
req.body.gitAuthSSHKey = path.join(dataDir, 'ssh/key.pem')
|
|
|
|
}
|
|
|
|
if (_.startsWith(req.body.gitAuthType, 'ssh')) {
|
2017-03-25 22:21:14 +00:00
|
|
|
return exec.stdout('git', ['config', '--local', 'core.sshCommand', 'ssh -i "' + req.body.gitAuthSSHKey + '" -o StrictHostKeyChecking=no'], { cwd: gitDir }).then(result => {
|
|
|
|
return 'Git SSH Private Key path has been set successfully.'
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitUseRemote === false) { return false }
|
2017-04-30 04:49:26 +00:00
|
|
|
return exec.stdout('git', ['remote', 'rm', 'origin'], { cwd: gitDir }).catch(err => {
|
|
|
|
if (_.includes(err.message, 'No such remote') || _.includes(err.message, 'Could not remove')) {
|
2017-03-25 22:21:14 +00:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
return exec.stdout('git', ['remote', 'add', 'origin', gitRemoteUrl], { cwd: gitDir }).then(result => {
|
|
|
|
return 'Git Remote was added successfully.'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
2017-03-26 03:17:05 +00:00
|
|
|
if (req.body.gitUseRemote === false) { return false }
|
2017-03-25 22:21:14 +00:00
|
|
|
return exec.stdout('git', ['pull', 'origin', req.body.gitBranch], { cwd: gitDir }).then(result => {
|
|
|
|
return 'Git Pull operation successful.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
], step => { return step() }).then(results => {
|
|
|
|
return res.json({ ok: true, results: _.without(results, false) })
|
2017-03-25 21:03:09 +00:00
|
|
|
}).catch(err => {
|
2017-03-25 22:21:14 +00:00
|
|
|
let errMsg = (err.stderr) ? err.stderr.replace(/(error:|warning:|fatal:)/gi, '').replace(/ \s+/g, ' ') : err.message
|
|
|
|
res.json({ ok: false, error: errMsg })
|
2017-03-25 21:03:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
2017-03-26 03:17:05 +00:00
|
|
|
* Finalize
|
2017-03-25 21:03:09 +00:00
|
|
|
*/
|
2017-11-19 22:08:23 +00:00
|
|
|
app.post('/finalize', async (req, res) => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.telemetry.sendEvent('setup', 'finalize')
|
2017-10-23 01:58:07 +00:00
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
console.error('DUDE')
|
|
|
|
|
2017-11-19 22:08:23 +00:00
|
|
|
try {
|
2018-03-05 20:49:36 +00:00
|
|
|
// Upgrade from WIKI.js 1.x?
|
2017-11-19 22:08:23 +00:00
|
|
|
if (req.body.upgrade) {
|
2018-03-05 20:49:36 +00:00
|
|
|
await WIKI.system.upgradeFromMongo({
|
2017-11-19 22:08:23 +00:00
|
|
|
mongoCnStr: cfgHelper.parseConfigValue(req.body.upgMongo)
|
2017-03-26 03:17:05 +00:00
|
|
|
})
|
2017-04-23 14:56:14 +00:00
|
|
|
}
|
2017-11-19 22:08:23 +00:00
|
|
|
|
2017-11-21 04:08:30 +00:00
|
|
|
// Update config file
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Writing config file to disk...')
|
|
|
|
let confRaw = await fs.readFileAsync(path.join(WIKI.ROOTPATH, 'config.yml'), 'utf8')
|
2017-11-19 22:08:23 +00:00
|
|
|
let conf = yaml.safeLoad(confRaw)
|
|
|
|
|
|
|
|
conf.port = req.body.port
|
2018-03-11 05:11:32 +00:00
|
|
|
conf.paths.data = req.body.pathData
|
|
|
|
conf.paths.content = req.body.pathContent
|
2017-11-19 22:08:23 +00:00
|
|
|
|
|
|
|
confRaw = yaml.safeDump(conf)
|
2018-03-05 20:49:36 +00:00
|
|
|
await fs.writeFileAsync(path.join(WIKI.ROOTPATH, 'config.yml'), confRaw)
|
2017-11-21 04:08:30 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
_.set(WIKI.config, 'port', req.body.port)
|
2018-03-11 05:11:32 +00:00
|
|
|
_.set(WIKI.config, 'paths.content', req.body.pathContent)
|
2018-01-15 03:05:08 +00:00
|
|
|
|
2017-11-21 04:08:30 +00:00
|
|
|
// Populate config namespaces
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.config.auth = WIKI.config.auth || {}
|
|
|
|
WIKI.config.features = WIKI.config.features || {}
|
|
|
|
WIKI.config.logging = WIKI.config.logging || {}
|
|
|
|
WIKI.config.site = WIKI.config.site || {}
|
|
|
|
WIKI.config.theme = WIKI.config.theme || {}
|
|
|
|
WIKI.config.uploads = WIKI.config.uploads || {}
|
2017-11-21 04:08:30 +00:00
|
|
|
|
|
|
|
// Site namespace
|
2018-03-05 20:49:36 +00:00
|
|
|
_.set(WIKI.config.site, 'title', req.body.title)
|
2018-04-29 21:55:36 +00:00
|
|
|
_.set(WIKI.config.site, 'lang', 'en')
|
2018-05-06 20:19:37 +00:00
|
|
|
_.set(WIKI.config.site, 'langAutoUpdate', true)
|
|
|
|
_.set(WIKI.config.site, 'rtl', false)
|
2018-03-05 20:49:36 +00:00
|
|
|
_.set(WIKI.config.site, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex'))
|
2017-11-21 04:08:30 +00:00
|
|
|
|
|
|
|
// Auth namespace
|
2018-03-05 20:49:36 +00:00
|
|
|
_.set(WIKI.config.auth, 'public', req.body.public === 'true')
|
2018-03-12 04:09:54 +00:00
|
|
|
_.set(WIKI.config.auth, 'strategies.local.isEnabled', true)
|
2018-03-05 20:49:36 +00:00
|
|
|
_.set(WIKI.config.auth, 'strategies.local.allowSelfRegister', req.body.selfRegister === 'true')
|
2017-12-17 04:41:16 +00:00
|
|
|
|
2017-11-21 04:08:30 +00:00
|
|
|
// Logging namespace
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.config.logging.telemetry = (req.body.telemetry === 'true')
|
2017-11-19 22:08:23 +00:00
|
|
|
|
2017-12-17 04:41:16 +00:00
|
|
|
// Save config to DB
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Persisting config to DB...')
|
2018-05-19 20:40:07 +00:00
|
|
|
await WIKI.db.settings.query().insert([
|
|
|
|
{ key: 'auth', value: WIKI.config.auth },
|
|
|
|
{ key: 'features', value: WIKI.config.features },
|
|
|
|
{ key: 'logging', value: WIKI.config.logging },
|
|
|
|
{ key: 'site', value: WIKI.config.site },
|
|
|
|
{ key: 'theme', value: WIKI.config.theme },
|
|
|
|
{ key: 'uploads', value: WIKI.config.uploads }
|
|
|
|
])
|
2017-12-17 04:41:16 +00:00
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
// Create default locale
|
|
|
|
WIKI.logger.info('Installing default locale...')
|
|
|
|
await WIKI.db.locales.query().insert({
|
|
|
|
code: 'en',
|
|
|
|
strings: require('./locales/default.json'),
|
|
|
|
isRTL: false,
|
|
|
|
name: 'English',
|
|
|
|
nativeName: 'English'
|
|
|
|
})
|
|
|
|
|
2017-12-17 04:41:16 +00:00
|
|
|
// Create root administrator
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Creating root administrator...')
|
2018-05-19 20:40:07 +00:00
|
|
|
await WIKI.db.users.query().insert({
|
2017-12-17 04:41:16 +00:00
|
|
|
email: req.body.adminEmail,
|
|
|
|
provider: 'local',
|
2018-05-19 20:40:07 +00:00
|
|
|
password: req.body.adminPassword,
|
2017-12-17 04:41:16 +00:00
|
|
|
name: 'Administrator',
|
|
|
|
role: 'admin',
|
2018-05-20 22:50:51 +00:00
|
|
|
locale: 'en',
|
2017-12-17 04:41:16 +00:00
|
|
|
tfaIsActive: false
|
|
|
|
})
|
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
// Create Guest account
|
|
|
|
WIKI.logger.info('Creating root administrator...')
|
|
|
|
const guestUsr = await WIKI.db.users.query().findOne({
|
|
|
|
provider: 'local',
|
|
|
|
email: 'guest@example.com'
|
|
|
|
})
|
|
|
|
if (!guestUsr) {
|
|
|
|
await WIKI.db.users.query().insert({
|
|
|
|
provider: 'local',
|
|
|
|
email: 'guest@example.com',
|
|
|
|
name: 'Guest',
|
|
|
|
password: '',
|
|
|
|
role: 'guest',
|
2018-05-20 22:50:51 +00:00
|
|
|
locale: 'en',
|
2018-05-19 20:40:07 +00:00
|
|
|
tfaIsActive: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Setup is complete!')
|
2018-01-15 03:05:08 +00:00
|
|
|
res.json({
|
|
|
|
ok: true,
|
2018-03-05 20:49:36 +00:00
|
|
|
redirectPath: WIKI.config.site.path,
|
|
|
|
redirectPort: WIKI.config.port
|
2018-01-15 03:05:08 +00:00
|
|
|
}).end()
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Stopping Setup...')
|
2018-03-11 05:11:32 +00:00
|
|
|
WIKI.server.destroy(() => {
|
2018-05-19 20:40:07 +00:00
|
|
|
WIKI.logger.info('Setup stopped. Starting Wiki.js...')
|
2018-01-15 03:05:08 +00:00
|
|
|
_.delay(() => {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.kernel.bootMaster()
|
2018-01-15 03:05:08 +00:00
|
|
|
}, 1000)
|
|
|
|
})
|
2017-11-19 22:08:23 +00:00
|
|
|
} catch (err) {
|
2017-03-26 03:17:05 +00:00
|
|
|
res.json({ ok: false, error: err.message })
|
2017-11-19 22:08:23 +00:00
|
|
|
}
|
2017-03-25 21:03:09 +00:00
|
|
|
})
|
|
|
|
|
2017-02-26 23:06:20 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Error handling
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(function (req, res, next) {
|
|
|
|
var err = new Error('Not Found')
|
|
|
|
err.status = 404
|
|
|
|
next(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
res.status(err.status || 500)
|
|
|
|
res.send({
|
|
|
|
message: err.message,
|
2018-03-05 20:49:36 +00:00
|
|
|
error: WIKI.IS_DEBUG ? err : {}
|
2017-02-26 23:06:20 +00:00
|
|
|
})
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error(err.message)
|
|
|
|
WIKI.telemetry.sendError(err)
|
2017-02-26 23:06:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Start HTTP server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info(`HTTP Server on port: ${WIKI.config.port}`)
|
2017-02-26 23:06:20 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
app.set('port', WIKI.config.port)
|
|
|
|
WIKI.server = http.createServer(app)
|
|
|
|
WIKI.server.listen(WIKI.config.port)
|
2017-03-29 02:29:53 +00:00
|
|
|
|
|
|
|
var openConnections = []
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.on('connection', (conn) => {
|
2017-03-29 02:29:53 +00:00
|
|
|
let key = conn.remoteAddress + ':' + conn.remotePort
|
|
|
|
openConnections[key] = conn
|
|
|
|
conn.on('close', () => {
|
|
|
|
delete openConnections[key]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.destroy = (cb) => {
|
|
|
|
WIKI.server.close(cb)
|
2017-03-29 02:29:53 +00:00
|
|
|
for (let key in openConnections) {
|
|
|
|
openConnections[key].destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.on('error', (error) => {
|
2017-02-26 23:06:20 +00:00
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error('Listening on port ' + WIKI.config.port + ' requires elevated privileges!')
|
2017-04-05 02:04:17 +00:00
|
|
|
return process.exit(1)
|
2017-02-26 23:06:20 +00:00
|
|
|
case 'EADDRINUSE':
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error('Port ' + WIKI.config.port + ' is already in use!')
|
2017-04-05 02:04:17 +00:00
|
|
|
return process.exit(1)
|
2017-02-26 23:06:20 +00:00
|
|
|
default:
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.server.on('listening', () => {
|
|
|
|
WIKI.logger.info('HTTP Server: RUNNING')
|
2017-02-26 23:06:20 +00:00
|
|
|
})
|
|
|
|
}
|