webio-node/route/api/server.js

142 lines
4.6 KiB
JavaScript

const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res, n) => {
res.send({ name: 'WebIO Server IPMI/SNMP API' });
})
.post('*', async(req, res, n) => {
try {
res.db = await mysql.getConn();
} catch (e) {
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
.post('/getdevicelist', (req, res, n) => {
if (!config.permission.server) return n('ERR9000');
let query = "select * from ??.??";
let param = [config.db.db11, 'dev'];
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8000');
res.api_res = {
record: tool.checkArray(row)
}
n();
})
})
.post('/getdevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
let query = "select * from ??.?? where `devuid` = ?";
let param = [config.db.db11, 'dev', arr.data.id];
try {
let dev = await tool.promiseQuery(res, query, param);
if (dev.data.length == 0) return n('ERR8000');
let ipmi_param = [config.db.db11, 'ipmi', arr.data.id];
let ipmi = await tool.promiseQuery(res, query, ipmi_param);
let snmp_param = [config.db.db11, 'snmp', arr.data.id];
let snmp = await tool.promiseQuery(res, query, snmp_param);
res.api_res = {
record: tool.checkArray(dev.data),
rt: {
ipmi: tool.checkArray(ipmi.data),
snmp: tool.checkArray(snmp.data)
}
}
} catch (e) {
return rt.err(res, e, n, 'ERR8000');
}
n();
})
.post('/adddevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.name) return n('ERR0026');
let u = '';
let obj = so.get(req.headers['x-auth-token'] || '');
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
try {
let query = "insert into ??.?? (`name`, `ctime`, `cuser`, `mtime`, `muser`) values \
(?, unix_timestamp(), ?, unix_timestamp(), ?)";
let param = [config.db.db11, 'dev', arr.data.name, u, u];
await tool.promiseQuery(res, query, param);
} catch (e) {
return rt.err(res, e, n, 'ERR8001');
}
res.api_res = {
record: []
};
n();
})
.post('/editdevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
let u = '';
let obj = so.get(req.headers['x-auth-token'] || '');
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
try {
let query = "update ??.?? set \
`name` = ?, \
`muser` = ?, \
`mtime` = unix_timestamp() \
where \
`devuid` = ?";
let param = [config.db.db11, 'dev', arr.data.name, u, arr.data.id];
await tool.promiseQuery(res, query, param);
} catch (e) {
return rt.err(res, e, n, 'ERR8002');
}
res.api_res = {
record: []
};
n();
})
.post('/deldevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
try {
} catch (err) {
return rt.err(res, err, n, 'ERR8003');
}
})
.all('*', rt.send);
module.exports = router;