-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (58 loc) · 2.68 KB
/
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
import fastify from 'fastify';
import fastifyCors from '@fastify/cors';
import fastifyCompress from '@fastify/compress';
import { config } from './config.js';
import { loadAndFormat } from './data-formatters.js';
const app = fastify();
await app.register(fastifyCors, {
origin: true, // reflect the request origin
});
await app.register(fastifyCompress, {
encodings: ['gzip'], // gzip is twice faster than br on this low-end server
});
app.get('/homepage', async request => {
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/timelines/home?offset=${offset}`, request.headers.authorization);
});
app.get('/searchpage', async request => {
const q = request.query.q;
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/search?qs=${encodeURIComponent(q)}&offset=${offset}`, request.headers.authorization, null, null, !q);
});
app.get('/discussionspage', async request => {
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/timelines/filter/discussions?with-my-posts=yes&offset=${offset}`, request.headers.authorization);
});
app.get('/directspage', async request => {
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/timelines/filter/directs?offset=${offset}`, request.headers.authorization);
});
app.get('/userpage/:username', async request => {
const username = request.params.username;
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/timelines/${username}?offset=${offset}`, request.headers.authorization, username);
});
app.get('/usercommentspage/:username', async request => {
const username = request.params.username;
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/timelines/${username}/comments?offset=${offset}`, request.headers.authorization, username);
});
app.get('/userlikespage/:username', async request => {
const username = request.params.username;
const offset = +request.query.offset || 0;
return loadAndFormat(`${config.api.host}/v2/timelines/${username}/likes?offset=${offset}`, request.headers.authorization, username);
});
app.get('/postpage/:postId', async request => {
const postId = request.params.postId;
const maxLikes = request.query.maxLikes;
return loadAndFormat(`${config.api.host}/v2/posts/${postId}?maxComments=all&maxLikes=${maxLikes}`, request.headers.authorization, null, postId);
});
(async () => {
try {
await app.listen({ port: 3001 });
console.log(`Server listening at http://${app.server.address().address}:${app.server.address().port}`);
} catch (err) {
app.log.error(err);
process.exit(1);
}
})();