preparing for multiple event types
This commit is contained in:
parent
50db9ce6df
commit
82adf635eb
69
index.js
69
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);
|
||||
|
@ -13,6 +13,6 @@
|
||||
"author": "Timo Hocker",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"pg": "^7.10.0"
|
||||
"postgresupdater": "^1.0.0"
|
||||
}
|
||||
}
|
||||
|
@ -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
32
updates/1.sql
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user