-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
228 lines (189 loc) · 5.34 KB
/
server.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const express = require('express');
const app = express();
const dataMiddleware = require('./lib/middleware/data');
const imagesMiddleware = require('./lib/middleware/images');
const styleMiddleware = require('./lib/middleware/style');
const nunjucks = require('nunjucks');
const filters = require('./lib/filters');
const { markdownToHtml } = require('./lib/utils/markdown');
const notFoundRoute = require('./lib/routes/404');
const cookieParser = require('cookie-parser');
const renderMarkdownPageFromRoute = require('./lib/routes/page')
const contactRoute = require('./lib/routes/contact');
const fs = require('fs');
const path = require('path');
//network monitoring & rate limiting
const morgan = require('morgan');
const compression = require('compression');
const rateLimit = require('express-rate-limit');
//trust 1 proxy
app.set('trust proxy', 1);
// Enable request logging
app.use(morgan('combined'));
// Enable Gzip compression
app.use(compression());
// Apply rate limiting
app.use(rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 1000 // limit each IP to 1000 requests per windowMs
}));
//load cookie parser
app.use(cookieParser());
// Configure Nunjucks
const env = nunjucks.configure('src', {
autoescape: true,
express: app
});
app.set('view engine', 'njk');
Object.keys(filters).forEach(filterName => {
env.addFilter(filterName, filters[filterName]);
});
// Parse URL encoded bodies
app.use(express.urlencoded({ extended: true }));
// Serve static files
const staticDirectories = [
path.join('./src/assets/static'),
path.join('./collections')
];
staticDirectories.forEach(staticDir => {
app.use(express.static(staticDir));
});
//load global data
app.use(dataMiddleware);
//load userStyles
app.use(styleMiddleware);
//handle image requests
app.use('/images', imagesMiddleware);
//load collections
app.use((req, res, next) => {
try {
res.locals.collections = {
articles: require('./collections/articles.json'),
tags: require('./collections/tags.json'),
kanga: require('./collections/kanga.json'),
kangaExamples: require('./collections/kanga-examples.json')
};
} catch (error) {
console.error("Failed to load collections:", error);
// Handle errors or set default empty objects
res.locals.collections = {
articles: [],
tags: [],
kanga: [],
kangaExamples: {}
};
}
next();
});
/*
--- routes ---
*/
app.get('/', (req, res) => {
renderMarkdownPageFromRoute(req, res, 'home');
});
app.get('/articles', (req,res) => {
res.render('views/articles', {
title: 'Articles',
section_id: 'articles'
});
});
app.get('/articles/:slug', (req,res) => {
const slug = req.params.slug;
const article = res.locals.collections.articles.find(item => item.slug === slug);
if (!article) {
return notFoundRoute(res,req);
}
res.render('views/article', {
section_id: 'articles',
...article,
breadcrumbs: [{
title: 'Articles',
url: '/articles'
}],
showContactInvite: true
});
});
//search through multi nested objects, like kanga.json
function findEntryBySlug(slug, data) {
for (const section of data) {
const foundItem = section.items.find(item => item.slug === slug);
if (foundItem) {
return {
section: section.title,
...foundItem
};
}
}
return null; // Return null if no matching entry is found
}
app.get('/kanga/:slug', (req,res) => {
const slug = req.params.slug;
const entry = findEntryBySlug(slug, res.locals.collections.kanga)
if (!entry) {
return notFoundRoute(res,req);
}
res.render('views/kanga', {
section_id: 'kanga',
showBreadcrumbs: true,
...entry
});
});
app.get('/kanga/example/:section/:slug', (req,res) => {
const section = req.params.section;
const slug = req.params.slug;
const entry = res.locals.collections.kangaExamples[section].find(item => item.slug === slug)
let renderedContent = ''
if(entry.type === "njk") {
renderedContent = env.renderString(entry.content);
}
if(entry.type === "md") {
renderedContent = markdownToHtml(entry.content);
}
res.render('views/kanga-example', {
section_id: 'kanga-example',
...entry,
title: `${entry.title} - Example `,
content: renderedContent,
locals: res.locals
})
})
app.get('/subjects', (req,res) => {
res.render('views/tags', {
title: 'Subjects',
section_id: 'subjects'
});
});
app.get('/subjects/:slug', (req,res) => {
const slug = req.params.slug;
const entry = res.locals.collections.tags.find(item => item.slug === slug)
if (!entry) {
return notFoundRoute(res,req);
}
res.render('views/tag', {
title: entry.title,
section_id: 'subjects',
posts: entry.posts,
breadcrumbs: [{
title: 'Subjects',
url: '/subjects'
}]
});
});
// handle forms
//--------------
app.post('/time-travel', (req,res) => {
const { style } = req.body;
res.cookie('userStyle', style, { maxAge: 2592000000, httpOnly: true });
res.redirect('/time-travel?updated=true');
});
app.post('/contact',(res, req) => {
contactRoute(res, req);
})
//serve top level pages
app.get('/:page', (req, res) => {
renderMarkdownPageFromRoute(req, res);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Roobottom-com is running at: http://localhost:${PORT}`);
});