-
Notifications
You must be signed in to change notification settings - Fork 8
/
handler.js
122 lines (104 loc) · 4.14 KB
/
handler.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const env = process.env.NODE_ENV || 'development';
const config = require('./config')[env];
const username = config.database.username
const password = config.database.password
const db_host = config.database.host
const db_port = config.database.port
const db_name = config.database.name
const db_url = 'postgres://'+username+':'+password+'@'+db_host+':'+db_port+'/'+db_name
const localStorage = require('localStorage');
const util = require('util');
var urls = JSON.parse(localStorage.getItem('urls')) || [];
var pgp = require('pg-promise')(/* options */)
try{
var db = pgp(db_url);
}catch(err){
console.log(err);
}
exports.main = async function(fullUrl) {
await db.oneOrNone("select * from public.short_urls where full_url=$1 limit 1",[fullUrl], r => !!r)
.then(data => {
if(data == false){
var shortUrl = shortenUrl(fullUrl)
storeUrl(fullUrl,shortUrl)
}else{
db.oneOrNone("select * from public.short_urls where full_url=$1 limit 1",[fullUrl],)
.then(data => {
console.log("existed: " + data)
})
}
}).catch(error => {
console.log(error)
})
await db.oneOrNone("select * from public.short_urls where full_url=$1 limit 1",[fullUrl],)
.then(data => {
console.log(data.short_url)
localStorage.setItem('shortUrl',data.short_url);
}).catch(error => {
console.log(error)
})
}
function shortenUrl() {
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var codeLength = 7;
var shortenUrl = ''
for (i=0;i<codeLength;i++){
shortenUrl += chars.charAt(Math.floor(Math.random() * chars.length))
}
return shortenUrl
}
async function storeUrl(fullUrl,shortUrl) {
var date = new Date()
var created_at = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
date.setMonth(date.getMonth() + 3)
var expired_at = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
await db.oneOrNone("select * from public.short_urls where short_url=$1 limit 1",[shortUrl], r => !!r)
.then(data => {
if(data == false){
db.oneOrNone("insert into public.short_urls(full_url,short_url,hits,created_at,expired_at) values ($1,$2,$3,$4,$5) returning id", [fullUrl, shortUrl, 0, created_at, expired_at])
.then(data => {
console.log('success: ',data)
storeUrlToLocalStorage(fullUrl)
})
.catch(error => {
console.log(error)
})
}else{
db.oneOrNone("select * from public.short_urls where short_url=$1 limit 1",[shortUrl],)
.then(data => {
console.log('success: ',data)
storeUrlToLocalStorage(fullUrl)
return data
})
}
}).catch(error => {
console.log(error)
})
}
async function storeUrlToLocalStorage(fullUrl) {
await db.oneOrNone("select full_url,short_url,hits from public.short_urls where full_url=$1 limit 1",[fullUrl])
.then(data => {
url = {
fullUrl: data.full_url,
shortUrl: data.short_url,
hits: data.hits,
}
urls.push(url)
localStorage.setItem('urls',JSON.stringify(urls))
}).catch(error => {
console.log(error)
})
}
exports.dbCleanup = function() {
var date = new Date()
var current_ts = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
db.any("DELETE FROM public.short_urls WHERE expired_at < NOW()")
.then(data => {
console.log("[DBCLEANUP] is running -- current timestamp: " + current_ts)
}).catch(error => {
console.log("[DBCLEANUP] Error cleaning DB -- current timestamp: "+current_ts)
})
}
exports.addHits = function(shortUrl) {
db.oneOrNone("UPDATE hits SET hits = hits + 1 WHERE shortUrl=$1;",[shortUrl])
}