-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (70 loc) · 2.45 KB
/
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
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
// obtendo pacotes principais
const express = require("express");
const bodyParser = require("body-parser");
const routes = require("./routes/routes.js");
const app = express();
// definindo classes:
const FirebaseDatabase = require("./models/firebase-database");
const ShortenedURLElement = require("./models/shortened-url-element");
// atualizando base
var shortenedCollection = [];
// inicializando serviços do firebase
const database = new FirebaseDatabase(
(storage) => shortenedCollection = storage ? storage : []
);
const encodeUrl = (url, hasSecurity) => {
const MIN = 1000000;
const MAX = 9999999;
serialize = Math.floor((Math.random() * MIN) + MAX);
shortened = serialize.toString(16);
if (shortenedCollection.length > 0) {
let isDuplicate = shortenedCollection.find(el => el.short_id == shortened);
if (isDuplicate) {
encodeUrl(url);
}
}
let sce = new ShortenedURLElement(url, shortened, hasSecurity);
if (shortenedCollection.length < MAX) {
shortenedCollection.push(sce);
database.updateRemoteStorage(shortenedCollection);
return sce;
} else return { error: "Infelizmente atingimos o limite de URLs encurtadas!", code: 3 }
};
const decodeUrl = (shortened) => {
let notFound = { error: "A url não foi encontrada ou está expirada!", code: 2 };
let url = shortenedCollection.find(sce => sce.short_id == shortened);
if (url) {
let encodeDate = new Date(url.check_in);
let expireLimit = new Date();
expireLimit.setDate(expireLimit.getDate() + 5);
if (encodeDate >= expireLimit) {
shortenedCollection = shortenedCollection.slice(url, 1);
database.updateRemoteStorage(shortenedCollection);
return notFound;
} else return url;
} else {
return notFound;
}
};
const getAll = () => {
let urls = [];
if (shortenedCollection == 0) {
return { error: "Nenhuma URL foi encurtada ainda!", code: 1 };
}
shortenedCollection.forEach((sce) => {
urls.push({ short_id: sce.short_id, url: sce.original_url });
}); return urls;
};
const urlTools = {
encode: encodeUrl,
decode: decodeUrl,
all: getAll,
};
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
routes(app, urlTools);
const server = app.listen(3000, () => {
console.log("[>] LVL URL SHORTENER is running! \n[!] Details:", JSON.stringify(server.address()));
});