aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-07-06 10:01:50 +0000
committerterminaldweller <thabogre@gmail.com>2022-07-06 10:01:50 +0000
commitdc57f759c616a23b1baf10b1ea71cca6ba581831 (patch)
treee641e4da50d2b616c10090b7178ec6ed86815f0d
parentWIP, adding rss to the blog (diff)
downloadblog-dc57f759c616a23b1baf10b1ea71cca6ba581831.tar.gz
blog-dc57f759c616a23b1baf10b1ea71cca6ba581831.zip
now the blog uses the db for everything. now need to decide on how to populate the DB and prevent duplication
-rw-r--r--model.js4
-rwxr-xr-xserver.js93
-rw-r--r--views/archive.ejs4
-rw-r--r--views/index.ejs3
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 @@
<body>
<script>0</script>
<div class="article">
- <% data.mds.forEach(function(md) { %>
+ <% data.blogPosts.forEach(function(blogPost) { %>
<ul>
- <li><a href=<%= "/mds/"+md %> target="_self" rel="noreferrer noopener" type="text/html"><%= md %></a></li>
+ <li><a href=<%= "/posts/"+blogPost.slug %> target="_self" rel="noreferrer noopener" type="text/html"><%= blogPost.title %></a></li>
</ul>
<% }) %>
</div>
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 @@
<hr/>
<br/>
<a class="left-footer">Keywords:</a>
- <%- data.Keywords %>
+ <%- data.keywords %>
+ <%- data.lastUpdated %>
<br/>
<br/>
<hr/>