improve setup and DB initialization

This commit is contained in:
lesion
2022-01-26 09:51:42 +01:00
parent 5a3ff7564d
commit 55c066b845
13 changed files with 96 additions and 57 deletions

View File

@@ -54,7 +54,7 @@ const settingsController = {
secretSettings: {},
async load () {
if (config.firstrun) {
if (config.status !== 'READY') {
settingsController.settings = defaultSettings
return
}

View File

@@ -8,49 +8,59 @@ const path = require('path')
const setupController = {
async setupDb (req, res, next) {
async _setupDb (dbConf) {
if (!dbConf) {
throw Error('Empty DB configuration')
}
if (dbConf.dialect === 'sqlite' && dbConf.storage) {
dbConf.storage = path.resolve(process.env.cwd || '', dbConf.storage)
} else {
dbConf.storage = ''
}
// 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
const settingsController = require('./settings')
await settingsController.load()
},
async setupDb (req, res) {
log.debug('[SETUP] Check db')
const dbConf = req.body.db
if (!dbConf) {
return res.sendStatus(400)
}
if (dbConf.storage) {
dbConf.storage = path.resolve(process.env.cwd || '', dbConf.storage)
}
try {
// 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.')
return res.status(400).send(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
}
await db.runMigrations()
config.db = dbConf
config.firstrun = false
config.db.logging = false
config.baseurl = req.protocol + '://' + req.headers.host
config.hostname = new URL.URL(config.baseurl).hostname
const settingsController = require('./settings')
await settingsController.load()
return res.sendStatus(200)
await setupController._setupDb(dbConf)
} catch (e) {
return res.status(400).send(String(e))
}
return res.sendStatus(200)
},
async restart (req, res) {
try {
config.baseurl = req.protocol + '://' + req.headers.host
config.hostname = new URL.URL(config.baseurl).hostname
// write configuration
config.write()
@@ -72,8 +82,9 @@ const setupController = {
log.info('Restart needed')
res.end()
// exit process so pm2 || docker could restart me || service
process.kill(process.pid)
setTimeout(() => process.kill(process.pid), 1000)
} catch (e) {
log.error(String(e))