2017-10-08 21:21:36 +00:00
|
|
|
const path = require('path')
|
2018-09-01 03:42:14 +00:00
|
|
|
const os = require('os')
|
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')
|
2018-09-02 02:37:20 +00:00
|
|
|
const fs = require('fs-extra')
|
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-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-08-16 01:57:28 +00:00
|
|
|
res.render('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
|
|
|
/**
|
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
|
|
|
|
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
|
|
|
|
2018-08-06 03:03:46 +00:00
|
|
|
// Create directory structure
|
2018-09-16 00:58:23 +00:00
|
|
|
const dataPath = path.join(process.cwd(), 'data')
|
|
|
|
await fs.ensureDir(dataPath)
|
|
|
|
await fs.ensureDir(path.join(dataPath, 'cache'))
|
|
|
|
await fs.ensureDir(path.join(dataPath, 'uploads'))
|
2018-08-06 03:03:46 +00:00
|
|
|
|
2018-05-28 18:46:55 +00:00
|
|
|
// Set config
|
2018-05-28 23:36:35 +00:00
|
|
|
_.set(WIKI.config, 'defaultEditor', 'markdown')
|
2018-05-28 18:46:55 +00:00
|
|
|
_.set(WIKI.config, 'graphEndpoint', 'https://graph.requarks.io')
|
2018-05-28 23:36:35 +00:00
|
|
|
_.set(WIKI.config, 'lang.code', 'en')
|
|
|
|
_.set(WIKI.config, 'lang.autoUpdate', true)
|
|
|
|
_.set(WIKI.config, 'lang.namespacing', false)
|
|
|
|
_.set(WIKI.config, 'lang.namespaces', [])
|
2018-08-27 02:38:08 +00:00
|
|
|
_.set(WIKI.config, 'public', false)
|
2018-05-28 18:46:55 +00:00
|
|
|
_.set(WIKI.config, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex'))
|
2018-05-28 23:36:35 +00:00
|
|
|
_.set(WIKI.config, 'telemetry.isEnabled', req.body.telemetry === 'true')
|
|
|
|
_.set(WIKI.config, 'telemetry.clientId', WIKI.telemetry.cid)
|
2018-06-11 03:23:09 +00:00
|
|
|
_.set(WIKI.config, 'theming.theme', 'default')
|
|
|
|
_.set(WIKI.config, 'theming.darkMode', false)
|
2018-08-27 02:38:08 +00:00
|
|
|
_.set(WIKI.config, 'title', 'Wiki.js')
|
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-28 23:36:35 +00:00
|
|
|
await WIKI.configSvc.saveToDb([
|
|
|
|
'defaultEditor',
|
|
|
|
'graphEndpoint',
|
|
|
|
'lang',
|
|
|
|
'public',
|
|
|
|
'sessionSecret',
|
|
|
|
'telemetry',
|
2018-06-11 03:23:09 +00:00
|
|
|
'theming',
|
2018-05-28 23:36:35 +00:00
|
|
|
'title'
|
2018-05-19 20:40:07 +00:00
|
|
|
])
|
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...')
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.locales.query().insert({
|
2018-05-20 22:50:51 +00:00
|
|
|
code: 'en',
|
|
|
|
strings: require('./locales/default.json'),
|
|
|
|
isRTL: false,
|
|
|
|
name: 'English',
|
|
|
|
nativeName: 'English'
|
|
|
|
})
|
|
|
|
|
2018-05-28 18:46:55 +00:00
|
|
|
// Load authentication strategies + enable local
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.authentication.refreshStrategiesFromDisk()
|
|
|
|
await WIKI.models.authentication.query().patch({ isEnabled: true }).where('key', 'local')
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
// Load editors + enable default
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.editors.refreshEditorsFromDisk()
|
|
|
|
await WIKI.models.editors.query().patch({ isEnabled: true }).where('key', 'markdown')
|
2018-05-28 18:46:55 +00:00
|
|
|
|
2018-09-01 19:15:44 +00:00
|
|
|
// Load loggers
|
|
|
|
await WIKI.models.loggers.refreshLoggersFromDisk()
|
|
|
|
|
2018-08-27 02:38:08 +00:00
|
|
|
// Load renderers
|
|
|
|
await WIKI.models.renderers.refreshRenderersFromDisk()
|
|
|
|
|
2018-09-01 19:15:44 +00:00
|
|
|
// Load search engines + enable default
|
|
|
|
await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
|
|
|
|
await WIKI.models.searchEngines.query().patch({ isEnabled: true }).where('key', 'db')
|
|
|
|
|
2018-06-26 02:04:47 +00:00
|
|
|
// Load storage targets
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.storage.refreshTargetsFromDisk()
|
2018-06-26 02:04:47 +00:00
|
|
|
|
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-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.users.query().delete().where({
|
2018-07-22 04:29:39 +00:00
|
|
|
providerKey: 'local',
|
2018-05-28 18:46:55 +00:00
|
|
|
email: req.body.adminEmail
|
|
|
|
})
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.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',
|
2018-05-28 18:46:55 +00:00
|
|
|
defaultEditor: 'markdown',
|
2017-12-17 04:41:16 +00:00
|
|
|
tfaIsActive: false
|
|
|
|
})
|
|
|
|
|
2018-05-19 20:40:07 +00:00
|
|
|
// Create Guest account
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.info('Creating guest account...')
|
2018-07-30 02:23:33 +00:00
|
|
|
const guestUsr = await WIKI.models.users.query().findOne({
|
2018-07-22 04:29:39 +00:00
|
|
|
providerKey: 'local',
|
2018-05-19 20:40:07 +00:00
|
|
|
email: 'guest@example.com'
|
|
|
|
})
|
|
|
|
if (!guestUsr) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.users.query().insert({
|
2018-05-19 20:40:07 +00:00
|
|
|
provider: 'local',
|
|
|
|
email: 'guest@example.com',
|
|
|
|
name: 'Guest',
|
|
|
|
password: '',
|
|
|
|
role: 'guest',
|
2018-05-20 22:50:51 +00:00
|
|
|
locale: 'en',
|
2018-05-28 18:46:55 +00:00
|
|
|
defaultEditor: 'markdown',
|
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-08-27 02:38:08 +00:00
|
|
|
redirectPath: '/',
|
2018-03-05 20:49:36 +00:00
|
|
|
redirectPort: WIKI.config.port
|
2018-01-15 03:05:08 +00:00
|
|
|
}).end()
|
|
|
|
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.config.setup = false
|
|
|
|
|
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-05-28 18:46:55 +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)
|
2018-09-01 03:42:14 +00:00
|
|
|
WIKI.server.listen(WIKI.config.port, WIKI.config.bindIP)
|
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', () => {
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.info('HTTP Server: [ RUNNING ]')
|
2018-09-29 19:39:09 +00:00
|
|
|
WIKI.logger.info('========================================')
|
|
|
|
WIKI.logger.info(`Browse to http://localhost:${WIKI.config.port}/`)
|
|
|
|
WIKI.logger.info('========================================')
|
2017-02-26 23:06:20 +00:00
|
|
|
})
|
|
|
|
}
|