-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (103 loc) · 3 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
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
const vsprintf = require("sprintf-js").vsprintf;
const bodyParser = require("body-parser");
const jsonfile = require('jsonfile');
const express = require("express");
const shortid = require("shortid");
const crypto = require('crypto');
const redis = require('redis');
const path = require('path');
var config = jsonfile.readFileSync(path.join(path.dirname(require.main.filename), 'config.json'));
var protocol = config.url.protocol; // read the protocol for the responses
var client = redis.createClient(); // this creates a new client
var app = express(); // create express app
// set the Ids seed
shortid.seed(82594);
// use the json parser for body
app.use(bodyParser.json());
// start listener
app.listen(config.server.port, () => {
console.log(vsprintf("Server running on port %s", [config.server.port.toString()]));
});
client.on('error', function (err) {
// do something on error
});
function getURLFromHash(hash, callback) {
if (shortid.isValid(hash)) {
client.get(hash, function (err, dbres) {
callback(dbres);
});
} else {
callback(null);
}
}
function generateNewShortId(counter, callback) {
var uuid = shortid.generate();
client.get(uuid, function (err, dbres) {
if (dbres) {
if (counter < 5) {
generateNewShortId(counter + 1, callback);
} else {
callback(null);
}
} else {
callback(uuid);
}
});
}
app.get("/api/getURL/:uuid", (req, res, next) => {
if ((req.header('apiKey')) && (crypto.createHash('md5').update(req.header('apiKey')).digest("hex") == "1ae80eea2d1fb4d5f4c60f511e6e180c")) {
getURLFromHash(req.params.uuid, function (data) {
res.send({
"url": data
});
});
} else {
res.status(404).send('Not found');
}
});
app.post("/api/setURL", (req, res, next) => {
if ((req.header('apiKey')) && (crypto.createHash('md5').update(req.header('apiKey')).digest("hex") == "1ae80eea2d1fb4d5f4c60f511e6e180c")) {
generateNewShortId(1, function (uuid) {
if (uuid) {
client.set(uuid, req.body.url, function (err, dbres) {
if (dbres) {
if (req.body.expire) {
client.expire(uuid, req.body.expire);
}
res.send({
"url": vsprintf("%s://%s/%s", [protocol, req.get('host'), uuid]),
"uuid": uuid
});
}
});
} else {
res.status(500);
}
});
} else {
res.status(404).send('Not found');
}
});
// server the redirect request
app.use(function (req, res) {
if (req.url) {
getURLFromHash(req.url.substring(1), function (hash) {
if (hash) {
res.redirect(hash);
} else {
res.status(404).send('Not found');
}
});
} else {
res.status(500).send('Internal server error');
}
});
// handle all possible errors
app.use(function (err, req, res, next) {
console.error(err.stack);
if (err.name == "IpDeniedError") {
res.status(404).send('Resource does not exist');
} else {
res.status(500).send('Internal server error');
}
});