diff options
author | Mark Powers <markppowers0@gmail.com> | 2019-03-17 12:07:34 -0400 |
---|---|---|
committer | Mark Powers <markppowers0@gmail.com> | 2019-03-17 12:07:34 -0400 |
commit | c30218c68330f1dbcca99983f2f7f7d5274b1832 (patch) | |
tree | 24608ea8298ef2964153ad4924f1639424d5fb07 /src | |
parent | 13407c1acdf6a8c8893c78f2c1959e7ac8752b20 (diff) |
Fix request logging bug
Diffstat (limited to 'src')
-rw-r--r-- | src/html/admin.html | 10 | ||||
-rw-r--r-- | src/index.js | 2 | ||||
-rw-r--r-- | 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 @@ </div> <table class="table session-table" v-if="showSessionTable"> <tr><th>Host</th><th>Total Requests</th></tr> - <tr v-for="item in stats.cookie"> - <td>{{item.cookie.substring(0,10)}}...</td><td>{{item.c}}</td> + <tr v-for="item in stats.session"> + <td>{{item.session.substring(0,10)}}...</td><td>{{item.c}}</td> </tr> </table> <div> @@ -71,7 +71,9 @@ <table class="table url-table" v-if="showUrlTable"> <tr><th>Method</th><th>Path</th><th>Total Requests</th></tr> <tr v-for="item in stats.url"> - <td>{{item.method}}</td><td>{{item.url}}</td><td>{{item.c}}</td> + <td>{{item.method}}</td> + <td>{{item.url.substring(0, Math.min(30, item.url.length))}}</td> + <td>{{item.c}}</td> </tr> </table> </div> 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 { |