This commit is contained in:
chaosBreaking
2019-08-31 14:59:13 +08:00
commit dc5423d356
65 changed files with 3813 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
const System = require('./system.model');
const _cache = {};
const provider = {
getValue: async function (key) {
if (!_cache[key]) {
await System.findOne({ key }).then(data => {
_cache[key] = data.value;
}).catch(() => {
mylog.error('unexpected key', key);
return null;
});
}
return _cache[key];
}
};
module.exports = {
provider,
getConfig: function (req, res) {
const { key } = req.query;
if (!_cache[key]) {
System.findOne({ key }).then(data => {
_cache[key] = data.value;
res.json({
code: 0,
data
});
}).catch(err => {
res.json({
code: 1,
err
});
});
} else res.json({
code: 0,
data: _cache[key]
});
},
publishConfig: async function (req, res) {
const { key, value, option } = req.body;
const isExist = await System.exists({ key });
if (!isExist) {
new System({ key, value, option }).save()
.then(data => {
_cache[key] = data.value;
res.json({
code: 0,
msg: 'success',
data
});
})
.catch(err => {
res.json({
code: 2,
err
});
});
} else {
res.json({
code: 1,
msg: 'key already existed'
});
}
},
updateConfig: async function (req, res) {
const { key, value, option } = req.body;
const isExist = await System.exists({ key });
if (isExist) {
System.findOneAndUpdate({ key }, { value, option }, { new: true })
.then(data => {
_cache[key] = data.value;
res.json({
code: 0,
msg: 'success',
data
});
})
.catch(err => {
res.json({
code: 2,
err
});
});
} else {
res.json({
code: 1,
err: "key doesn't existed"
});
}
}
};

View File

@@ -0,0 +1,26 @@
'use strict';
const mongoose = require('mongoose');
const SystemSchema = new mongoose.Schema({
key: {
type: String,
required: true,
unique: true
},
value: {
type: {},
required: true
},
option: {
type: {}
},
}, {
timestamps: {
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
const systemSchema = mongoose.model('System', SystemSchema);
module.exports = systemSchema;

View File

@@ -0,0 +1,25 @@
const indexRoute = '/system';
const controller = require('./system.controller');
const services = [
{
url: '/get',
method: 'GET',
controller: controller.getConfig
},
{
url: '/publish',
method: 'POST',
controller: controller.publishConfig
},
{
url: '/update',
method: 'POST',
controller: controller.updateConfig
},
];
module.exports = services.map(service => {
// 可以在此处对要导出的服务map进行统一处理
service.url = indexRoute + service.url;
return service;
});