diff --git a/index.js b/index.js index 37936c5..b92dbf7 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,8 @@ -const http = require('http'); -const url = require('url'); +// const http = require('http'); +// const url = require('url'); +const path = require('path'); const fs = require('fs'); +const express = require('express'); const config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); @@ -12,51 +14,99 @@ const pg = require('postgresupdater')( config.database.database ); -const server = http.createServer(async (req, res) => { - await pg.waitForInit(); - const queryUrl = url.parse(req.url, true); +const app = express(); - let body; - req.on('data', data => { - body += data; - }); +app.use(express.json()); +app.use(express.static(path.join(__dirname, 'html'))); - 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(body.app, body.client, body.message, body.misc, body.stack); - pg.query( - `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); - if (body) console.error(body); - } - - res.writeHead(201); - res.end(); - }); +app.post('/', (req, res, next) => { + console.log('post'); + if (req.query.json) { + // json mod requests + } else { + console.log(req.body); + pg.query( + `INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`, + [ + req.body.app, + req.body.type, + req.body.client, + req.body.message, + req.body.misc, + req.body.stack + ] + ); + } + res.status(201).end(); + next(); }); -server.listen(config.port); +app.get('/', async (req, res, next) => { + console.log('get'); + if (req.query.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 + .status(200) + .type('application/json') + .end(JSON.stringify(rows)); + return; + } + next(); +}); + +app.listen(config.port); + +// const server = http.createServer(async (req, res) => { +// await pg.waitForInit(); +// const queryUrl = url.parse(req.url, true); + +// 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); +// }); +// 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(body.app, body.client, body.message, body.misc, body.stack); +// pg.query( +// `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); +// if (body) console.error(body); +// } + +// res.writeHead(201); +// res.end(); +// }); +// }); + +// server.listen(config.port); console.log('server listening on', config.port); diff --git a/package.json b/package.json index a921651..2697fc0 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "license": "GPL-3.0-or-later", "dependencies": { "express": "^4.17.1", - "postgresupdater": "^1.0.0", - "serve-static": "^1.14.1" + "postgresupdater": "^1.0.0" } } diff --git a/view.html b/view.html deleted file mode 100644 index daae497..0000000 --- a/view.html +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - -
-
-
- GNU General Public License v3.0 or later © Timo Hocker - - - diff --git a/web/src/App.vue b/web/src/App.vue index 6f86de0..143bda7 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -1,28 +1,55 @@ diff --git a/web/src/assets/logo.png b/web/src/assets/logo.png deleted file mode 100644 index f3d2503..0000000 Binary files a/web/src/assets/logo.png and /dev/null differ diff --git a/web/src/components/HelloWorld.vue b/web/src/components/HelloWorld.vue deleted file mode 100644 index 1c19f2a..0000000 --- a/web/src/components/HelloWorld.vue +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - diff --git a/web/src/components/LogEntry.vue b/web/src/components/LogEntry.vue new file mode 100644 index 0000000..3b3b55d --- /dev/null +++ b/web/src/components/LogEntry.vue @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file