AppReports/index.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-09-03 08:30:47 +02:00
const path = require('path');
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
2019-09-19 12:28:14 +02:00
app.use(bodyParser.text({type: '*/*'}));
2019-09-03 08:30:47 +02:00
app.post('/', (req, res, next) => {
console.log('post');
if (req.query.json) {
// json mod requests
2019-09-19 12:28:14 +02:00
} else {
try {
const json = JSON.parse(req.body);
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
]
);
} catch {
console.log('invalid json', req.body);
}
2019-09-03 08:30:47 +02:00
}
res.status(201).end();
next();
});
2019-07-04 12:21:37 +02:00
2019-09-03 12:24:50 +02:00
app.get('/*', async (req, res, next) => {
if (typeof req.query.json != 'undefined') {
2019-09-03 08:30:47 +02:00
const data = await pg.query(
`SELECT "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "LogView"`
);
const rows = [];
for (let row of data.rows) {
rows.push(Object.values(row));
}
res
.status(200)
.type('application/json')
.end(JSON.stringify(rows));
2019-09-03 12:24:50 +02:00
next();
} else {
express.static(path.join(__dirname, 'html'))(req, res, next);
2019-09-03 08:30:47 +02:00
}
2019-07-04 12:21:37 +02:00
});
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);