-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (30 loc) 路 857 Bytes
/
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
const fastify = require('fastify')();
const { isCURL, hl, hlCLI } = require('./util');
const axios = require('axios').default;
fastify.get('*', async (req, reply) => {
const param = req.params['*'].slice(1);
if (!param) {
reply.redirect('https://github.com/qingwei-li/highlightext');
}
let url = /\./g.test(param) ? param : `//text.cinwell.xyz/${param}`;
if (!/^http(s):/.test(url)) {
if (!/^\/\//.test(url)) {
url = 'http://' + url;
} else {
url = 'http:' + url;
}
}
const code = (await axios(url)).data;
let text;
if (!isCURL(req)) {
text = hl(code);
reply.header('content-type', 'text/html');
} else {
text = hlCLI(code);
}
reply.send(text);
});
fastify.listen(4931, '0.0.0.0', err => {
if (err) throw err;
console.log(`server listening on ${fastify.server.address().port}`);
});