AppReports/index.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-07-04 12:21:37 +02:00
const fs = require('fs');
2019-09-03 08:30:47 +02:00
const express = require('express');
2019-09-19 12:28:14 +02:00
const bodyParser = require('body-parser');
2019-07-04 12:21:37 +02:00
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
2019-08-29 11:35:53 +02:00
const pg = require('postgresupdater')(
config.database.host,
config.database.port,
config.database.user,
config.database.password,
config.database.database
);
2019-07-04 12:21:37 +02:00
2019-09-03 08:30:47 +02:00
const app = express();
2019-08-29 11:35:53 +02:00
const tryParseJSON = function(text) {
try {
return JSON.parse(text);
} catch {
console.log('invalid json');
}
};
2019-09-26 15:07:56 +02:00
app.use(bodyParser.text({ type: '*/*' }));
2019-09-03 08:30:47 +02:00
app.post('/', (req, res, next) => {
const json = tryParseJSON(req.body);
if (typeof req.query.json != 'undefined') {
switch (json.action) {
case 'delete': {
pg.query('DELETE FROM "Log" WHERE ID = $1', json.data);
}
2019-09-19 12:28:14 +02:00
}
} else {
pg.query(
`INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`,
[json.app, json.type, json.client, json.message, json.misc, json.stack]
);
2019-09-03 08:30:47 +02:00
}
res.status(201).end();
next();
});
2019-07-04 12:21:37 +02:00
2019-09-26 15:07:56 +02:00
app.get('/', async (req, res, next) => {
2019-09-03 12:24:50 +02:00
if (typeof req.query.json != 'undefined') {
2019-09-03 08:30:47 +02:00
const data = await pg.query(
`SELECT "ID", "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "Log"`
2019-09-03 08:30:47 +02:00
);
const rows = [];
const headings = data.rows.length > 0 ? Object.keys(data.rows[0]) : [];
const hiddenData = [0];
2019-09-03 08:30:47 +02:00
for (let row of data.rows) {
rows.push(Object.values(row));
}
res
.status(200)
.type('application/json')
.end(
JSON.stringify({ headings: headings, data: rows, hidden: hiddenData })
);
2019-09-03 12:24:50 +02:00
} else {
2019-09-26 15:07:56 +02:00
next();
2019-09-03 08:30:47 +02:00
}
2019-07-04 12:21:37 +02:00
});
2019-09-26 15:07:56 +02:00
app.use(express.static('html'));
2019-09-03 08:30:47 +02:00
app.listen(config.port);
2019-07-04 12:21:37 +02:00
console.log('server listening on', config.port);