diff options
author | Mark Powers <mark@marks.kitchen> | 2021-09-02 19:23:11 -0500 |
---|---|---|
committer | Mark Powers <mark@marks.kitchen> | 2021-09-02 19:23:11 -0500 |
commit | 524e521ad3548934c4effa104eb4ac1299a1816a (patch) | |
tree | 641296819bde9fb31c9c9f566c4905b564e81a81 /src | |
parent | d5f2c72acd877acbb3683b50cfbbe9f3c0d98af3 (diff) |
Add function to generate sitemap
Diffstat (limited to 'src')
-rw-r--r-- | src/index.js | 3 | ||||
-rw-r--r-- | src/misc/sitemap.xml | 411 | ||||
-rw-r--r-- | src/server.js | 41 |
3 files changed, 39 insertions, 416 deletions
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 @@ -<?xml version="1.0" encoding="UTF-8"?>
-<urlset
- xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
- http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
-<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
-
-
-<url>
- <loc>https://marks.kitchen/</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>1.00</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/bread</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/blog</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/email</loc>
- <lastmod>2021-03-13T19:55:53+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/projects</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/misc</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/index/71</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/update</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/index/52</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/index/43</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/index/35</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/index/12</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.80</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/72</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/focaccia</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/67</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/wheat</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/rosemary</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/56</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/baguette</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/54</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/pizza</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/scones</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/51</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/sourdough</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/50</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/49</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/46</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/white</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/45</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/babka</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/44</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/41</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/40</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/37</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/36</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/33</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/32</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/31</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/30</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/28</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/27</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/1</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/2</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/3</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/4</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/5</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/6</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/7</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/8</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/9</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/10</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/bread/11</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/73</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/best-of</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/70</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/100days</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/69</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/68</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/66</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/65</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/dev</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/64</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/63</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/62</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/61</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/60</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/55</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/59</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/57</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/53</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/48</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/47</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/blog</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/42</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/39</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/tags/telegram</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/38</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/post/blog/34</loc>
- <lastmod>2021-03-13T20:17:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/chess</loc>
- <lastmod>2021-03-13T19:55:27+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-<url>
- <loc>https://marks.kitchen/word-square</loc>
- <lastmod>2021-03-13T19:57:12+00:00</lastmod>
- <priority>0.64</priority>
-</url>
-
-</urlset>
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 `<url><loc>${host}${path}</loc><lastmod>${updated}</lastmod></url>` +} + +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>${urlset.join("")}</urlset>`; +} + 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 }); |