-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
182 lines (151 loc) · 5.07 KB
/
api.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
import express from 'express';
import cors from 'cors';
import { SNIPPETS_ROOT } from './gather-snippets/constants.js';
import comments from './public/data/comments.json' assert { type: 'json' };
import langs from './public/data/langs.json' assert { type: 'json' };
import links from './public/data/links.json' assert { type: 'json' };
import snippets from './public/data/snippets.json' assert { type: 'json' };
import snips from './public/data/snips.json' assert { type: 'json' };
import tags from './public/data/tags.json' assert { type: 'json' };
import { readSnippets } from './gather-snippets/read-snippets.js';
const rawSnippets = new Proxy(
(await readSnippets(SNIPPETS_ROOT)).snippets.reduce(
(all, next) => ({ ...all, [next.title.toLowerCase()]: next.text }),
{},
),
{
get(target, key) {
return key.toLowerCase() in target
? target[key.toLowerCase()]
: target['mijn-kat.txt'];
},
},
);
// --- utils ---
const includes = (str = '', query = '') => str?.toLowerCase().includes(query);
// --- server setup ---
const app = express();
app.use(cors());
app.use((req, _, next) => (console.log(`${req.method} ${req.url}`), next()));
app.use(express.static('./'));
// --- define routes ---
app.get('/api', (_, res) => {
res.set('Content-Type', 'text/plain');
res.send(`snippetry endpoints:
/api/comments ? search={query}
/api/langs ? search={query}
/api/links ? search={query}
? title={query}
? from={query}
& to={query}
& type={query}
/api/snippets ? search={query}
& tags={tag,tag,...}
? title={file-name.ext}
/ {file-name.ext}
/api/snips ? search={query}
/api/tags ? search={query}
/api/* - ik stuur mijn kat -`);
});
app.get('/api/comments', serveOther(comments));
app.get('/api/snippets', serve(snippets));
app.get('/api/snippets/:fileName', serveRawSnippets);
app.get('/api/links', serveLinks(links));
app.get('/api/snips', serveOther(snips));
app.get('/api/tags', serveOther(tags));
app.get('/api/langs', serveOther(langs));
app.get('/api/*', stuurMijnKat);
// --- start the server ---
const port = process.argv[2] || 4567;
app.listen(port, () => {
console.log(`Snippetry is running on port ${port}`);
});
// ---- define handlers ----
function serve(ppets) {
return function serveSnippets(req, res) {
const tags = req.query.tags?.split(',').filter((tag) => tag) || [];
const search = req.query.search?.toLowerCase() || '';
if (req.query.title) {
const search = ppets.find(
(snippet) => snippet.title.toLowerCase() === req.query.title.toLowerCase(),
);
res.send(search ? search : []);
return;
}
const filteredSnippets = ppets
.filter((snippet) =>
tags.length === 0 ? true : snippet.tags?.some((tag) => tags.includes(tag)),
)
.filter(
(snippet) =>
snippet.title.toLowerCase().includes(search) ||
snippet.text.toLowerCase().includes(search),
);
res.send(filteredSnippets);
};
}
function serveRawSnippets(req, res) {
const snippet = rawSnippets[req.params.fileName];
res.set('Content-Type', 'text/plain');
if (snippet) {
res.send(snippet);
} else {
stuurMijnKat(req, res);
}
}
function serveOther(strings = ['']) {
return function serveData(req, res) {
const search = req.query.search?.toLowerCase() || '';
const filtered = strings.filter((string) => string.toLowerCase().includes(search));
res.send(filtered);
};
}
function serveLinks(links = []) {
return (req, res) => {
if (req.query.search) {
const search = req.query.search;
res.send(
links.filter(
({ from, to, type }) =>
includes(from, search) || includes(to, search) || includes(type, search),
),
);
} else if (req.query.title) {
const snippet = req.query.title.toLowerCase();
res.send(
links.filter(
({ from, to }) =>
snippet === from.toLowerCase() || snippet === to.toLowerCase(),
),
);
} else if (req.query.from || req.query.to || req.query.type) {
const fromQuery = req.query.from?.toLowerCase();
const toQuery = req.query.to?.toLowerCase();
const typeQuery = req.query.type?.toLowerCase();
res.send(
links.filter(
({ from, to, type }) =>
(fromQuery ? includes(from, fromQuery) : true) &&
(toQuery ? includes(to, toQuery) : true) &&
(typeQuery ? includes(type, typeQuery) : true),
),
);
} else {
res.send(links);
}
};
}
const mijnKat = rawSnippets['mijn-kat.txt']
.split('\n')
.map((line) => ` ${line}`)
.join('\n');
async function stuurMijnKat(_, res) {
// https://www.vlaanderen.be/team-taaladvies/taaladviezen/zijn-kat-sturen
console.log('kat gestuurd');
res.set('Content-Type', 'text/plain');
// https://user.xmission.com/~emailbox/ascii_cats.htm
// -Skorch
res.status(404);
res.send(`404: kat gestuurd \n\n\n${mijnKat}`);
}
// tags: coAIthored