AppReports/index.js

62 lines
1.4 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-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-03 08:30:47 +02:00
app.use(express.json());
app.post('/', (req, res, next) => {
console.log('post');
if (req.query.json) {
// json mod requests
} else {
console.log(req.body);
pg.query(
`INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`,
[
req.body.app,
req.body.type,
req.body.client,
req.body.message,
req.body.misc,
req.body.stack
]
);
}
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);