-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
96 lines (81 loc) · 2.32 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
const express = require('express');
const qr = require('qr-image');
const {
loadDatabase,
searchDatabase,
addMarkdown,
addSimilarItems,
} = require('./googleSpreadsheet');
const app = express();
const allScans = [];
function logScanned(uuid, fixture) {
// TRICK: fixture tells if the the uuid was found in database or not
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)) {
return;
}
const now = new Date();
if (!fixture) {
allScans.unshift({ time: now, status: 'missing', uuid });
return;
}
allScans.map((item) => {
const itm = item;
itm.status = (itm.uuid === uuid) ? 'fixed' : itm.status;
return itm;
});
allScans.unshift({ time: now, fixture, uuid });
}
app.set('view engine', 'ejs');
app.use(express.static(`${__dirname}/public`));
app.get(['/favicon.ico', '/robots.txt'], (req, res) => {
res.sendStatus(204);
});
app.get('/search', (req, res) => {
loadDatabase((allItems) => {
res.render('search', {
matches: searchDatabase(req.query, allItems)
.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)),
});
});
});
app.get('/qrlist', (req, res) => {
loadDatabase((allItems) => {
const qrList = searchDatabase(req.query, allItems)
.filter(item => item.uuid !== '')
.filter(item => item.uuid !== undefined)
.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1));
qrList.forEach((item) => {
const itm = item;
itm.qr = qr.imageSync(`http://url.coderbunker.com/${itm.uuid}`, { type: 'svg' });
return itm;
});
res.render('qrList', { matches: qrList });
});
});
app.get('/recent', (req, res) => {
res.render('recent', { allScans });
});
app.get('/:uuid', (req, res) => {
loadDatabase((allItems) => {
const match = searchDatabase(req.params, allItems)[0];
if (match === undefined) {
logScanned(req.params.uuid);
res.status(404).render('notFound', {
item: '',
id: req.params.uuid,
});
return;
}
addMarkdown(match);
addSimilarItems(match, allItems);
logScanned(req.params.uuid, match.fixture);
res.render('item', match);
});
});
app.get('/', (req, res) => {
res.render('home', {});
});
const port = process.env.PORT || 1234;
app.listen(port, () => {
console.log(`working on ${port}`);
});