This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
app.js
137 lines (113 loc) · 3.94 KB
/
app.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
'use strict';
let express = require('express');
let fs = require('fs');
let app = express();
let authors = require('./authors.js');
let metadata = {all: []};
let metadataMap = {};
app.disable('x-powered-by');
for (let meta of require('./content-gen.js')) {
metadata.all.push(meta);
if (!metadata[meta.category])
metadata[meta.category] = [];
metadata[meta.category].push(meta);
metadataMap[meta.relativePath] = meta;
}
app.use('/assets', express.static('assets'));
// Legacy image paths from webcomponents.github.io
app.use('/img/stories', express.static('assets/images'));
// Used for static pages that don't show up in any other resources
app.use('/static', express.static('static',
{setHeaders: (res) => res.header('Access-Control-Allow-Origin', '*')}));
app.get('/content/*', function(request, response) {
response.header('Access-Control-Allow-Origin', '*');
let extensions = ['.md', '.html', '.html.md'];
let file = null;
for (let extension of extensions) {
if (fs.existsSync('documents/'+ request.params[0] + extension))
file = 'documents/'+ request.params[0] + extension;
}
if (!file) {
response.status(404).send('Invalid path');
return;
}
let path = fs.realpathSync(file);
let isolatedDir = fs.realpathSync('documents');
if (path.indexOf(isolatedDir) != 0) {
response.status(400).send('Invalid path');
return;
}
fs.readFile(path, 'utf8', function(err, data) {
if (err) {
response.status(404).send('Cannot find ' + err.path);
return;
}
data = data.replace(/([^]*<!-- Excerpt -->)/m, '').trim();
const metadata = metadataMap[request.params[0]];
// Return full author metadata
if (metadata.authors) {
for (let author of metadata.authors)
Object.assign(author, authors[author.id]);
}
response.setHeader('Content-Type', 'application/json');
response.status(200).send({metadata: metadata, content: data});
});
});
app.get('/author/:id', function(request, response) {
response.header('Access-Control-Allow-Origin', '*');
const id = request.params.id;
if (!authors[id]) {
response.status(404).send('Cannot find author ' + id);
return;
}
response.setHeader('Content-Type', 'application/json');
response.status(200).send(authors[id]);
});
/**
* @param {!Array.<Object>} bucket
* @param {number} offset
* @param {number} limit
* @return {Object} result
*/
function createResult(bucket, offset, limit) {
return {count: bucket.length, results: bucket.slice(offset, offset + limit)};
}
app.get('/resources', function(request, response) {
response.header('Access-Control-Allow-Origin', '*');
const offset = parseInt(request.query.offset) || 0;
const limit = parseInt(request.query.limit) || 10;
response.status(200).send(createResult(metadata.all, offset, limit));
});
app.get('/resources/author/:author', function(request, response) {
response.header('Access-Control-Allow-Origin', '*');
const offset = parseInt(request.query.offset) || 0;
const limit = parseInt(request.query.limit) || 10;
let filtered = metadata.all.filter(function(item) {
if (!item.authors)
return false;
for (let author of item.authors) {
if (author.id == request.params.author)
return true;
}
return false;
});
response.status(200).send(
createResult(filtered, offset, limit));
});
app.get('/resources/:bucket', function(request, response) {
response.header('Access-Control-Allow-Origin', '*');
if (!metadata[request.params.bucket]) {
response.status(404).send('Unknown category');
return;
}
const offset = parseInt(request.query.offset) || 0;
const limit = parseInt(request.query.limit) || 10;
response.status(200).send(
createResult(metadata[request.params.bucket], offset, limit));
});
// Start the server
let server = app.listen(process.env.PORT || '8080', function() {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
module.exports = server;