-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·151 lines (128 loc) · 3.76 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var express = require('express');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var url = require("url");
var path = require('path');
var redis = require('redis');
var config = require('./config.js');
var baseHash = require('./baseHash.js');
app.use(express.static(path.join(__dirname, 'public')));
app.set('port', (process.env.PORT || 5000));
var db_url;
if (process.env.MONGODB_URI) db_url = process.env.MONGODB_URI;
else db_url = config.db.localhost + ':' + config.db.port;
console.log(db_url);
var rClient = redis.createClient(process.env.REDIS_URL);
rClient.on('connect', function() {
console.log('Redis Connected');
});
MongoClient.connect(db_url, function(err, client) {
if (err) throw err;
console.log("DB connected");
var db = client.db(config.db.name);
var query_arr = {_id: 'url_count'};
db.collection('counters').findOne(query_arr, function(err, res) {
if (err) {
client.close();
throw err;
}
if (!res){
var insert_arr = {_id: 'url_count', val: 1};
db.collection('counters').insertOne(insert_arr, function(err, res){
if (err) {
client.close();
throw err;
}
console.log("Counters Collection Inserted");
client.close();
});
}
else {
console.log("Counters exists!");
client.close();
}
});
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'views/index.html'));
});
app.get('/api/shorten', function(req, res) {
var longUrl = req.headers.url;
var result = url.parse(longUrl);
if(!result.protocol || longUrl.substring(0,4)=='www.') longUrl = 'https://' + longUrl;
result = url.parse(longUrl);
if(!result.host) {
res.send("Not a valid URL");
return;
}
rClient.exists(longUrl, function(err, reply) {
if (err) throw err;
if (reply === 1) {
console.log('URL already exists');
rClient.get(longUrl, function(err, reply) {
if (err) throw err;
res.send(reply);
return;
});
} else {
console.log('URL doesn\'t exist');
var shortUrl = '';
MongoClient.connect(db_url, function(err, client) {
if (err) throw err;
var db = client.db(config.db.name);
var urlCollection = db.collection('urls');
urlCollection.findOne({'long_url': longUrl}, function(err, doc) {
if (err) throw err;
if (doc) {
shortUrl = config.webhost + baseHash.encode(doc._id);
rClient.set(longUrl, shortUrl);
res.send(shortUrl);
client.close();
return;
}
else {
var countersCollection = db.collection('counters');
countersCollection.findOne({_id: 'url_count'}, function(err, docs) {
if (err) throw err;
var newId = docs.val;
countersCollection.update({_id: 'url_count'}, {$inc: {val: 1}}, function(err) {
if (err) throw err;
shortUrl = config.webhost + baseHash.encode(newId);
rClient.set(longUrl, shortUrl);
urlCollection.insert({_id: newId, long_url: longUrl}, function(err) {
if (err) throw err;
client.close();
res.send(shortUrl);
return;
});
});
});
}
});
});
}
});
});
app.get('/:encoded_id', function(req, res){
res.statusCode = 302;
var hashedUrl = req.params.encoded_id;
var id = baseHash.decode(hashedUrl);
MongoClient.connect(db_url, function(err, client) {
if (err) throw err;
var db = client.db(config.db.name);
var urlCollection = db.collection('urls');
urlCollection.findOne({'_id': id}, function(err, doc) {
if (err) throw err;
if (doc) {
res.redirect(doc.long_url);
client.close();
}
else{
res.redirect(config.webhost);
}
});
});
});
var server = app.listen(app.get('port'), function(){
console.log('Server listening on port ' + app.get('port'));
});