From 75e5102ffd532a50604f6a95864fa6a13c32c235 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 5 Mar 2022 01:29:05 +0330 Subject: rss, WIP --- server.js | 19 +++++++++++-------- views/index.ejs | 4 ++++ views/rss_feed.pug | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 views/rss_feed.pug diff --git a/server.js b/server.js index ec4350e..6ae2184 100755 --- a/server.js +++ b/server.js @@ -33,6 +33,10 @@ app.engine("ejs", require("ejs").__express); app.use(helmet()); app.use(morgan("combined")); +async function enumerateDir() { + return await fs.readdirSync(path.join(__dirname, "mds")); +} + function renderAndSend(req, res) { try { let viewPath; @@ -90,10 +94,13 @@ app.get("/robots.txt", (req, res) => { res.send(robots_txt); }); -// app.get("/rss/feed", (req, res) => { -// let html = pug.renderFile("./views/rss_feed.pug", merge(options, localls)); -// res.send(html); -// }); +app.get("/rss/feed", (req, res) => { + const compiledFunction = pug.compileFile("./views/rss_feed.pug"); + const files = fs.readdirSync(path.join(__dirname, "mds")); + for (const file of files) { + res.send(compiledFunction(file)); + } +}); app.get("/$", (req, res) => { renderAndSend(req, res); @@ -106,10 +113,6 @@ app.get("/mds/:mdname$", (req, res) => { renderAndSend(req, res); }); -async function enumerateDir() { - return await fs.readdirSync(path.join(__dirname, "mds")); -} - app.use(sitemap(enumerateDir, "https://blog.terminaldweller.com")); app.use((req, res) => { diff --git a/views/index.ejs b/views/index.ejs index 4679e4a..529a5ed 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -5,6 +5,8 @@ Blog + + @@ -15,6 +17,8 @@
Archive About +     + RSS diff --git a/views/rss_feed.pug b/views/rss_feed.pug new file mode 100644 index 0000000..9bdd9bd --- /dev/null +++ b/views/rss_feed.pug @@ -0,0 +1,21 @@ +doctype xml +rss(version='2.0', xmlns:atom='http://www.w3.org/2005/Atom') + channel + title DevSmash + link http://devsmash.com + atom:link(href='http://devsmash.com/feed/rss', rel='self', type='application/rss+xml') + description Developers talking about stuff that developers like to talk about. + language en-US + if posts.length + lastBuildDate= new Date(posts[0].publishedAt).toUTCString() + each post in posts + item + title= post.title + link http://devsmash.com/blog/#{post.slug} + description + | http://devsmash.com/blog/#{post.slug}')!= 'Read more »' + | ]]> + pubDate= new Date(post.publishedAt).toUTCString() + guid(isPermaLink='false') Date: Fri, 25 Mar 2022 19:06:57 +0430 Subject: wip --- docker-compose-test.yaml | 28 +++ model.js | 32 ++++ package-lock.json | 485 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + server.js | 36 +++- static/about.html | 6 + views/index.ejs | 2 +- 7 files changed, 588 insertions(+), 2 deletions(-) create mode 100644 model.js diff --git a/docker-compose-test.yaml b/docker-compose-test.yaml index 73981ef..243b282 100644 --- a/docker-compose-test.yaml +++ b/docker-compose-test.yaml @@ -6,6 +6,7 @@ services: context: ./ networks: - webnet + - dbnet ports: - "19009:9000" cap_drop: @@ -13,5 +14,32 @@ services: environment: - SERVER_DEPLOYMENT_TYPE=test - SERVER_LISTEN_PORT=9000 + depends_on: + - mongo + mongo: + image: mongo:5.0 + networks: + - dbnet + restart: on-failure + ports: + - "27117:27017" + - "127.0.0.1:27118:27018" + - "127.0.0.1:27119:27019" + volumes: + - db-data:/data/db + env: + - MONGO_INITDB_ROOT_USERNAME_FILE=/run/secrets/mongo_user + - MONGO_INITDB_ROOT_PASSWORD_FILE=/run/secrets/mongo_pass + secrets: + - mongo_user + - mongo_pass networks: webnet: + dbnet: +volumes: + db-data: +secrets: + mongo_user: + file: ./mongo_user + mongo_pass: + file: ./mongo_pass diff --git a/model.js b/model.js new file mode 100644 index 0000000..7eba2a4 --- /dev/null +++ b/model.js @@ -0,0 +1,32 @@ +"use strict"; + +const mongoose = require("mongoose"); +mongoose.Promise = global.Promise; +const db = {}; +db.mongoose = mongoose; +db.url = "mongo:27017"; + +const blogPostModel = mongoose.model( + "blogPost", + mongoose.Schema( + { + title: String, + description: String, + published: Boolean, + }, + { timestamps: true } + ) +); + +function dbInit() { + db.mongoose + .connect(db.url, { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => { + console.log("successfully connected to db"); + }) + .catch((err) => { + console.log("cannot connect to the database: ", err); + process.exit(1); + }); +} +module.exports = dbInit; diff --git a/package-lock.json b/package-lock.json index c111ea8..aa9d629 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "markdown-it-highlightjs": "^3.4.0", "markdown-it-multimd-table": "^4.0.2", "markdown-it-texmath": "^0.7.0", + "mongoose": "^6.2.8", "morgan": "^1.10.0", "pug": "^3.0.2", "spdy": "^4.0.2" @@ -176,6 +177,25 @@ "node": ">= 6" } }, + "node_modules/@types/node": { + "version": "17.0.23", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz", + "integrity": "sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==" + }, + "node_modules/@types/webidl-conversions": { + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-6.1.1.tgz", + "integrity": "sha512-XAahCdThVuCFDQLT7R7Pk/vqeObFNL3YqRyFZg+AqAP/W1/w3xHaIxuW7WszQqTbIBOPRcItYJIou3i/mppu3Q==" + }, + "node_modules/@types/whatwg-url": { + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-8.2.1.tgz", + "integrity": "sha512-2YubE1sjj5ifxievI5Ge1sckb9k/Er66HyR2c+3+I6VDUUg1TLPdYYTEbQ+DjRkS4nTxMJhgWfSfMRD2sl2EYQ==", + "dependencies": { + "@types/node": "*", + "@types/webidl-conversions": "*" + } + }, "node_modules/abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", @@ -376,6 +396,25 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -421,6 +460,40 @@ "resolved": "https://registry.npmjs.org/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz", "integrity": "sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==" }, + "node_modules/bson": { + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/bson/-/bson-4.6.2.tgz", + "integrity": "sha512-VeJKHShcu1b/ugl0QiujlVuBepab714X9nNyBdA1kfekuDGecxgpTA2Z6nYbagrWFeiIyzSWIOzju3lhj+RNyQ==", + "dependencies": { + "buffer": "^5.6.0" + }, + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -625,6 +698,14 @@ "node": ">=0.4.0" } }, + "node_modules/denque": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.0.1.tgz", + "integrity": "sha512-tfiWc6BQLXNLpNiR5iGd0Ocu3P3VpxfzFiqubLgMfhfOw9WyvgJBd46CClNn9k3qfbjvT//0cf7AlYRX/OslMQ==", + "engines": { + "node": ">=0.10" + } + }, "node_modules/depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -1579,6 +1660,25 @@ "node": ">=0.10.0" } }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/ignore": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", @@ -1628,6 +1728,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, + "node_modules/ip": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", + "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=" + }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -1840,6 +1945,11 @@ "promise": "^7.0.1" } }, + "node_modules/kareem": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.4.tgz", + "integrity": "sha512-Vcrt8lcpVl0s8ePx634BxwRqmFo+5DcOhlmNadehxreMTIQi/9hOL/B3hZQQbK5DgMS7Lem3xABXV7/S3jy+7g==" + }, "node_modules/katex": { "version": "0.11.1", "resolved": "https://registry.npmjs.org/katex/-/katex-0.11.1.tgz", @@ -1987,6 +2097,12 @@ "url": "https://github.com/sindresorhus/mem?sponsor=1" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, "node_modules/merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -2054,6 +2170,89 @@ "node": "*" } }, + "node_modules/mongodb": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.3.1.tgz", + "integrity": "sha512-sNa8APSIk+r4x31ZwctKjuPSaeKuvUeNb/fu/3B6dRM02HpEgig7hTHM8A/PJQTlxuC/KFWlDlQjhsk/S43tBg==", + "dependencies": { + "bson": "^4.6.1", + "denque": "^2.0.1", + "mongodb-connection-string-url": "^2.4.1", + "socks": "^2.6.1" + }, + "engines": { + "node": ">=12.9.0" + }, + "optionalDependencies": { + "saslprep": "^1.0.3" + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-2.5.2.tgz", + "integrity": "sha512-tWDyIG8cQlI5k3skB6ywaEA5F9f5OntrKKsT/Lteub2zgwSUlhqEN2inGgBTm8bpYJf8QYBdA/5naz65XDpczA==", + "dependencies": { + "@types/whatwg-url": "^8.2.1", + "whatwg-url": "^11.0.0" + } + }, + "node_modules/mongodb-connection-string-url/node_modules/tr46": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz", + "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", + "dependencies": { + "punycode": "^2.1.1" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/mongodb-connection-string-url/node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "engines": { + "node": ">=12" + } + }, + "node_modules/mongodb-connection-string-url/node_modules/whatwg-url": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz", + "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", + "dependencies": { + "tr46": "^3.0.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/mongoose": { + "version": "6.2.8", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-6.2.8.tgz", + "integrity": "sha512-Wq6HG0sOJEQHp5YqMlxrnf93vBFwdY2zlAwqI97EPPSt69kiVV21pTv4cDanrCNWi4upG8ajQ/p9jpDjcECjkQ==", + "dependencies": { + "bson": "^4.2.2", + "kareem": "2.3.4", + "mongodb": "4.3.1", + "mpath": "0.8.4", + "mquery": "4.0.2", + "ms": "2.1.3", + "sift": "16.0.0" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mongoose/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, "node_modules/morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -2077,6 +2276,46 @@ "node": ">= 0.8" } }, + "node_modules/mpath": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.8.4.tgz", + "integrity": "sha512-DTxNZomBcTWlrMW76jy1wvV37X/cNNxPW1y2Jzd4DZkAaC5ZGsm8bfGfNOthcDuRJujXLqiuS6o3Tpy0JEoh7g==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-4.0.2.tgz", + "integrity": "sha512-oAVF0Nil1mT3rxty6Zln4YiD6x6QsUWYz927jZzjMxOK2aqmhEz5JQ7xmrKK7xRFA2dwV+YaOpKU/S+vfNqKxA==", + "dependencies": { + "debug": "4.x" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/mquery/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/mquery/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -2515,6 +2754,18 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "node_modules/saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "dependencies": { + "sparse-bitfield": "^3.0.3" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/saxes": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/saxes/-/saxes-5.0.1.tgz", @@ -2622,6 +2873,11 @@ "node": ">=8" } }, + "node_modules/sift": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/sift/-/sift-16.0.0.tgz", + "integrity": "sha512-ILTjdP2Mv9V1kIxWMXeMTIRbOBrqKc4JAXmFMnFq3fKeyQ2Qwa3Dw1ubcye3vR+Y6ofA0b9gNDr/y2t6eUeIzQ==" + }, "node_modules/slice-ansi": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-4.0.0.tgz", @@ -2672,6 +2928,28 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/smart-buffer": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz", + "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==", + "engines": { + "node": ">= 6.0.0", + "npm": ">= 3.0.0" + } + }, + "node_modules/socks": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/socks/-/socks-2.6.2.tgz", + "integrity": "sha512-zDZhHhZRY9PxRruRMR7kMhnf3I8hDs4S3f9RecfnGxvcBHQcKcIH/oUcEWffsfl1XxdYlA7nnlGbbTvPz9D8gA==", + "dependencies": { + "ip": "^1.1.5", + "smart-buffer": "^4.2.0" + }, + "engines": { + "node": ">= 10.13.0", + "npm": ">= 3.0.0" + } + }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -2681,6 +2959,15 @@ "node": ">=0.10.0" } }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/spdy": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/spdy/-/spdy-4.0.2.tgz", @@ -3297,6 +3584,25 @@ "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==" }, + "@types/node": { + "version": "17.0.23", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz", + "integrity": "sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==" + }, + "@types/webidl-conversions": { + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-6.1.1.tgz", + "integrity": "sha512-XAahCdThVuCFDQLT7R7Pk/vqeObFNL3YqRyFZg+AqAP/W1/w3xHaIxuW7WszQqTbIBOPRcItYJIou3i/mppu3Q==" + }, + "@types/whatwg-url": { + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-8.2.1.tgz", + "integrity": "sha512-2YubE1sjj5ifxievI5Ge1sckb9k/Er66HyR2c+3+I6VDUUg1TLPdYYTEbQ+DjRkS4nTxMJhgWfSfMRD2sl2EYQ==", + "requires": { + "@types/node": "*", + "@types/webidl-conversions": "*" + } + }, "abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", @@ -3451,6 +3757,11 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" + }, "basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -3490,6 +3801,23 @@ "resolved": "https://registry.npmjs.org/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz", "integrity": "sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==" }, + "bson": { + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/bson/-/bson-4.6.2.tgz", + "integrity": "sha512-VeJKHShcu1b/ugl0QiujlVuBepab714X9nNyBdA1kfekuDGecxgpTA2Z6nYbagrWFeiIyzSWIOzju3lhj+RNyQ==", + "requires": { + "buffer": "^5.6.0" + } + }, + "buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "requires": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -3660,6 +3988,11 @@ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, + "denque": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.0.1.tgz", + "integrity": "sha512-tfiWc6BQLXNLpNiR5iGd0Ocu3P3VpxfzFiqubLgMfhfOw9WyvgJBd46CClNn9k3qfbjvT//0cf7AlYRX/OslMQ==" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -4375,6 +4708,11 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" + }, "ignore": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", @@ -4412,6 +4750,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, + "ip": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", + "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=" + }, "ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -4579,6 +4922,11 @@ "promise": "^7.0.1" } }, + "kareem": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.4.tgz", + "integrity": "sha512-Vcrt8lcpVl0s8ePx634BxwRqmFo+5DcOhlmNadehxreMTIQi/9hOL/B3hZQQbK5DgMS7Lem3xABXV7/S3jy+7g==" + }, "katex": { "version": "0.11.1", "resolved": "https://registry.npmjs.org/katex/-/katex-0.11.1.tgz", @@ -4702,6 +5050,12 @@ "mimic-fn": "^3.0.0" } }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -4748,6 +5102,72 @@ "brace-expansion": "^1.1.7" } }, + "mongodb": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.3.1.tgz", + "integrity": "sha512-sNa8APSIk+r4x31ZwctKjuPSaeKuvUeNb/fu/3B6dRM02HpEgig7hTHM8A/PJQTlxuC/KFWlDlQjhsk/S43tBg==", + "requires": { + "bson": "^4.6.1", + "denque": "^2.0.1", + "mongodb-connection-string-url": "^2.4.1", + "saslprep": "^1.0.3", + "socks": "^2.6.1" + } + }, + "mongodb-connection-string-url": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-2.5.2.tgz", + "integrity": "sha512-tWDyIG8cQlI5k3skB6ywaEA5F9f5OntrKKsT/Lteub2zgwSUlhqEN2inGgBTm8bpYJf8QYBdA/5naz65XDpczA==", + "requires": { + "@types/whatwg-url": "^8.2.1", + "whatwg-url": "^11.0.0" + }, + "dependencies": { + "tr46": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz", + "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", + "requires": { + "punycode": "^2.1.1" + } + }, + "webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==" + }, + "whatwg-url": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz", + "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", + "requires": { + "tr46": "^3.0.0", + "webidl-conversions": "^7.0.0" + } + } + } + }, + "mongoose": { + "version": "6.2.8", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-6.2.8.tgz", + "integrity": "sha512-Wq6HG0sOJEQHp5YqMlxrnf93vBFwdY2zlAwqI97EPPSt69kiVV21pTv4cDanrCNWi4upG8ajQ/p9jpDjcECjkQ==", + "requires": { + "bson": "^4.2.2", + "kareem": "2.3.4", + "mongodb": "4.3.1", + "mpath": "0.8.4", + "mquery": "4.0.2", + "ms": "2.1.3", + "sift": "16.0.0" + }, + "dependencies": { + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + } + } + }, "morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -4767,6 +5187,34 @@ } } }, + "mpath": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.8.4.tgz", + "integrity": "sha512-DTxNZomBcTWlrMW76jy1wvV37X/cNNxPW1y2Jzd4DZkAaC5ZGsm8bfGfNOthcDuRJujXLqiuS6o3Tpy0JEoh7g==" + }, + "mquery": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-4.0.2.tgz", + "integrity": "sha512-oAVF0Nil1mT3rxty6Zln4YiD6x6QsUWYz927jZzjMxOK2aqmhEz5JQ7xmrKK7xRFA2dwV+YaOpKU/S+vfNqKxA==", + "requires": { + "debug": "4.x" + }, + "dependencies": { + "debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -5121,6 +5569,15 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, "saxes": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/saxes/-/saxes-5.0.1.tgz", @@ -5206,6 +5663,11 @@ "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", "dev": true }, + "sift": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/sift/-/sift-16.0.0.tgz", + "integrity": "sha512-ILTjdP2Mv9V1kIxWMXeMTIRbOBrqKc4JAXmFMnFq3fKeyQ2Qwa3Dw1ubcye3vR+Y6ofA0b9gNDr/y2t6eUeIzQ==" + }, "slice-ansi": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-4.0.0.tgz", @@ -5243,12 +5705,35 @@ } } }, + "smart-buffer": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz", + "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==" + }, + "socks": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/socks/-/socks-2.6.2.tgz", + "integrity": "sha512-zDZhHhZRY9PxRruRMR7kMhnf3I8hDs4S3f9RecfnGxvcBHQcKcIH/oUcEWffsfl1XxdYlA7nnlGbbTvPz9D8gA==", + "requires": { + "ip": "^1.1.5", + "smart-buffer": "^4.2.0" + } + }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", "optional": true }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "requires": { + "memory-pager": "^1.0.2" + } + }, "spdy": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/spdy/-/spdy-4.0.2.tgz", diff --git a/package.json b/package.json index e7d7a5e..bef37be 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "markdown-it-highlightjs": "^3.4.0", "markdown-it-multimd-table": "^4.0.2", "markdown-it-texmath": "^0.7.0", + "mongoose": "^6.2.8", "morgan": "^1.10.0", "pug": "^3.0.2", "spdy": "^4.0.2" diff --git a/server.js b/server.js index 6ae2184..4b67056 100755 --- a/server.js +++ b/server.js @@ -25,12 +25,46 @@ const morgan = require("morgan"); const pug = require("pug"); const app = express(); +app.disable("x-powered-by"); app.use(express.static(path.join(__dirname, "css"))); app.use(express.static(path.join(__dirname, "static"))); app.set("views", path.join(__dirname, "views")); app.set("view engine", "ejs"); app.engine("ejs", require("ejs").__express); -app.use(helmet()); + +app.use(helmet.crossOriginEmbedderPolicy()); +app.use(helmet.crossOriginOpenerPolicy()); +app.use(helmet.crossOriginResourcePolicy()); +app.use(helmet.dnsPrefetchControl()); +app.use(helmet.expectCt()); +app.use(helmet.frameguard()); +app.use(helmet.hidePoweredBy()); +app.use(helmet.hsts()); +app.use(helmet.ieNoOpen()); +app.use(helmet.noSniff()); +app.use(helmet.originAgentCluster()); +app.use(helmet.permittedCrossDomainPolicies()); +app.use(helmet.referrerPolicy()); +app.use(helmet.xssFilter()); +app.use((req, res, next) => { + res.setHeader( + "Permissions-Policy", + "geolocation=(none),midi=(none),notifications=(none),push=(none),sync-xhr=(none),microphone=(none),camera=(none),magnetometer=(none),gyroscope=(none),speaker=(none),vibrate=(none),fullscreen=(self),payment=(none)" + ); + next(); +}); +app.use( + helmet.contentSecurityPolicy({ + useDefaults: false, + directives: { + baseUri: ["self"], + defaultSrc: ["self"], + scriptSrc: ["none"], + styleSrc: ["self", "https:", "unsafef-inline"], + }, + }) +); + app.use(morgan("combined")); async function enumerateDir() { diff --git a/static/about.html b/static/about.html index ad9ed10..b6bdfa9 100644 --- a/static/about.html +++ b/static/about.html @@ -18,6 +18,12 @@

You can find my github here + . +

+

+ Also if the blog is broken please make a new issue + here + .

diff --git a/views/index.ejs b/views/index.ejs index 529a5ed..6cc3a45 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -9,7 +9,7 @@ - +
<%- data.blogHttp %>
-- cgit v1.2.3 From acd76a660ad5cc6502e78553fbb3a4ad8f58ec7a Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 8 Apr 2022 10:45:41 +0430 Subject: updated the permissions policiy --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 4b67056..a42072f 100755 --- a/server.js +++ b/server.js @@ -49,7 +49,7 @@ app.use(helmet.xssFilter()); app.use((req, res, next) => { res.setHeader( "Permissions-Policy", - "geolocation=(none),midi=(none),notifications=(none),push=(none),sync-xhr=(none),microphone=(none),camera=(none),magnetometer=(none),gyroscope=(none),speaker=(none),vibrate=(none),fullscreen=(self),payment=(none)" + "geolocation=(self),midi=(self),sync-xhr=(self),microphone=(self),camera=(self),magnetometer=(self),gyroscope=(self),fullscreen=(self),payment=(self),usb=(self)" ); next(); }); -- cgit v1.2.3 From 4b70fc00a84c38e0051efb88227318f9337f9632 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 15 Apr 2022 11:50:58 +0430 Subject: was using node image for self-signed certificates --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index f7697b1..23e5984 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:lts-alpine3.13 AS certbuilder +FROM alpine:3.15 AS certbuilder RUN apk add openssl WORKDIR /certs RUN openssl req -nodes -new -x509 -subj="/C=US/ST=Denial/L=springfield/O=Dis/CN=localhost" -keyout server.key -out server.cert -- cgit v1.2.3 From 4eaa437c0a285c617e30a533d1ef369eaa8644a6 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 5 Jul 2022 20:19:16 +0430 Subject: WIP, adding rss to the blog --- Dockerfile | 4 ++-- docker-compose-test.yaml | 25 ++++++++++++---------- docker-compose.yaml | 39 ++++++++++++++++++++++++++++++---- model.js | 55 ++++++++++++++++++++++++++++++++++++------------ server.js | 34 ++++++++++++++++++++++-------- static/about.html | 6 ++++++ views/index.ejs | 8 ++++++- views/rss_feed.pug | 18 ++++++++-------- 8 files changed, 140 insertions(+), 49 deletions(-) diff --git a/Dockerfile b/Dockerfile index 23e5984..551946e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ RUN apk add openssl WORKDIR /certs RUN openssl req -nodes -new -x509 -subj="/C=US/ST=Denial/L=springfield/O=Dis/CN=localhost" -keyout server.key -out server.cert -FROM node:lts-alpine3.13 +FROM node:lts-alpine3.15 COPY --from=certbuilder /certs/ /certs COPY ./package.* /server/ RUN cd /server && npm install --production @@ -11,6 +11,6 @@ COPY ./css /server/css/ COPY ./views /server/views/ COPY ./static /server/static/ COPY ./mds /server/mds/ -COPY ./server.js /server/ +COPY ./*.js /server/ ENTRYPOINT ["/server/server.js"] EXPOSE 9000 diff --git a/docker-compose-test.yaml b/docker-compose-test.yaml index 243b282..fceb9b9 100644 --- a/docker-compose-test.yaml +++ b/docker-compose-test.yaml @@ -1,11 +1,11 @@ version: "3.7" services: - web: - image: web + blog: + image: blog build: - context: ./ + context: . networks: - - webnet + - blognet - dbnet ports: - "19009:9000" @@ -16,30 +16,33 @@ services: - SERVER_LISTEN_PORT=9000 depends_on: - mongo + secrets: + - mongo_user + - mongo_pass mongo: image: mongo:5.0 networks: - dbnet restart: on-failure ports: - - "27117:27017" + - "127.0.0.1:27117:27017" - "127.0.0.1:27118:27018" - "127.0.0.1:27119:27019" volumes: - - db-data:/data/db - env: + - blog-data:/data/db + environment: - MONGO_INITDB_ROOT_USERNAME_FILE=/run/secrets/mongo_user - MONGO_INITDB_ROOT_PASSWORD_FILE=/run/secrets/mongo_pass secrets: - mongo_user - mongo_pass networks: - webnet: + blognet: dbnet: volumes: - db-data: + blog-data: secrets: mongo_user: - file: ./mongo_user + file: ./mongo_secrets/mongo_user mongo_pass: - file: ./mongo_pass + file: ./mongo_secrets/mongo_pass diff --git a/docker-compose.yaml b/docker-compose.yaml index 5f3e217..79e5d0c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,11 +1,12 @@ version: "3.7" services: - web: - image: web + blog: + image: blog build: context: ./ networks: - - webnet + - blognet + - dbnet restart: unless-stopped ports: - "9000:9000" @@ -16,5 +17,35 @@ services: environment: - SERVER_DEPLOYMENT_TYPE=deployment - SERVER_LISTEN_PORT=9000 + depends_on: + - mongo + secrets: + - mongo_user + - mongo_pass + mongo: + image: mongo:5.0 + networks: + - dbnet + restart: on-failure + ports: + - "127.0.0.1:27117:27017" + - "127.0.0.1:27118:27018" + - "127.0.0.1:27119:27019" + volumes: + - blog-data:/data/db + environment: + - MONGO_INITDB_ROOT_USERNAME_FILE=/run/secrets/mongo_user + - MONGO_INITDB_ROOT_PASSWORD_FILE=/run/secrets/mongo_pass + secrets: + - mongo_user + - mongo_pass networks: - webnet: + blognet: + dbnet: +volumes: + blog-data: +secrets: + mongo_user: + file: ./mongo_secrets/mongo_user + mongo_pass: + file: ./mongo_secrets/mongo_pass diff --git a/model.js b/model.js index 7eba2a4..d4bfc83 100644 --- a/model.js +++ b/model.js @@ -1,22 +1,30 @@ "use strict"; +const fs = require("fs"); +const path = require("path"); const mongoose = require("mongoose"); +const Schema = mongoose.Schema; mongoose.Promise = global.Promise; const db = {}; db.mongoose = mongoose; -db.url = "mongo:27017"; +const db_pass = fs.readFileSync("/run/secrets/mongo_pass").toString(); +const db_user = fs.readFileSync("/run/secrets/mongo_user").toString(); +db.url = "mongodb://" + db_user + ":" + db_pass + "@mongo:27017"; -const blogPostModel = mongoose.model( - "blogPost", - mongoose.Schema( - { - title: String, - description: String, - published: Boolean, - }, - { timestamps: true } - ) -); +const BlogPostSchema = new Schema({ + title: { type: String, required: true, trim: true }, + slug: { type: String, required: true, lowercase: true, trim: true }, + body: { type: String, required: true }, + teaser: { type: String, required: true }, + keywords: { type: Array, required: true }, + lastUpdatedAt: { type: Number }, +}); + +// a simple hook to update the timestamp(lastUpdatedAt) on update +BlogPostSchema.pre("save", function (next) { + this.lastUpdatedAt = Date.now(); + next(); +}); function dbInit() { db.mongoose @@ -29,4 +37,25 @@ function dbInit() { process.exit(1); }); } -module.exports = dbInit; + +function populateDB(model) { + let filePaths = fs.readdirSync(path.join(__dirname, "mds")); + filePaths.forEach((fileName) => { + let fileContent = fs + .readFileSync(path.join(__dirname, "mds", fileName), "utf-8") + .toString(); + let newBlogPost = new model({ + title: fileName, + slug: "slug", + body: fileContent, + teaser: "teaser", + keywords: ["kw1", "kw2"], + }); + newBlogPost.save(); + }); +} +module.exports = { + blogPost: mongoose.model("BlogPost", BlogPostSchema), + dbInit: dbInit, + populateDB: populateDB, +}; diff --git a/server.js b/server.js index a42072f..bd23a1f 100755 --- a/server.js +++ b/server.js @@ -22,15 +22,23 @@ const mit = require("markdown-it")({ html: true }) const spdy = require("spdy"); const helmet = require("helmet"); const morgan = require("morgan"); -const pug = require("pug"); +// const pug = require("pug"); +const model = require("./model"); + +model.dbInit(); +model.populateDB(model.blogPost); const app = express(); app.disable("x-powered-by"); app.use(express.static(path.join(__dirname, "css"))); app.use(express.static(path.join(__dirname, "static"))); app.set("views", path.join(__dirname, "views")); +// app.set("view engine", "ejs"); +// app.engine("ejs", require("ejs").__express); +// app.engine("pug", require("pug").renderFilej); +// app.engine("pug", "pug"); app.set("view engine", "ejs"); -app.engine("ejs", require("ejs").__express); +app.set("view engine", "pug"); app.use(helmet.crossOriginEmbedderPolicy()); app.use(helmet.crossOriginOpenerPolicy()); @@ -85,7 +93,7 @@ function renderAndSend(req, res) { ); //FIXME-this can obviously fail readStream.on("data", (chunk) => { - res.render("index", { + res.render("index.ejs", { cache: true, data: { blogHttp: mit.render(chunk), @@ -111,7 +119,7 @@ app.get("/about", (req, res) => { app.get("/archive", (req, res) => { res.type("text/html"); - res.render("archive", { + res.render("archive.ejs", { cache: true, data: { mds: fs.readdirSync(path.join(__dirname, "mds"), "utf-8"), @@ -129,11 +137,19 @@ app.get("/robots.txt", (req, res) => { }); app.get("/rss/feed", (req, res) => { - const compiledFunction = pug.compileFile("./views/rss_feed.pug"); - const files = fs.readdirSync(path.join(__dirname, "mds")); - for (const file of files) { - res.send(compiledFunction(file)); - } + model.blogPost + .find({}) + .sort("-lastUpdatedAt") + .select("title slug lastUpdatedAt teaser") + .exec(function (err, posts) { + if (err) return err; + return res.render("rss_feed.pug", { cache: true, posts: posts }); + }); + // const compiledFunction = pug.compileFile("./views/rss_feed.pug"); + // const files = fs.readdirSync(path.join(__dirname, "mds")); + // for (const file of files) { + // res.send(compiledFunction(file)); + // } }); app.get("/$", (req, res) => { diff --git a/static/about.html b/static/about.html index b6bdfa9..a56ad24 100644 --- a/static/about.html +++ b/static/about.html @@ -25,5 +25,11 @@ here .

+
+
+
+
+ Home +
diff --git a/views/index.ejs b/views/index.ejs index 6cc3a45..0e47f53 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -6,7 +6,7 @@ Blog - + @@ -15,6 +15,12 @@


+ Keywords: + <%- data.Keywords %> +
+
+
+
Archive About     diff --git a/views/rss_feed.pug b/views/rss_feed.pug index 9bdd9bd..0ae2b74 100644 --- a/views/rss_feed.pug +++ b/views/rss_feed.pug @@ -1,21 +1,21 @@ doctype xml rss(version='2.0', xmlns:atom='http://www.w3.org/2005/Atom') channel - title DevSmash - link http://devsmash.com - atom:link(href='http://devsmash.com/feed/rss', rel='self', type='application/rss+xml') - description Developers talking about stuff that developers like to talk about. + title deviblog + link https://blog.terminaldweller.com + atom:link(href='https://blog.terminaldweller.com/feed/rss', rel='self', type='application/rss+xml') + description I talk about software here. language en-US if posts.length - lastBuildDate= new Date(posts[0].publishedAt).toUTCString() + lastBuildDate= new Date(posts[0].lastUpdatedAt).toUTCString() each post in posts item title= post.title - link http://devsmash.com/blog/#{post.slug} + link https://terminaldweller.com/blog/#{post.slug} description | http://devsmash.com/blog/#{post.slug}')!= 'Read more »' + p: a(href='https://blog.terminaldweller.com/blog/#{post.slug}') 'Read more »' | ]]> - pubDate= new Date(post.publishedAt).toUTCString() - guid(isPermaLink='false') Date: Wed, 6 Jul 2022 14:31:50 +0430 Subject: now the blog uses the db for everything. now need to decide on how to populate the DB and prevent duplication --- model.js | 4 +-- server.js | 93 +++++++++++++++++++++++++++++++------------------------ views/archive.ejs | 4 +-- views/index.ejs | 3 +- 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/model.js b/model.js index d4bfc83..5df1270 100644 --- a/model.js +++ b/model.js @@ -46,9 +46,9 @@ function populateDB(model) { .toString(); let newBlogPost = new model({ title: fileName, - slug: "slug", + slug: fileName, body: fileContent, - teaser: "teaser", + teaser: fileName, keywords: ["kw1", "kw2"], }); newBlogPost.save(); diff --git a/server.js b/server.js index bd23a1f..4c2be0c 100755 --- a/server.js +++ b/server.js @@ -22,7 +22,6 @@ const mit = require("markdown-it")({ html: true }) const spdy = require("spdy"); const helmet = require("helmet"); const morgan = require("morgan"); -// const pug = require("pug"); const model = require("./model"); model.dbInit(); @@ -33,10 +32,6 @@ app.disable("x-powered-by"); app.use(express.static(path.join(__dirname, "css"))); app.use(express.static(path.join(__dirname, "static"))); app.set("views", path.join(__dirname, "views")); -// app.set("view engine", "ejs"); -// app.engine("ejs", require("ejs").__express); -// app.engine("pug", require("pug").renderFilej); -// app.engine("pug", "pug"); app.set("view engine", "ejs"); app.set("view engine", "pug"); @@ -79,31 +74,29 @@ async function enumerateDir() { return await fs.readdirSync(path.join(__dirname, "mds")); } -function renderAndSend(req, res) { - try { - let viewPath; - if (req.path == "/") { - viewPath = "mds/cstruct2luatable.md"; - } else { - viewPath = req.path; - } - let readStream = fs.createReadStream( - path.join(__dirname, viewPath), - "utf-8" - ); - //FIXME-this can obviously fail - readStream.on("data", (chunk) => { - res.render("index.ejs", { +function renderAndSend_v2(req, res) { + model.blogPost + .findOne( + { _slug: req.path }, + { + projection: { + _id: 0, + title: 0, + teaser: 0, + }, + } + ) + .exec(function (err, blogPost) { + if (err) return err; + return res.render("index.ejs", { cache: true, data: { - blogHttp: mit.render(chunk), - mds: fs.readdirSync(path.join(__dirname, "mds"), "utf-8"), + blogHttp: mit.render(blogPost.body), + lastUpdatedAt: blogPost.lastUpdatedAt, + keywords: blogPost.keywords, }, }); }); - } catch (err) { - console.log(err); - } } app.get("/health", (req, res) => { @@ -119,12 +112,23 @@ app.get("/about", (req, res) => { app.get("/archive", (req, res) => { res.type("text/html"); - res.render("archive.ejs", { - cache: true, - data: { - mds: fs.readdirSync(path.join(__dirname, "mds"), "utf-8"), - }, - }); + model.blogPost + .find({}, { _id: 0, body: 0, teaser: 0, keywords: 0, lastUpdatedAt: 0 }) + .exec(function (err, blogPosts) { + if (err) return err; + res.render("archive.ejs", { + cache: true, + data: { + blogPosts: blogPosts, + }, + }); + }); + // res.render("archive.ejs", { + // cache: true, + // data: { + // mds: fs.readdirSync(path.join(__dirname, "mds"), "utf-8"), + // }, + // }); }); app.get("/robots.txt", (req, res) => { @@ -145,22 +149,31 @@ app.get("/rss/feed", (req, res) => { if (err) return err; return res.render("rss_feed.pug", { cache: true, posts: posts }); }); - // const compiledFunction = pug.compileFile("./views/rss_feed.pug"); - // const files = fs.readdirSync(path.join(__dirname, "mds")); - // for (const file of files) { - // res.send(compiledFunction(file)); - // } }); app.get("/$", (req, res) => { - renderAndSend(req, res); + model.blogPost + .find({}, { projection: { _id: 0, title: 0, teaser: 0 } }) + .limit(1) + .sort({ $natural: -1 }) + .exec(function (err, blogPost) { + if (err) return err; + return res.render("index.ejs", { + cache: true, + data: { + blogHttp: mit.render(blogPost[0].body), + lastUpdatedAt: blogPost[0].lastUpdatedAt, + keywords: blogPost[0].keywords, + }, + }); + }); }); -app.get("/mds/:mdname$", (req, res) => { - if (req.params["mdname"] == "") { +app.get("/posts/:postName", (req, res) => { + if (req.params["postName"] == "") { res.write("nothing requested!"); } - renderAndSend(req, res); + renderAndSend_v2(req, res); }); app.use(sitemap(enumerateDir, "https://blog.terminaldweller.com")); diff --git a/views/archive.ejs b/views/archive.ejs index 36191de..72b963d 100644 --- a/views/archive.ejs +++ b/views/archive.ejs @@ -9,9 +9,9 @@
- <% data.mds.forEach(function(md) { %> + <% data.blogPosts.forEach(function(blogPost) { %> <% }) %>
diff --git a/views/index.ejs b/views/index.ejs index 0e47f53..fdd49cc 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -16,7 +16,8 @@

Keywords: - <%- data.Keywords %> + <%- data.keywords %> + <%- data.lastUpdated %>


-- cgit v1.2.3 From fc713bcbb320ff692cac6332f5aa587dc817305c Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Jul 2022 15:54:39 +0430 Subject: WIP, we now have to fix the pug template which is not working --- model.js | 1 + put_in_db.js | 26 ++++++++++++++++++++++++++ server.js | 6 ------ views/rss_feed.pug | 2 +- 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 put_in_db.js diff --git a/model.js b/model.js index 5df1270..dde3913 100644 --- a/model.js +++ b/model.js @@ -54,6 +54,7 @@ function populateDB(model) { newBlogPost.save(); }); } + module.exports = { blogPost: mongoose.model("BlogPost", BlogPostSchema), dbInit: dbInit, diff --git a/put_in_db.js b/put_in_db.js new file mode 100644 index 0000000..da5a050 --- /dev/null +++ b/put_in_db.js @@ -0,0 +1,26 @@ +"use strict"; +// mongosh --host 127.0.0.1 --port 27117 -u mongo -p mongo -f put_in_db.js + +const fs = require("fs"); +const path = require("path"); +// db = connect("http://mongo:mongo@127.0.0.1:27117"); + +blog_entry_1 = { + title: "Turning C structs into Lua tables", + slug: "c_struct_lua_table", + body: fs.readFileSync(path.join(__dirname) + "/mds/cstruct2luatable.md"), + teaser: "Turning C structures into Lua tables", + keywords: "c,lua", + lastUpdatedAt: Date.now(), +}; + +blog_entry_2 = { + title: "Lazy Makefiles", + slug: "lazy_makefile", + body: fs.readFileSync(path.join(__dirname) + "/mds/lazymakefiles.md"), + teaser: "Lazy Makefiles", + keywords: "makefile,c,c++", + lastUpdatedAt: Date.now(), +}; + +db.blogposts.insertMany([blog_entry_1, blog_entry_2]); diff --git a/server.js b/server.js index 4c2be0c..6d51d1b 100755 --- a/server.js +++ b/server.js @@ -123,12 +123,6 @@ app.get("/archive", (req, res) => { }, }); }); - // res.render("archive.ejs", { - // cache: true, - // data: { - // mds: fs.readdirSync(path.join(__dirname, "mds"), "utf-8"), - // }, - // }); }); app.get("/robots.txt", (req, res) => { diff --git a/views/rss_feed.pug b/views/rss_feed.pug index 0ae2b74..e75225d 100644 --- a/views/rss_feed.pug +++ b/views/rss_feed.pug @@ -18,4 +18,4 @@ rss(version='2.0', xmlns:atom='https://blog.terminaldweller.com/blog/#{post.slug}') 'Read more »' | ]]> pubDate= new Date(post.lastUpdatedAt).toUTCString() - guid(isPermaLink='false') https://blog.terminaldweller.com/blog/#{post.slug} -- cgit v1.2.3 From c9f5b12ab281c8889364bfb2455dc97fdb92e9f9 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Jul 2022 16:23:42 +0430 Subject: a newer simpler template for the rss feed --- server.js | 4 ++-- views/rss_feed_v2.pug | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 views/rss_feed_v2.pug diff --git a/server.js b/server.js index 6d51d1b..286932d 100755 --- a/server.js +++ b/server.js @@ -25,7 +25,7 @@ const morgan = require("morgan"); const model = require("./model"); model.dbInit(); -model.populateDB(model.blogPost); +// model.populateDB(model.blogPost); const app = express(); app.disable("x-powered-by"); @@ -141,7 +141,7 @@ app.get("/rss/feed", (req, res) => { .select("title slug lastUpdatedAt teaser") .exec(function (err, posts) { if (err) return err; - return res.render("rss_feed.pug", { cache: true, posts: posts }); + return res.render("rss_feed_v2.pug", { cache: true, posts: posts }); }); }); diff --git a/views/rss_feed_v2.pug b/views/rss_feed_v2.pug new file mode 100644 index 0000000..92c9a87 --- /dev/null +++ b/views/rss_feed_v2.pug @@ -0,0 +1,21 @@ +doctype xml +rss(version='2.0') + channel + title deviblog + link https://blog.terminaldweller.com + description I talk about software here. + managingEditro devi@terminaldweller.com + webMaster devi@terminaldweller.com + language en-US + if posts.length + lastBuildDate= new Date(posts[0].lastUpdatedAt).toUTCString() + each post in posts + item + title= post.title + link https://blog.terminaldweller.com/posts/#{post.slug} + description + | + pubDate= new Date(post.lastUpdatedAt).toUTCString() + guid(isPermaLink='false') https://blog.terminaldweller.com/blog/#{post.slug} -- cgit v1.2.3 From 4302411fb080456ba5047a2b98700ae52a0fad29 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Jul 2022 16:58:04 +0430 Subject: fixed rss validator errors except atom error --- server.js | 19 ++++++++++--------- views/rss_feed_v2.pug | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index 286932d..b609018 100755 --- a/server.js +++ b/server.js @@ -74,10 +74,10 @@ async function enumerateDir() { return await fs.readdirSync(path.join(__dirname, "mds")); } -function renderAndSend_v2(req, res) { +function renderAndSend_v2(req, res, slug) { model.blogPost .findOne( - { _slug: req.path }, + { slug: slug }, { projection: { _id: 0, @@ -135,6 +135,7 @@ app.get("/robots.txt", (req, res) => { }); app.get("/rss/feed", (req, res) => { + res.type("application/rss+xml"); model.blogPost .find({}) .sort("-lastUpdatedAt") @@ -145,6 +146,13 @@ app.get("/rss/feed", (req, res) => { }); }); +app.get("/posts/:postName", (req, res) => { + if (req.params["postName"] == "") { + res.write("nothing requested!"); + } + renderAndSend_v2(req, res, req.params.postName); +}); + app.get("/$", (req, res) => { model.blogPost .find({}, { projection: { _id: 0, title: 0, teaser: 0 } }) @@ -163,13 +171,6 @@ app.get("/$", (req, res) => { }); }); -app.get("/posts/:postName", (req, res) => { - if (req.params["postName"] == "") { - res.write("nothing requested!"); - } - renderAndSend_v2(req, res); -}); - app.use(sitemap(enumerateDir, "https://blog.terminaldweller.com")); app.use((req, res) => { diff --git a/views/rss_feed_v2.pug b/views/rss_feed_v2.pug index 92c9a87..b680cb9 100644 --- a/views/rss_feed_v2.pug +++ b/views/rss_feed_v2.pug @@ -4,7 +4,7 @@ rss(version='2.0') title deviblog link https://blog.terminaldweller.com description I talk about software here. - managingEditro devi@terminaldweller.com + managingEditor devi@terminaldweller.com webMaster devi@terminaldweller.com language en-US if posts.length -- cgit v1.2.3 From 2c437f4ca76e107ce3eb3c93ae2fc183caf6caab Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Jul 2022 17:05:17 +0430 Subject: more fixes for the rss feed --- server.js | 1 - views/rss_feed_v2.pug | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index b609018..eea4695 100755 --- a/server.js +++ b/server.js @@ -25,7 +25,6 @@ const morgan = require("morgan"); const model = require("./model"); model.dbInit(); -// model.populateDB(model.blogPost); const app = express(); app.disable("x-powered-by"); diff --git a/views/rss_feed_v2.pug b/views/rss_feed_v2.pug index b680cb9..261f054 100644 --- a/views/rss_feed_v2.pug +++ b/views/rss_feed_v2.pug @@ -1,11 +1,12 @@ doctype xml -rss(version='2.0') +rss(version='2.0', xmlns:atom='http://www.w3.org/2005/Atom') channel title deviblog - link https://blog.terminaldweller.com + link https://blog.terminaldweller.com + atom:link(href='https://blog.terminaldweller.com/feed/rss', rel='self', type='application/rss+xml') description I talk about software here. - managingEditor devi@terminaldweller.com - webMaster devi@terminaldweller.com + managingEditor devi@terminaldweller.com (Farzad Sadeghi) + webMaster devi@terminaldweller.com (Farzad Sadeghi) language en-US if posts.length lastBuildDate= new Date(posts[0].lastUpdatedAt).toUTCString() -- cgit v1.2.3 From 07234ee01b79e5123a4718679da1467d1dc669ea Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Jul 2022 17:10:17 +0430 Subject: fixes for atom --- views/rss_feed_v2.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/rss_feed_v2.pug b/views/rss_feed_v2.pug index 261f054..76a928d 100644 --- a/views/rss_feed_v2.pug +++ b/views/rss_feed_v2.pug @@ -1,5 +1,5 @@ doctype xml -rss(version='2.0', xmlns:atom='http://www.w3.org/2005/Atom') +rss(version='2.0', xmlns:atom="http://www.w3.org/2005/Atom") channel title deviblog link https://blog.terminaldweller.com -- cgit v1.2.3 From b50e31f75ff62da85539dba72998853a34b9d6ae Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Jul 2022 17:44:19 +0430 Subject: fix for atom --- views/rss_feed_v2.pug | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/views/rss_feed_v2.pug b/views/rss_feed_v2.pug index 76a928d..d04297d 100644 --- a/views/rss_feed_v2.pug +++ b/views/rss_feed_v2.pug @@ -1,9 +1,9 @@ doctype xml -rss(version='2.0', xmlns:atom="http://www.w3.org/2005/Atom") +rss(version='2.0', xmlns:atom='http://www.w3.org/2005/Atom') channel title deviblog - link https://blog.terminaldweller.com - atom:link(href='https://blog.terminaldweller.com/feed/rss', rel='self', type='application/rss+xml') + link https://blog.terminaldweller.com + atom:link(href='https://blog.terminaldweller.com/rss/feed', rel='self', type='application/rss+xml') description I talk about software here. managingEditor devi@terminaldweller.com (Farzad Sadeghi) webMaster devi@terminaldweller.com (Farzad Sadeghi) -- cgit v1.2.3