From b70b80bbea1e50b41259f71159f197c4212d2b81 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Wed, 19 Aug 2020 12:43:22 +0200 Subject: [PATCH] add post log api --- lib/api/index.js | 2 ++ lib/api/post-log.js | 36 ++++++++++++++++++++++++++++++++++++ lib/db/log.js | 5 +++++ 3 files changed, 43 insertions(+) create mode 100644 lib/api/post-log.js diff --git a/lib/api/index.js b/lib/api/index.js index de299cc..72cf33c 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -13,4 +13,6 @@ const router = require ('express') router.get ('/log', require ('./get-log')); router.get ('/app', require ('./get-app')); +router.post ('/log', require ('./post-log')); + module.exports = router; diff --git a/lib/api/post-log.js b/lib/api/post-log.js new file mode 100644 index 0000000..a84323c --- /dev/null +++ b/lib/api/post-log.js @@ -0,0 +1,36 @@ +/* + * Copyright (C) Sapphirecode - All Rights Reserved + * This file is part of appreports which is released under GPL-3.0-or-later. + * See file 'LICENSE' for full license details. + * Created by Timo Hocker , August 2020 + */ + +'use strict'; + +const db = require ('../db'); +const { http } = require ('@sapphirecode/consts'); + +module.exports = async (req, res) => { + const { app_id } = req.headers; + if ( + app_id === 'undefined' + || isNaN (parseInt (app_id)) + ) { + res.status (http.status_bad_request) + .end (); + return; + } + + const log = req.body; + if (typeof log !== 'object') { + res.status (http.status_bad_request) + .end (); + return; + } + + const { message, data, timestamp } = log; + await db.log.insert (app_id, message, data, timestamp); + + res.status (http.status_created) + .end (); +}; diff --git a/lib/db/log.js b/lib/db/log.js index e39da50..39cc304 100644 --- a/lib/db/log.js +++ b/lib/db/log.js @@ -18,5 +18,10 @@ module.exports = (get_db) => ({ ) .from ('log') .where ({ app_id }); + }, + insert (app_id, message, data = '{}', timestamp = (new Date)) { + const knex = get_db (); + return knex ('log') + .insert ({ message, data, timestamp, app_id }); } });