-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (83 loc) · 2.7 KB
/
index.js
File metadata and controls
97 lines (83 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require("dotenv").config();
const debug = require("debug")("hellomeinte:main");
const error = require("debug")("hellomeinte:error");
const GhostContentAPI = require("@tryghost/content-api");
const handleBars = require("handlebars");
const express = require("express");
const exphbs = require("express-handlebars");
const ghostUtils = require("./ghost-utils");
const hbsHelpers = require("./handlebars-helpers");
const portfolioContentIds = require("./config/portfolio-content-ids.json");
const API_KEY = process.env.GHOST_API_KEY;
const PORT = process.env.WWWPORT;
const IS_PRODUCTION = process.env.NODE_ENV === "production";
debug("starting..");
const app = express();
app.engine("hbs", exphbs({ defaultLayout: "main", extname: ".hbs" }));
app.set("view engine", "hbs");
app.use(express.static("public"));
hbsHelpers(handleBars);
const api = new GhostContentAPI({
url: "https://blog.hellomeinte.com",
key: API_KEY,
version: "v2"
});
const projectNotFound = res => {
res.status(400);
res.render("error", {
error_msg: "Bad request, please provide a valid project URL..."
});
};
const generalError = (err, res) => {
error(err);
res.status(500);
res.render("error", {
error_msg: `<p>Something went wrong...</p><p>${err.message}</p>`
});
};
//hard caching of content, never invalidates unless service is restarted.
let cachedContent = {};
app.get("/", function(req, res) {
if (cachedContent["root"] && IS_PRODUCTION) {
debug("using cached content");
res.render("home", cachedContent["root"]);
return;
}
const start = new Date();
ghostUtils
.grabPortfolio(api, portfolioContentIds)
.then(content => {
content.clients = ghostUtils.bundleContent(content, "client");
content.timelines = ghostUtils.bundleContent(content, "Timeline");
return content;
})
.then(content => {
debug("mapped content: ", content);
debug("request duration ", new Date() - start);
cachedContent["root"] = content;
res.render("home", content);
})
.catch(err => generalError(err, res));
});
app.get("/projects/*", function(req, res) {
const projectId = req.url.split("/projects/")[1];
if (cachedContent[projectId] && IS_PRODUCTION) {
debug("using cached content");
res.render("project", cachedContent[projectId]);
return;
}
if (!projectId.length) {
return projectNotFound(res);
}
ghostUtils
.grabProject(api, projectId)
.then(page => {
if (!page || !page.html) {
throw new Error(`Project not found with ID: ${projectId}`);
}
cachedContent[projectId] = page;
res.render("project", page);
})
.catch(err => generalError(err, res));
});
app.listen(PORT, () => debug(`Listening on port ${PORT}!`));