-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
105 lines (88 loc) · 2.99 KB
/
main.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
require('dotenv').config()
const express = require('express');
const app = express();
const PORT = 8080;
const http = require('http');
const server = http.createServer(app);
const bodyParser = require('body-parser');
const { Deta } = require("deta")
const deta = Deta(process.env.DETA_PROJECT_KEY)
const db = deta.Base("shortened_urls")
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/', express.static(__dirname + '/public', {
extensions: ['html']
}));
app.post('/new_url', (req, resp) => {
let query = req.body;
checkURL = () => {
if (query.shares >= 1) {
let protocol_ok = query.url.startsWith("http://") || query.url.startsWith("https://") || query.url.startsWith("ftp://");
if (query.url != '' && query.address != '' && protocol_ok) {
getRandom();
}
}
};
getRandom = async () => {
let hash = "";
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 5; i++)
hash += possible.charAt(Math.floor(Math.random() * possible.length));
try {
await db.insert({ ...query }, hash)
resp.send({ success: true, hash });
} catch (e) {
console.log(e);
getRandom();
}
};
checkURL();
});
app.post('/redirect', async (req, resp) => {
const data = req.body;
try {
const item = await db.get(data.hash);
if (process.env.SKIP_MINING) resp.send({ success: true, data_to_redirect: { ...item, shares: 0 } });
if (item) {
resp.send({ success: true, data_to_redirect: { ...item, url: 'Not yet' } });
} else {
resp.send({ success: false, error: 'Error trying to fecth: ' + data.hash });
console.log('Error trying to fecth: ', data.hash);
}
} catch (e) {
console.log(e);
resp.send({ success: false, error });
console.log('Error trying to fecth: ', data.hash);
}
});
app.post('/share_found', async (req, resp) => {
const data = req.body;
try {
await db.update({ shares_mined: db.util.increment(1) }, data.hash);
const item = await db.get(data.hash);
if (item) {
if (Number(data.shares) >= Number(item.shares)) {
resp.send({ success: true, url: item.url });
}
}
} catch (e) {
console.log(e);
resp.send({ success: false, error: 'wrong_url' });
}
});
app.get('/statistics/:hash', async (req, resp) => {
const data = req.params;
try {
const item = await db.get(data.hash)
resp.send({ success: true, statistics_answer: item });
} catch (error) {
console.log(error);
resp.send({ success: false, error: 'wrong_url' });
}
});
server.listen(PORT, () => {
console.log('Server started: http://localhost:' + PORT);
});
process.on('unhandledRejection', (reason, p) => {
console.log(`Unhandled Rejection ${reason}`);
});