-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcollection-server.js
173 lines (153 loc) · 5.06 KB
/
collection-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
const http = require('http');
const path = require('path');
const fs = require('fs');
const timeliner = process.env.TIMELINER || 'http://localhost:5000/';
http
.createServer(function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'content-type');
console.log(`${req.method} ${req.url}`);
if (req.url.slice(1, 5) === 'save') {
if (req.method !== 'POST') {
res.end();
return;
}
let body = '';
req.on('data', chunk => {
body += chunk.toString(); // convert Buffer to string
});
req.on('end', () => {
const target = req.url.replace(/save/, 'manifests');
fs.writeFileSync(__dirname + target, body);
res.writeHead(201);
res.end('Updated!');
});
return;
}
if (req.url !== '/') {
res.setHeader('Content-Type', 'application/json');
try {
res.writeHead(200);
res.end(fs.readFileSync(__dirname + req.url));
} catch (err) {
res.end();
}
return;
}
fs.readdir(path.resolve(__dirname, 'manifests'), function(err, items) {
const manifests = items
.map(item => {
try {
return {
item,
file: fs.readFileSync(path.resolve(__dirname, 'manifests', item)),
};
} catch (err) {
return null;
}
})
.filter(r => r)
.map(ret => {
const manifest = JSON.parse(ret.file);
return {
label: manifest.label.en.join(''),
summary: manifest.summary.en.join(''),
url: `http://localhost:5001/manifests/${ret.item}`,
callback: `http://localhost:5001/save/${ret.item}`,
};
});
res.write(renderPage(manifests));
res.end();
});
})
.listen(5001); //the server object listens on port 8080
console.log('Server started: http://localhost:5001/');
function renderPage(manifests) {
return `
<html lang="en" class="mdl-js">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&lang=en">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.teal-red.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<title>Collection server</title>
<style>
.demo-ribbon {
width: 100%;
height: 40vh;
background-color: #3F51B5;
-webkit-flex-shrink: 0;
-ms-flex-negative: 0;
flex-shrink: 0;
}
.demo-main {
margin-top: -35vh;
-webkit-flex-shrink: 0;
-ms-flex-negative: 0;
flex-shrink: 0;
}
.demo-header .mdl-layout__header-row {
padding-left: 40px;
}
.demo-container {
max-width: 1600px;
width: calc(100% - 16px);
margin: 0 auto;
}
.demo-content {
border-radius: 2px;
padding: 20px 40px 40px 40px;
margin-bottom: 40px;
}
.demo-layout.is-small-screen .demo-content {
padding: 40px 28px;
}
.demo-content h3 {
margin-top: 48px;
}
.demo-footer {
padding-left: 40px;
}
.demo-footer .mdl-mini-footer--link-list a {
font-size: 13px;
}
</style>
</head>
<body>
<div class="demo-layout mdl-layout mdl-layout--fixed-header mdl-js-layout mdl-color--grey-100">
<header class="demo-header mdl-layout__header mdl-layout__header--scroll mdl-color--grey-100 mdl-color-text--grey-800">
<div class="mdl-layout__header-row">
<span class="mdl-layout-title">Collection server</span>
<div class="mdl-layout-spacer"></div>
</div>
</header>
<div class="demo-ribbon"></div>
<main class="demo-main mdl-layout__content">
${manifests.map(renderManifest)}
</main>
</div>
</body>
</html>
`;
}
function renderManifest(manifest) {
return `
<div class="demo-container mdl-grid">
<div class="mdl-cell mdl-cell--2-col mdl-cell--hide-tablet mdl-cell--hide-phone"></div>
<div class="demo-content mdl-color--white mdl-shadow--4dp content mdl-color-text--grey-800 mdl-cell mdl-cell--8-col">
<h2>${manifest.label}</h2>
<p>
${manifest.summary}
</p>
<a href="${timeliner}#resource=${manifest.url}&callback=${manifest.callback}"
target="_blank"
class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored mdl-js-ripple-effect">
Edit this manifest
</a>
</div>
</div>
`;
}