From c30218c68330f1dbcca99983f2f7f7d5274b1832 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sun, 17 Mar 2019 12:07:34 -0400 Subject: Fix request logging bug --- src/html/admin.html | 10 ++++++---- src/index.js | 2 +- src/server.js | 29 +++++++++++++++-------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/html/admin.html b/src/html/admin.html index a1fcec8..c42ee95 100644 --- a/src/html/admin.html +++ b/src/html/admin.html @@ -13,7 +13,7 @@ var feed = new Vue({ el: '#stats', data: { - stats: {cookie: [], url: []}, + stats: {session: [], url: []}, showSessionTable: true, showUrlTable: true, }, @@ -60,8 +60,8 @@ - - + +
HostTotal Requests
{{item.cookie.substring(0,10)}}...{{item.c}}
{{item.session.substring(0,10)}}...{{item.c}}
@@ -71,7 +71,9 @@ - + + +
MethodPathTotal Requests
{{item.method}}{{item.url}}{{item.c}}{{item.method}}{{item.url.substring(0, Math.min(30, item.url.length))}}{{item.c}}
diff --git a/src/index.js b/src/index.js index 1a78436..3a0573e 100644 --- a/src/index.js +++ b/src/index.js @@ -70,7 +70,7 @@ function setUpModels(){ allowNull: false, },}), "requests": database.define('requests', { - cookie: Sequelize.STRING, + session: Sequelize.STRING, method: Sequelize.STRING, url: Sequelize.STRING, }) diff --git a/src/server.js b/src/server.js index f154f8d..bf415ed 100644 --- a/src/server.js +++ b/src/server.js @@ -26,12 +26,12 @@ const server = express(); server.use(cookieParser()) server.use(bodyParser.urlencoded({ extended: true })); -async function addImagesAndTagsToPosts(models, posts){ +async function addImagesAndTagsToPosts(models, posts) { for (const post of posts) { const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id } }).map(x => x.source); post.images = images; const tags = await models.tags.findAll({ attributes: ["text"], where: { postId: post.id } }).map(x => x.text); - post.tags= tags; + post.tags = tags; } } @@ -69,13 +69,14 @@ function setUpRoutes(models, jwtFunctions, database) { // Route logging server.use(function (req, res, next) { - let cookie = req.cookies.authorization + let cookie = req.cookies.session; if (!cookie) { - res.cookie('session-id', uuidv4(), { expires: new Date(Date.now() + (1000*60*60))}); + cookie = uuidv4(); + res.cookie('session', session, { expires: new Date(Date.now() + (1000 * 60 * 60)) }); } models.requests.create({ - createdAt: new Date(), cookie: cookie, method: req.method, url: req.originalUrl + createdAt: new Date(), session: cookie, method: req.method, url: req.originalUrl }); next() }) @@ -96,9 +97,9 @@ function setUpRoutes(models, jwtFunctions, database) { }) server.get('/admin/stats', async (req, res, next) => { try { - var sessionResult = await database.query("SELECT cookie, count(id) as c FROM requests GROUP BY cookie", { type: database.QueryTypes.SELECT }) + var sessionResult = await database.query("SELECT session, count(id) as c FROM requests GROUP BY session", { type: database.QueryTypes.SELECT }) var urlResult = await database.query("SELECT method, url, count(id) as c FROM requests GROUP BY method, url", { type: database.QueryTypes.SELECT }) - res.status(200).send({ cookie: sessionResult, url: urlResult }); + res.status(200).send({ session: sessionResult, url: urlResult }); next(); } catch (e) { res.status(400).send(e.message); @@ -112,11 +113,11 @@ function setUpRoutes(models, jwtFunctions, database) { try { const { name } = req.params; const postsWithTag = await models.tags.findAll({ attributes: ["postId"], where: { text: name } }) - .map(function(x) { - return {id: x.postId} + .map(function (x) { + return { id: x.postId } }); - var posts = await models.posts.findAll({ - where: { [Op.or]: postsWithTag }, order: [['createdAt', 'DESC']] + var posts = await models.posts.findAll({ + where: { [Op.or]: postsWithTag }, order: [['createdAt', 'DESC']] }); posts = posts.map(x => x.get({ plain: true })); await addImagesAndTagsToPosts(models, posts) @@ -132,7 +133,7 @@ function setUpRoutes(models, jwtFunctions, database) { try { const { type } = req.params; var posts = await models.posts.findAll({ - where: { type: type }, order: [['createdAt', 'DESC']] + where: { type: type }, order: [['createdAt', 'DESC']] }); posts = posts.map(x => x.get({ plain: true })); await addImagesAndTagsToPosts(models, posts) @@ -151,7 +152,7 @@ function setUpRoutes(models, jwtFunctions, database) { console.log("uploaded ", file.path); }) req.body.tags.split(" ").forEach(async (tag) => { - await models.tags.create({ "text": tag, "postId": newPost.id}); + await models.tags.create({ "text": tag, "postId": newPost.id }); }) console.log(newPost); res.redirect(`/${type}`); @@ -165,7 +166,7 @@ function setUpRoutes(models, jwtFunctions, database) { const user = await models.users.findOne({ where: { username: req.body.username, password: hash } }) if (user) { const token = jwtFunctions.sign(user.username); - res.cookie('authorization', token, { expires: new Date(Date.now() + (1000*60*60))}); + res.cookie('authorization', token, { expires: new Date(Date.now() + (1000 * 60 * 60)) }); console.debug("Redirecting to admin - logged in") res.redirect('/admin'); } else { -- cgit v1.2.3