♻️ 重构本地数据库-notify模块
This commit is contained in:
parent
dafb2cc5c9
commit
98d44e8ab4
@ -1,9 +1,11 @@
|
|||||||
const { readNotifyConfig, writeNotifyConfig, readNotifyList, writeNotifyList } = require('../utils/storage')
|
|
||||||
const { sendServerChan, sendEmail } = require('../utils/notify')
|
const { sendServerChan, sendEmail } = require('../utils/notify')
|
||||||
// const commonTemp = require('../template/commonTemp')
|
const { NotifyConfigDB, NotifyDB } = require('../utils/db-class')
|
||||||
|
const notifyDB = new NotifyDB().getInstance()
|
||||||
|
const notifyConfigDB = new NotifyConfigDB().getInstance()
|
||||||
|
|
||||||
async function getNotifyConfig({ res }) {
|
async function getNotifyConfig({ res }) {
|
||||||
const data = await readNotifyConfig()
|
const data = await notifyConfigDB.findOneAsync({})
|
||||||
|
console.log(data)
|
||||||
return res.success({ data })
|
return res.success({ data })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11,7 +13,7 @@ async function updateNotifyConfig({ res, request }) {
|
|||||||
let { body: { noticeConfig } } = request
|
let { body: { noticeConfig } } = request
|
||||||
let { type } = noticeConfig
|
let { type } = noticeConfig
|
||||||
try {
|
try {
|
||||||
switch(type) {
|
switch (type) {
|
||||||
case 'sct':
|
case 'sct':
|
||||||
await sendServerChan(noticeConfig[type]['sendKey'], 'EasyNode通知测试', '这是一条测试通知')
|
await sendServerChan(noticeConfig[type]['sendKey'], 'EasyNode通知测试', '这是一条测试通知')
|
||||||
break
|
break
|
||||||
@ -19,7 +21,7 @@ async function updateNotifyConfig({ res, request }) {
|
|||||||
await sendEmail(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知')
|
await sendEmail(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知')
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
await writeNotifyConfig(noticeConfig)
|
await notifyConfigDB.update({}, { $set: noticeConfig }, { upsert: true })
|
||||||
return res.success({ msg: '测试通过 | 保存成功' })
|
return res.success({ msg: '测试通过 | 保存成功' })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.fail({ msg: error.message })
|
return res.fail({ msg: error.message })
|
||||||
@ -27,18 +29,14 @@ async function updateNotifyConfig({ res, request }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getNotifyList({ res }) {
|
async function getNotifyList({ res }) {
|
||||||
const data = await readNotifyList()
|
const data = await notifyDB.findAsync({})
|
||||||
res.success({ data })
|
res.success({ data })
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateNotifyList({ res, request }) {
|
async function updateNotifyList({ res, request }) {
|
||||||
let { body: { type, sw } } = request
|
let { body: { type, sw } } = request
|
||||||
if (!([true, false].includes(sw))) return res.fail({ msg: `Error type for sw:${ sw }, must be Boolean` })
|
if (!([true, false].includes(sw))) return res.fail({ msg: `Error type for sw:${ sw }, must be Boolean` })
|
||||||
const notifyList = await readNotifyList()
|
await notifyDB.updateAsync({ type }, { $set: { sw } })
|
||||||
let target = notifyList.find((item) => item.type === type)
|
|
||||||
if (!target) return res.fail({ msg: `更新失败, 不存在该通知类型:${ type }` })
|
|
||||||
target.sw = sw
|
|
||||||
await writeNotifyList(notifyList)
|
|
||||||
res.success()
|
res.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
125
server/app/db.js
125
server/app/db.js
@ -1,4 +1,4 @@
|
|||||||
const { writeKey, writeNotifyList, writeNotifyConfig } = require('./utils/storage')
|
const { writeKey } = require('./utils/storage')
|
||||||
const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB } = require('./utils/db-class')
|
const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB } = require('./utils/db-class')
|
||||||
|
|
||||||
function initKeyDB() {
|
function initKeyDB() {
|
||||||
@ -37,83 +37,56 @@ async function initGroupDB() {
|
|||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
function initNotifyDB() {
|
async function initNotifyDB() {
|
||||||
return new Promise((resolve, reject) => {
|
const notifyDB = new NotifyDB().getInstance()
|
||||||
const notifyDB = new NotifyDB().getInstance()
|
let count = await notifyDB.countAsync({})
|
||||||
notifyDB.find({}, async (err, notifyList) => {
|
if (count !== 0) return
|
||||||
if (err) {
|
consola.log('初始化notifyDB✔')
|
||||||
consola.log('初始化notifyDB错误:', err)
|
let defaultData = [{
|
||||||
reject(err)
|
'type': 'login',
|
||||||
} else {
|
'desc': '登录面板提醒',
|
||||||
let defaultData = [{
|
'sw': false
|
||||||
'type': 'login',
|
}, {
|
||||||
'desc': '登录面板提醒',
|
'type': 'err_login',
|
||||||
'sw': false
|
'desc': '登录错误提醒(连续5次)',
|
||||||
}, {
|
'sw': false
|
||||||
'type': 'err_login',
|
}, {
|
||||||
'desc': '登录错误提醒(连续5次)',
|
'type': 'updatePwd',
|
||||||
'sw': false
|
'desc': '修改密码提醒',
|
||||||
}, {
|
'sw': false
|
||||||
'type': 'updatePwd',
|
}, {
|
||||||
'desc': '修改密码提醒',
|
'type': 'host_login',
|
||||||
'sw': false
|
'desc': '服务器登录提醒',
|
||||||
}, {
|
'sw': false
|
||||||
'type': 'host_login',
|
}, {
|
||||||
'desc': '服务器登录提醒',
|
'type': 'onekey_complete',
|
||||||
'sw': false
|
'desc': '批量指令执行完成提醒',
|
||||||
}, {
|
'sw': false
|
||||||
'type': 'onekey_complete',
|
}, {
|
||||||
'desc': '批量指令执行完成提醒',
|
'type': 'host_expired',
|
||||||
'sw': false
|
'desc': '服务器到期提醒',
|
||||||
}, {
|
'sw': false
|
||||||
'type': 'host_expired',
|
}]
|
||||||
'desc': '服务器到期提醒',
|
return notifyDB.insertAsync(defaultData)
|
||||||
'sw': false
|
|
||||||
}]
|
|
||||||
if (notifyList.length === 0) {
|
|
||||||
consola.log('初始化notifyDB✔')
|
|
||||||
} else {
|
|
||||||
consola.log('同步notifyDB✔')
|
|
||||||
defaultData = defaultData.map(defaultItem => {
|
|
||||||
let item = notifyList.find(notify => notify.type === defaultItem.type)
|
|
||||||
defaultItem.sw = item ? item.sw : false
|
|
||||||
return item
|
|
||||||
})
|
|
||||||
}
|
|
||||||
await writeNotifyList(defaultData)
|
|
||||||
}
|
|
||||||
resolve()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function initNotifyConfigDB() {
|
async function initNotifyConfigDB() {
|
||||||
return new Promise((resolve, reject) => {
|
const notifyConfigDB = new NotifyConfigDB().getInstance()
|
||||||
const notifyConfigDB = new NotifyConfigDB().getInstance()
|
let count = await notifyConfigDB.countAsync({})
|
||||||
notifyConfigDB.count({}, async (err, count) => {
|
if (count !== 0) return
|
||||||
if (err) {
|
consola.log('初始化NotifyConfigDB✔')
|
||||||
consola.log('初始化NotifyConfigDB错误:', err)
|
const defaultData = {
|
||||||
reject(err)
|
type: 'sct',
|
||||||
} else {
|
sct: {
|
||||||
if (count === 0) {
|
sendKey: ''
|
||||||
consola.log('初始化NotifyConfigDB✔')
|
},
|
||||||
const defaultData = {
|
email: {
|
||||||
type: 'sct',
|
service: 'QQ',
|
||||||
sct: {
|
user: '',
|
||||||
sendKey: ''
|
pass: ''
|
||||||
},
|
}
|
||||||
email: {
|
}
|
||||||
service: 'QQ',
|
return notifyConfigDB.insertAsync(defaultData)
|
||||||
user: '',
|
|
||||||
pass: ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await writeNotifyConfig(defaultData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolve()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async () => {
|
module.exports = async () => {
|
||||||
|
@ -52,7 +52,6 @@ module.exports.NotifyDB = class NotifyDB {
|
|||||||
if (!NotifyDB.instance) {
|
if (!NotifyDB.instance) {
|
||||||
NotifyDB.instance = new Datastore({ filename: notifyDBPath, autoload: true })
|
NotifyDB.instance = new Datastore({ filename: notifyDBPath, autoload: true })
|
||||||
// NotifyDB.instance.setAutocompactionInterval(5000)
|
// NotifyDB.instance.setAutocompactionInterval(5000)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getInstance() {
|
getInstance() {
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
const nodemailer = require('nodemailer')
|
const nodemailer = require('nodemailer')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const { getNotifySwByType, readNotifyConfig } = require('../utils/storage')
|
|
||||||
const commonTemp = require('../template/commonTemp')
|
const commonTemp = require('../template/commonTemp')
|
||||||
|
const { NotifyDB, NotifyConfigDB } = require('./db-class')
|
||||||
|
const notifyConfigDB = new NotifyConfigDB().getInstance()
|
||||||
|
const notifyDB = new NotifyDB().getInstance()
|
||||||
|
|
||||||
function sendServerChan(sendKey, title, content) {
|
function sendServerChan(sendKey, title, content) {
|
||||||
if (!sendKey) return consola.error('发送server酱通知失败, sendKey 为空')
|
if (!sendKey) return consola.error('发送server酱通知失败, sendKey 为空')
|
||||||
@ -56,10 +58,11 @@ function sendEmail({ service, user, pass }, title, content) {
|
|||||||
// 异步发送通知
|
// 异步发送通知
|
||||||
async function asyncSendNotice(noticeAction, title, content) {
|
async function asyncSendNotice(noticeAction, title, content) {
|
||||||
try {
|
try {
|
||||||
let sw = await getNotifySwByType(noticeAction) // 获取对应动作的通知开关
|
let notifyList = await notifyDB.findAsync({})
|
||||||
|
let { sw } = notifyList.find((item) => item.type === noticeAction) // 获取对应动作的通知开关
|
||||||
console.log('notify swtich: ', noticeAction, sw)
|
console.log('notify swtich: ', noticeAction, sw)
|
||||||
if (!sw) return
|
if (!sw) return
|
||||||
let notifyConfig = await readNotifyConfig()
|
let notifyConfig = await notifyConfigDB.findOneAsync({})
|
||||||
let { type } = notifyConfig
|
let { type } = notifyConfig
|
||||||
if (!type) return consola.error('通知类型不存在: ', type)
|
if (!type) return consola.error('通知类型不存在: ', type)
|
||||||
title = `EasyNode-${ title }`
|
title = `EasyNode-${ title }`
|
||||||
|
@ -64,81 +64,17 @@ const writeSSHRecord = async (record = []) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const readNotifyConfig = async () => {
|
// const getNotifySwByType = async (type) => {
|
||||||
return new Promise((resolve, reject) => {
|
// if (!type) throw Error('missing params: type')
|
||||||
const notifyConfigDB = new NotifyConfigDB().getInstance()
|
// try {
|
||||||
notifyConfigDB.findOne({}, (err, doc) => {
|
// let notifyList = await readNotifyList()
|
||||||
if (err) {
|
// let { sw } = notifyList.find((item) => item.type === type)
|
||||||
reject(err)
|
// return sw
|
||||||
} else {
|
// } catch (error) {
|
||||||
resolve(doc)
|
// consola.error(`通知类型[${ type }]不存在`)
|
||||||
}
|
// return false
|
||||||
})
|
// }
|
||||||
})
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
const writeNotifyConfig = async (keyObj = {}) => {
|
|
||||||
const notifyConfigDB = new NotifyConfigDB().getInstance()
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
notifyConfigDB.update({}, { $set: keyObj }, { upsert: true }, (err, numReplaced) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err)
|
|
||||||
} else {
|
|
||||||
notifyConfigDB.compactDatafile()
|
|
||||||
resolve(numReplaced)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const getNotifySwByType = async (type) => {
|
|
||||||
if (!type) throw Error('missing params: type')
|
|
||||||
try {
|
|
||||||
let notifyList = await readNotifyList()
|
|
||||||
let { sw } = notifyList.find((item) => item.type === type)
|
|
||||||
return sw
|
|
||||||
} catch (error) {
|
|
||||||
consola.error(`通知类型[${ type }]不存在`)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const readNotifyList = async () => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const notifyDB = new NotifyDB().getInstance()
|
|
||||||
notifyDB.find({}, (err, docs) => {
|
|
||||||
if (err) {
|
|
||||||
consola.error('读取notify list错误: ', err)
|
|
||||||
reject(err)
|
|
||||||
} else {
|
|
||||||
resolve(docs)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const writeNotifyList = async (notifyList) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const notifyDB = new NotifyDB().getInstance()
|
|
||||||
notifyDB.remove({}, { multi: true }, (err) => {
|
|
||||||
if (err) {
|
|
||||||
consola.error('清空notify list出错:', err)
|
|
||||||
reject(err)
|
|
||||||
} else {
|
|
||||||
notifyDB.compactDatafile()
|
|
||||||
notifyDB.insert(notifyList, (err, newDocs) => {
|
|
||||||
if (err) {
|
|
||||||
consola.error('写入新的notify list出错:', err)
|
|
||||||
reject(err)
|
|
||||||
} else {
|
|
||||||
notifyDB.compactDatafile()
|
|
||||||
resolve(newDocs)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const readScriptList = async () => {
|
const readScriptList = async () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -225,8 +161,6 @@ const deleteOneKeyRecord = async (ids =[]) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
readSSHRecord, writeSSHRecord,
|
readSSHRecord, writeSSHRecord,
|
||||||
readKey, writeKey,
|
readKey, writeKey,
|
||||||
readNotifyList, writeNotifyList,
|
|
||||||
readNotifyConfig, writeNotifyConfig, getNotifySwByType,
|
|
||||||
readScriptList, writeScriptList,
|
readScriptList, writeScriptList,
|
||||||
readOneKeyRecord, writeOneKeyRecord, deleteOneKeyRecord
|
readOneKeyRecord, writeOneKeyRecord, deleteOneKeyRecord
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user