From 82adf635eb63344c0955e3b4de3eb9f5050ab273 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 29 Aug 2019 11:35:53 +0200 Subject: [PATCH] preparing for multiple event types --- index.js | 69 ++++++++++++++++++--------------------- package.json | 2 +- init.sql => updates/0.sql | 1 - updates/1.sql | 32 ++++++++++++++++++ 4 files changed, 64 insertions(+), 40 deletions(-) rename init.sql => updates/0.sql (89%) create mode 100644 updates/1.sql diff --git a/index.js b/index.js index 7bc02a4..37936c5 100644 --- a/index.js +++ b/index.js @@ -1,59 +1,52 @@ 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) { - fs.readFile('view.html', (err, file) => { - res.writeHead(200, { 'content-type': 'text/html' }); - res.end(file); - }); - return; - } else if (queryUrl.search == '?json') { - const data = await pg.query( - `SELECT "Timestamp", "App", "Client", "Message", "Misc", "Stack" FROM "LogView"` - ); - const rows = []; - for (let row of data.rows) { - rows.push(Object.values(row)); - } - res.writeHead(200, { 'content-type': 'application/json' }); - res.end(JSON.stringify(rows)); - 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', () => { + req.on('end', async () => { + if (!body && !queryUrl.search) { + fs.readFile('view.html', (err, file) => { + res.writeHead(200, { 'content-type': 'text/html' }); + res.end(file); + }); + return; + } else if (queryUrl.search == '?json') { + 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.writeHead(200, { 'content-type': 'application/json' }); + res.end(JSON.stringify(rows)); + return; + } + 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); diff --git a/package.json b/package.json index 14343ed..41a90af 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,6 @@ "author": "Timo Hocker", "license": "GPL-3.0-or-later", "dependencies": { - "pg": "^7.10.0" + "postgresupdater": "^1.0.0" } } diff --git a/init.sql b/updates/0.sql similarity index 89% rename from init.sql rename to updates/0.sql index 1fdd4c2..9dd5b68 100644 --- a/init.sql +++ b/updates/0.sql @@ -1,4 +1,3 @@ --- sql file will be automatically executed to setup the database -- create table CREATE TABLE IF NOT EXISTS public."Log" ( diff --git a/updates/1.sql b/updates/1.sql new file mode 100644 index 0000000..6389fc9 --- /dev/null +++ b/updates/1.sql @@ -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; \ No newline at end of file