Compare commits

...

11 Commits

Author SHA1 Message Date
627cfa8592 order log 2020-02-26 15:26:19 +01:00
653cc41ff2 fix error 2020-02-26 13:57:22 +01:00
ad17778f38 update modules 2020-01-23 08:41:45 +01:00
687735e29e add entry count info 2020-01-23 08:36:53 +01:00
de3ec4572d remove item import 2020-01-22 07:35:34 +01:00
a8223d110b fix 2020-01-21 18:25:35 +01:00
207c7c3ba9 add log clearing, client restriction 2020-01-21 16:14:53 +01:00
f493dfaed0 fix readme 2019-10-21 11:08:56 +02:00
2db228bdb1 modularize items 2019-10-21 11:07:16 +02:00
850091188f update packages, fix readme 2019-10-21 10:16:32 +02:00
b6bccfb07f giving more data to client to allow for later management 2019-09-26 15:59:24 +02:00
7 changed files with 84 additions and 42 deletions

View File

@ -54,18 +54,17 @@ window.onerror = async function(msg, source, lineno, colno, error) {
let data;
if (error) {
data = {
app: 'app1',
type: 'error',
client: appClientId,
message: msg,
stack: error.stack,
misc: `v: ${appVersion} src: ${source} ln: ${lineno} col: ${colno}`
};
} else {
data = { message: msg };
data = { app: 'app1', type: 'error', client: appClientId, message: msg };
}
/*
* the first 4 characters of the client will be displayed as app
* the remaining characters (up to 64) will be displayed as client
*/
fetch('http://localhost:8080?client=app1' + appClientId, {
fetch('http://localhost:8080', {
method: 'POST',
body: JSON.stringify(data),
mode: 'no-cors'

View File

@ -6,5 +6,11 @@
"password": "12345",
"database": "appreports"
},
"port": "8080"
"port": "8080",
"restrictClients": false,
"allowedClients": [
"127.0.0.1",
"::1"
],
"clearLogAfterDays": -1
}

View File

@ -1,4 +1,3 @@
const path = require('path');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
@ -15,22 +14,25 @@ const pg = require('postgresupdater')(
const app = express();
const tryParseJSON = function(text) {
try {
return JSON.parse(text);
} catch {
console.log('invalid json');
}
};
app.use(bodyParser.text({ type: '*/*' }));
app.post('/', (req, res, next) => {
console.log('post');
if (req.query.json) {
// json mod requests
const json = tryParseJSON(req.body);
if (typeof req.query.json != 'undefined') {
//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);
}
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]
);
}
res.status(201).end();
next();
@ -38,17 +40,35 @@ app.post('/', (req, res, next) => {
app.get('/', async (req, res, next) => {
if (typeof req.query.json != 'undefined') {
if (
config.restrictClients &&
!config.allowedClients.includes(req.connection.remoteAddress)
) {
console.log('blocked request from ' + req.connection.remoteAddress);
res.status(403).end();
return;
}
const data = await pg.query(
`SELECT "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "LogView"`
`SELECT "ID", "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "Log" ORDER BY "Timestamp" ASC`
);
const rows = [];
const headings = data.rows.length > 0 ? Object.keys(data.rows[0]) : [];
const hiddenData = [0];
for (let row of data.rows) {
rows.push(Object.values(row));
}
res
.status(200)
.type('application/json')
.end(JSON.stringify(rows));
.end(
JSON.stringify({ headings: headings, data: rows, hidden: hiddenData })
);
try {
const clearLogInterval = Number.parseInt(config.clearLogAfterDays);
if (clearLogInterval > 0)
await pg.query(`DELETE FROM "Log" WHERE "Timestamp" < now() - interval '${clearLogInterval} days'`);
} catch (e) {}
} else {
next();
}

26
package-lock.json generated
View File

@ -264,14 +264,15 @@
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
},
"pg": {
"version": "7.12.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.12.1.tgz",
"integrity": "sha512-l1UuyfEvoswYfcUe6k+JaxiN+5vkOgYcVSbSuw3FvdLqDbaoa2RJo1zfJKfPsSYPFVERd4GHvX3s2PjG1asSDA==",
"version": "7.17.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.17.1.tgz",
"integrity": "sha512-SYWEip6eADsgDQIZk0bmB2JDOrC8Xu6z10KlhlXl03NSomwVmHB6ZTVyDCwOfT6bXHI8QndJdk5XxSSRXikaSA==",
"requires": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-pool": "^2.0.4",
"pg-packet-stream": "^1.1.0",
"pg-pool": "^2.0.9",
"pg-types": "^2.1.0",
"pgpass": "1.x",
"semver": "4.3.2"
@ -287,10 +288,15 @@
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="
},
"pg-packet-stream": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/pg-packet-stream/-/pg-packet-stream-1.1.0.tgz",
"integrity": "sha512-kRBH0tDIW/8lfnnOyTwKD23ygJ/kexQVXZs7gEyBljw4FYqimZFxnMMx50ndZ8In77QgfGuItS5LLclC2TtjYg=="
},
"pg-pool": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.7.tgz",
"integrity": "sha512-UiJyO5B9zZpu32GSlP0tXy8J2NsJ9EFGFfz5v6PSbdz/1hBLX1rNiiy5+mAm5iJJYwfCv4A0EBcQLGWwjbpzZw=="
"version": "2.0.9",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.9.tgz",
"integrity": "sha512-gNiuIEKNCT3OnudQM2kvgSnXsLkSpd6mS/fRnqs6ANtrke6j8OY5l9mnAryf1kgwJMWLg0C1N1cYTZG1xmEYHQ=="
},
"pg-types": {
"version": "2.2.0",
@ -336,9 +342,9 @@
}
},
"postgresupdater": {
"version": "1.0.13",
"resolved": "https://registry.npmjs.org/postgresupdater/-/postgresupdater-1.0.13.tgz",
"integrity": "sha512-pKPEZCR42lLjx8uYmf+Bqccdmx5tUa1CHGsNK91AEpEy8lM9CLuXkitLHFaiSG37bUweqSdd4C5a//mGTS/9Vw==",
"version": "1.0.16",
"resolved": "https://registry.npmjs.org/postgresupdater/-/postgresupdater-1.0.16.tgz",
"integrity": "sha512-6pe6ozTKwSqevDcXC/WIUvb0qBMUlCWtwKb8EOg1c+qf12Hyh+5U/t5Mq79QI8Pa3Z7d86nStGhnVpSevH2HHg==",
"requires": {
"pg": "^7.12.1"
}

View File

@ -15,6 +15,6 @@
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"postgresupdater": "^1.0.13"
"postgresupdater": "^1.0.16"
}
}

2
updates/2.sql Normal file
View File

@ -0,0 +1,2 @@
-- drop view
DROP VIEW public."LogView";

View File

@ -1,9 +1,19 @@
<template>
<div id="app">
<div>
<div v-if="json.headings && json.data">
<p>successfully loaded {{json.data.length}} entries</p>
<table>
<tr v-for="(row,rowKey) in json" :key="rowKey">
<td v-for="(value,key) in row" :key="key">{{value}}</td>
<tr>
<th
v-for="(value, key) in json.headings.filter((val,ind)=>!json.hidden.includes(ind))"
:key="key"
>{{value}}</th>
</tr>
<tr v-for="(row,rowKey) in json.data" :key=rowKey>
<td
v-for="(value,key) in row.filter((val,ind)=>!json.hidden.includes(ind))"
:key="key"
>{{value}}</td>
</tr>
</table>
</div>
@ -22,7 +32,7 @@ export default {
name: "App",
data() {
return {
json: []
json: {}
};
},
mounted() {
@ -30,11 +40,6 @@ export default {
fetch("?json")
.then(res => res.json())
.then(json => (self.json = json));
},
watch: {
json() {
console.log(this.json);
}
}
};
</script>
@ -47,6 +52,10 @@ td {
padding: 5px;
}
th {
padding: 5px;
}
table {
border-collapse: collapse;
}