2021-09-30 11:12:26 +02:00
|
|
|
const URL = require('url')
|
2021-10-18 15:46:38 +02:00
|
|
|
const helpers = require('../../helpers.js')
|
2021-09-27 11:12:14 +02:00
|
|
|
const log = require('../../log')
|
|
|
|
const db = require('../models/index.js')
|
|
|
|
const config = require('../../config')
|
2021-09-30 11:12:26 +02:00
|
|
|
const settingsController = require('./settings')
|
2021-10-21 16:18:28 +02:00
|
|
|
const path = require('path')
|
2022-02-07 12:28:38 +01:00
|
|
|
const escape = require('lodash/escape')
|
2021-09-27 11:12:14 +02:00
|
|
|
|
|
|
|
const setupController = {
|
|
|
|
|
2022-01-26 09:51:42 +01:00
|
|
|
async _setupDb (dbConf) {
|
|
|
|
|
2021-09-27 11:12:14 +02:00
|
|
|
if (!dbConf) {
|
2022-01-26 09:51:42 +01:00
|
|
|
throw Error('Empty DB configuration')
|
2021-09-27 11:12:14 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:51:42 +01:00
|
|
|
if (dbConf.dialect === 'sqlite' && dbConf.storage) {
|
2021-10-22 12:41:45 +02:00
|
|
|
dbConf.storage = path.resolve(process.env.cwd || '', dbConf.storage)
|
2022-01-26 09:51:42 +01:00
|
|
|
} else {
|
|
|
|
dbConf.storage = ''
|
2021-10-21 16:18:28 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:51:42 +01:00
|
|
|
// try to connect
|
|
|
|
dbConf.logging = false
|
|
|
|
await db.connect(dbConf)
|
|
|
|
|
|
|
|
// is empty ?
|
|
|
|
const isEmpty = await db.isEmpty()
|
|
|
|
if (!isEmpty) {
|
|
|
|
log.warn(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
|
|
|
|
throw Error(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.runMigrations()
|
|
|
|
|
|
|
|
config.db = dbConf
|
|
|
|
config.status = 'DBCONF'
|
|
|
|
config.db.logging = false
|
2021-11-08 11:02:14 +01:00
|
|
|
|
2022-01-26 09:51:42 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
async setupDb (req, res) {
|
|
|
|
log.debug('[SETUP] Check db')
|
|
|
|
const dbConf = req.body.db
|
|
|
|
|
|
|
|
try {
|
|
|
|
await setupController._setupDb(dbConf)
|
2021-09-27 11:12:14 +02:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(400).send(String(e))
|
|
|
|
}
|
2022-01-26 09:51:42 +01:00
|
|
|
|
|
|
|
return res.sendStatus(200)
|
2021-09-27 11:12:14 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async restart (req, res) {
|
|
|
|
|
2021-09-30 11:12:26 +02:00
|
|
|
try {
|
2021-09-27 11:12:14 +02:00
|
|
|
|
2022-01-26 09:51:42 +01:00
|
|
|
config.baseurl = req.protocol + '://' + req.headers.host
|
|
|
|
config.hostname = new URL.URL(config.baseurl).hostname
|
|
|
|
|
2021-09-30 11:12:26 +02:00
|
|
|
// write configuration
|
|
|
|
config.write()
|
2021-09-27 11:12:14 +02:00
|
|
|
|
2021-09-30 11:12:26 +02:00
|
|
|
// calculate default settings values
|
|
|
|
await settingsController.set('theme.is_dark', true)
|
|
|
|
await settingsController.set('instance_name', settingsController.settings.title.toLowerCase().replace(/ /g, ''))
|
|
|
|
// create admin
|
2021-10-18 15:46:38 +02:00
|
|
|
const password = helpers.randomString()
|
|
|
|
const email = `admin`
|
2021-09-30 11:12:26 +02:00
|
|
|
const User = require('../models/user')
|
|
|
|
await User.create({
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
is_admin: true,
|
|
|
|
is_active: true
|
|
|
|
})
|
|
|
|
|
|
|
|
res.json({ password, email })
|
|
|
|
log.info('Restart needed')
|
2021-10-29 15:24:20 +02:00
|
|
|
|
|
|
|
res.end()
|
2022-01-26 09:51:42 +01:00
|
|
|
|
2021-10-29 15:24:20 +02:00
|
|
|
// exit process so pm2 || docker could restart me || service
|
2022-01-26 09:51:42 +01:00
|
|
|
setTimeout(() => process.kill(process.pid), 1000)
|
2021-10-29 15:24:20 +02:00
|
|
|
|
2021-09-30 11:12:26 +02:00
|
|
|
} catch (e) {
|
2021-10-29 15:24:20 +02:00
|
|
|
log.error(String(e))
|
2022-02-07 12:28:38 +01:00
|
|
|
return res.status(400).send(escape(String(e)))
|
2021-09-30 11:12:26 +02:00
|
|
|
}
|
2021-09-27 11:12:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = setupController
|