-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (63 loc) · 1.75 KB
/
index.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
const path = require('path');
const fs = require('fs');
const { fastify } = require('fastify');
const fastifyStatic = require('fastify-static');
const Monk = require('monk');
const db = Monk('localhost/passcake');
const passwords = db.get('passwords');
passwords.createIndex('expiresAt', { expireAfterSeconds: 0 });
(async () => {
const app = fastify({
ignoreTrailingSlash: true,
bodyLimit: 512 * 1024,
});
await app.register(fastifyStatic.default, {
root: path.join(__dirname, 'static'),
});
app.get(
'/password/:id',
{
schema: {
params: {
type: 'object',
properties: { id: { type: 'string' } },
required: ['id'],
},
},
},
async (req, res) => {
const html = await fs.promises.readFile(path.join(__dirname, 'static/password.html'), 'utf8');
const password = await passwords.findOne(req.params.id);
if (password) {
await res.type('text/html').send(html.replace('%data%', JSON.stringify(password)));
} else {
await res.send('Error 404 - this link is invalid or expired');
}
}
);
app.post(
'/create-password',
{
schema: {
body: {
type: 'object',
properties: {
payload: { type: 'string' },
counter: { type: 'integer' },
expiresIn: { type: 'integer' },
},
required: ['payload', 'counter', 'expiresIn'],
},
},
},
async (req, res) => {
const password = await passwords.insert({
payload: req.body.payload,
counter: req.body.counter,
expiresAt: new Date(Date.now() + req.body.expiresIn),
});
res.send(password._id.toString());
},
);
await app.listen(3000, '0.0.0.0');
})();