giving more data to client to allow for later management

This commit is contained in:
Timo Hocker 2019-09-26 15:59:24 +02:00
parent 078ad18862
commit b6bccfb07f
3 changed files with 44 additions and 24 deletions

View File

@ -1,4 +1,3 @@
const path = require('path');
const fs = require('fs'); const fs = require('fs');
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
@ -15,22 +14,29 @@ const pg = require('postgresupdater')(
const app = express(); const app = express();
const tryParseJSON = function(text) {
try {
return JSON.parse(text);
} catch {
console.log('invalid json');
}
};
app.use(bodyParser.text({ type: '*/*' })); app.use(bodyParser.text({ type: '*/*' }));
app.post('/', (req, res, next) => { app.post('/', (req, res, next) => {
console.log('post'); const json = tryParseJSON(req.body);
if (req.query.json) { if (typeof req.query.json != 'undefined') {
// json mod requests switch (json.action) {
case 'delete': {
pg.query('DELETE FROM "Log" WHERE ID = $1', json.data);
}
}
} else { } else {
try {
const json = JSON.parse(req.body);
pg.query( pg.query(
`INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`, `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] [json.app, json.type, json.client, json.message, json.misc, json.stack]
); );
} catch {
console.log('invalid json', req.body);
}
} }
res.status(201).end(); res.status(201).end();
next(); next();
@ -39,16 +45,20 @@ app.post('/', (req, res, next) => {
app.get('/', async (req, res, next) => { app.get('/', async (req, res, next) => {
if (typeof req.query.json != 'undefined') { if (typeof req.query.json != 'undefined') {
const data = await pg.query( 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"`
); );
const rows = []; const rows = [];
const headings = data.rows.length > 0 ? Object.keys(data.rows[0]) : [];
const hiddenData = [0];
for (let row of data.rows) { for (let row of data.rows) {
rows.push(Object.values(row)); rows.push(Object.values(row));
} }
res res
.status(200) .status(200)
.type('application/json') .type('application/json')
.end(JSON.stringify(rows)); .end(
JSON.stringify({ headings: headings, data: rows, hidden: hiddenData })
);
} else { } else {
next(); next();
} }

2
updates/2.sql Normal file
View File

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

View File

@ -1,9 +1,18 @@
<template> <template>
<div id="app"> <div id="app">
<div> <div>
<table> <table v-if="json.headings && json.data">
<tr v-for="(row,rowKey) in json" :key="rowKey"> <tr>
<td v-for="(value,key) in row" :key="key">{{value}}</td> <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> </tr>
</table> </table>
</div> </div>
@ -22,7 +31,7 @@ export default {
name: "App", name: "App",
data() { data() {
return { return {
json: [] json: {}
}; };
}, },
mounted() { mounted() {
@ -30,11 +39,6 @@ export default {
fetch("?json") fetch("?json")
.then(res => res.json()) .then(res => res.json())
.then(json => (self.json = json)); .then(json => (self.json = json));
},
watch: {
json() {
console.log(this.json);
}
} }
}; };
</script> </script>
@ -47,6 +51,10 @@ td {
padding: 5px; padding: 5px;
} }
th {
padding: 5px;
}
table { table {
border-collapse: collapse; border-collapse: collapse;
} }