preparing for multiple event types

This commit is contained in:
Timo Hocker 2019-08-29 11:35:53 +02:00
parent 50db9ce6df
commit 82adf635eb
4 changed files with 64 additions and 40 deletions

View File

@ -1,20 +1,28 @@
const http = require('http');
const url = require('url');
const { Client } = require('pg');
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
const pg = new Client(config.database);
pg.connect();
pg.query(fs.readFileSync('init.sql', 'utf-8'));
const pg = require('postgresupdater')(
config.database.host,
config.database.port,
config.database.user,
config.database.password,
config.database.database
);
const server = http.createServer(async (req, res) => {
await pg.waitForInit();
const queryUrl = url.parse(req.url, true);
const query = queryUrl.query;
if (!queryUrl.search) {
let body;
req.on('data', data => {
body += data;
});
req.on('end', async () => {
if (!body && !queryUrl.search) {
fs.readFile('view.html', (err, file) => {
res.writeHead(200, { 'content-type': 'text/html' });
res.end(file);
@ -22,7 +30,7 @@ const server = http.createServer(async (req, res) => {
return;
} else if (queryUrl.search == '?json') {
const data = await pg.query(
`SELECT "Timestamp", "App", "Client", "Message", "Misc", "Stack" FROM "LogView"`
`SELECT "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "LogView"`
);
const rows = [];
for (let row of data.rows) {
@ -33,27 +41,12 @@ const server = http.createServer(async (req, res) => {
return;
}
if (!query.client || query.client.length < 5) {
res.writeHead(400);
res.end();
return;
}
const client = query.client.substr(4);
const app = query.client.substr(0, 4);
let body;
req.on('data', data => {
body += data;
});
req.on('end', () => {
try {
body = JSON.parse(/(?:undefined)*(.*)/.exec(body)[1]);
console.log(app, client, body.message, body.misc, body.stack);
console.log(body.app, body.client, body.message, body.misc, body.stack);
pg.query(
`INSERT INTO "Log" ("App", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5)`,
[app, client, body.message, body.misc, body.stack]
`INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`,
[body.app, body.type, body.client, body.message, body.misc, body.stack]
);
} catch (e) {
console.error(e);

View File

@ -13,6 +13,6 @@
"author": "Timo Hocker",
"license": "GPL-3.0-or-later",
"dependencies": {
"pg": "^7.10.0"
"postgresupdater": "^1.0.0"
}
}

View File

@ -1,4 +1,3 @@
-- sql file will be automatically executed to setup the database
-- create table
CREATE TABLE IF NOT EXISTS public."Log"
(

32
updates/1.sql Normal file
View File

@ -0,0 +1,32 @@
-- drop view to remove dependencies
DROP VIEW public."LogView";
-- add type column
ALTER TABLE public."Log"
ADD COLUMN "Type" character varying(64) NOT NULL DEFAULT 'Error';
-- change app column to varying 64
ALTER TABLE public."Log"
ALTER COLUMN "App" TYPE character varying(64);
-- recreate view
CREATE OR REPLACE VIEW public."LogView" AS
SELECT 'Timestamp'::text AS "Timestamp",
'App'::character varying AS "App",
'Type'::character varying AS "Type",
'Client'::character varying AS "Client",
'Message'::text AS "Message",
'Misc'::text AS "Misc",
'Stack'::text AS "Stack",
'0'::bigint AS "ID"
UNION ALL
SELECT COALESCE(to_char("Log"."Timestamp", 'YYYY-MM-DD HH24:MI:SS'::text), ''::text) AS "Timestamp",
"Log"."App",
"Log"."Type",
"Log"."Client",
"Log"."Message",
"Log"."Misc",
"Log"."Stack",
"Log"."ID"
FROM "Log"
ORDER BY 8;