-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
69 lines (60 loc) · 1.53 KB
/
server.ts
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
import fastify from 'fastify';
import socketioServer from 'fastify-socket.io';
import {getFullTextArticle} from './lib/diffbot';
const server = fastify({logger: true});
server.register(socketioServer);
const articleSchema = {
schema: {
querystring: {
feedId: {type: 'string'},
id: {type: 'string'},
url: {
type: 'string',
format: 'uri',
pattern: '^(https?|http)://',
},
},
},
};
interface articleType {
Querystring: {
feedId: string;
id: string;
url: string;
};
}
server.get<articleType>('/v1/article', articleSchema, async (request, reply) => {
const {url} = request.query;
const data = await getFullTextArticle(url);
if (data.error) {
if (typeof data.errorCode === 'number') {
reply.statusCode = data.errorCode;
}
return new Error(data.error);
}
return data;
});
server.get<articleType>('/v1/socket/article', articleSchema, async (request) => {
const {feedId, id, url} = request.query as any;
getFullTextArticle(url).then((data) => {
if (!data.error && data.objects) {
server.io.emit('fulltextArticle', {
feedId,
id,
url,
data: data.objects[0],
});
}
});
});
server.get('/', async () => {
return {hello: 'world'};
});
server.listen(process.env.PORT || 3000, '0.0.0.0', (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
server.io.on('connect', (socket: any) => console.log('Socket connected!', socket.id));
console.log(`Server listening at ${address}`);
});