AppReports/index.js
Timo Hocker 4960433c0a fixes
2019-09-19 12:28:14 +02:00

67 lines
1.6 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
const pg = require('postgresupdater')(
config.database.host,
config.database.port,
config.database.user,
config.database.password,
config.database.database
);
const app = express();
app.use(bodyParser.text({type: '*/*'}));
app.post('/', (req, res, next) => {
console.log('post');
if (req.query.json) {
// json mod requests
} 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);
}
}
res.status(201).end();
next();
});
app.get('/*', async (req, res, next) => {
if (typeof req.query.json != 'undefined') {
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));
next();
} else {
express.static(path.join(__dirname, 'html'))(req, res, next);
}
});
app.listen(config.port);
console.log('server listening on', config.port);