preparing for multiple event types
This commit is contained in:
parent
50db9ce6df
commit
82adf635eb
45
index.js
45
index.js
@ -1,20 +1,28 @@
|
|||||||
const http = require('http');
|
const http = require('http');
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
const { Client } = require('pg');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
|
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
|
||||||
|
|
||||||
const pg = new Client(config.database);
|
const pg = require('postgresupdater')(
|
||||||
pg.connect();
|
config.database.host,
|
||||||
|
config.database.port,
|
||||||
pg.query(fs.readFileSync('init.sql', 'utf-8'));
|
config.database.user,
|
||||||
|
config.database.password,
|
||||||
|
config.database.database
|
||||||
|
);
|
||||||
|
|
||||||
const server = http.createServer(async (req, res) => {
|
const server = http.createServer(async (req, res) => {
|
||||||
|
await pg.waitForInit();
|
||||||
const queryUrl = url.parse(req.url, true);
|
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) => {
|
fs.readFile('view.html', (err, file) => {
|
||||||
res.writeHead(200, { 'content-type': 'text/html' });
|
res.writeHead(200, { 'content-type': 'text/html' });
|
||||||
res.end(file);
|
res.end(file);
|
||||||
@ -22,7 +30,7 @@ const server = http.createServer(async (req, res) => {
|
|||||||
return;
|
return;
|
||||||
} else if (queryUrl.search == '?json') {
|
} else if (queryUrl.search == '?json') {
|
||||||
const data = await pg.query(
|
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 = [];
|
const rows = [];
|
||||||
for (let row of data.rows) {
|
for (let row of data.rows) {
|
||||||
@ -33,27 +41,12 @@ const server = http.createServer(async (req, res) => {
|
|||||||
return;
|
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 {
|
try {
|
||||||
body = JSON.parse(/(?:undefined)*(.*)/.exec(body)[1]);
|
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(
|
pg.query(
|
||||||
`INSERT INTO "Log" ("App", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5)`,
|
`INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`,
|
||||||
[app, client, body.message, body.misc, body.stack]
|
[body.app, body.type, body.client, body.message, body.misc, body.stack]
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
@ -13,6 +13,6 @@
|
|||||||
"author": "Timo Hocker",
|
"author": "Timo Hocker",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
"dependencies": {
|
"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
|
||||||
CREATE TABLE IF NOT EXISTS public."Log"
|
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