From 524e521ad3548934c4effa104eb4ac1299a1816a Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Thu, 2 Sep 2021 19:23:11 -0500 Subject: Add function to generate sitemap --- src/index.js | 3 +- src/misc/sitemap.xml | 411 --------------------------------------------------- src/server.js | 41 ++++- 3 files changed, 39 insertions(+), 416 deletions(-) delete mode 100644 src/misc/sitemap.xml diff --git a/src/index.js b/src/index.js index 7ceecf0..c2ad0c1 100644 --- a/src/index.js +++ b/src/index.js @@ -59,7 +59,7 @@ function setUpModels(){ }, likes: { type: Sequelize.INTEGER, - } + }, }), "pictures": database.define('pictures', { source: { type: Sequelize.TEXT, allowNull: false}, @@ -109,7 +109,6 @@ function setUpModels(){ const models = setUpModels(); const templates = require('./templates'); sync(); - server.setUpRoutes(models, jwtFunctions, database, templates.setUpTemplates()); server.listen(config.port); diff --git a/src/misc/sitemap.xml b/src/misc/sitemap.xml deleted file mode 100644 index 1554976..0000000 --- a/src/misc/sitemap.xml +++ /dev/null @@ -1,411 +0,0 @@ - - - - - - - https://marks.kitchen/ - 2021-03-13T20:17:27+00:00 - 1.00 - - - https://marks.kitchen/bread - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/blog - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/email - 2021-03-13T19:55:53+00:00 - 0.80 - - - https://marks.kitchen/projects - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/misc - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/post/index/71 - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/tags/update - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/post/index/52 - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/post/index/43 - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/post/index/35 - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/post/index/12 - 2021-03-13T20:17:27+00:00 - 0.80 - - - https://marks.kitchen/post/bread/72 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/focaccia - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/67 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/wheat - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/rosemary - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/56 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/baguette - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/54 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/pizza - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/scones - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/51 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/sourdough - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/50 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/49 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/46 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/white - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/45 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/babka - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/44 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/41 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/40 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/37 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/36 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/33 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/32 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/31 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/30 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/28 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/27 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/1 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/2 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/3 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/4 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/5 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/6 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/7 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/8 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/9 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/10 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/bread/11 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/73 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/best-of - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/70 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/100days - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/69 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/68 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/66 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/65 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/dev - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/64 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/63 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/62 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/61 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/60 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/55 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/59 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/57 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/53 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/48 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/47 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/blog - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/42 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/39 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/tags/telegram - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/38 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/post/blog/34 - 2021-03-13T20:17:27+00:00 - 0.64 - - - https://marks.kitchen/chess - 2021-03-13T19:55:27+00:00 - 0.64 - - - https://marks.kitchen/word-square - 2021-03-13T19:57:12+00:00 - 0.64 - - - diff --git a/src/server.js b/src/server.js index c770304..478e601 100644 --- a/src/server.js +++ b/src/server.js @@ -58,7 +58,7 @@ function formatDate(d) { async function formatPostsforSingle(models, postType, postId){ var posts = await models.posts.findAll({ - where: { + where: { type: postType, id: postId }, order: [['createdAt', 'DESC']] @@ -85,6 +85,36 @@ async function formatPostsForType(models, postType){ return posts; } +function to_sitemap_xml(host, path, updated){ + return `${host}${path}${updated}` +} + +async function sitemap(models) { + urlset = [] + host = "https://marks.kitchen" + let routes = [ + "/", + "/bread", + "/blog", + "/email", + "/work-square", + "/misc", + "/projects", + ] + routes.forEach(item => { + urlset.push(to_sitemap_xml(host, item, Date.now())) + }) + let posts = (await models.posts.findAll()).map(x => x.get({ plain: true })); + posts.forEach(post => { + urlset.push(to_sitemap_xml(host, `/posts/${post.type}/${post.id}`, posts.updatedAt)) + }) + let tags = (await models.tags.findAll()).map(x => x.get({ plain: true })); + tags.forEach(tag => { + urlset.push(to_sitemap_xml(host, `/tags/${tag.text}`, tag.updatedAt)) + }) + return `${urlset.join("")}`; +} + function setUpRoutes(models, jwtFunctions, database, templates) { // Authentication routine server.use(function (req, res, next) { @@ -174,8 +204,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { let body = templates["tags"]({posts, name}) res.status(200).send(body) }) - - server.get('/sitemap.xml', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/misc/sitemap.xml")); + server.get('/robots.txt', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/misc/robots.txt")); server.get('/admin', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/admin.html")); server.get('/login', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/login.html")) @@ -199,6 +228,11 @@ function setUpRoutes(models, jwtFunctions, database, templates) { res.status(200).send(body) }) + server.get('/sitemap.xml', cache('1 day'), async (req, res) => { + res.setHeader('Content-Type', 'text/xml') + res.status(200).send(await sitemap(models)) + }); + server.get('/wordsquares/best', cache('5 minutes'), async (req, res, next) => { var best = await database.query("select words, name from wordsquares where best = 1", { type: database.QueryTypes.SELECT }) res.status(200).send({ best: best }); @@ -269,6 +303,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { const type = req.body.type req.body.description = marked(req.body.description) req.body.likes = 0 + req.body.sent_email = false const newPost = await models.posts.create(req.body); req.files.forEach(async (file) => { await models.pictures.create({ "source": "uploads/" + file.filename, "postId": newPost.id }); -- cgit v1.2.3