-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (74 loc) · 2.12 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
const app = require('express')();
const {Client} = require("pg");
const HashRing = require('hashring');
const crypto = require('crypto');
const hr = new HashRing();
const postgresHostName = "christymac.local"; // TODO: change this to local machine hostname
hr.add("5432");
hr.add("5433");
hr.add("5434");
const clients = {
"5432" : new Client( {
"host": postgresHostName,
"port" : "5432",
"user" : "postgres",
"password" : "changeme",
"database" : "postgres"
}),
"5433" : new Client( {
"host": postgresHostName,
"port" : "5433",
"user" : "postgres",
"password" : "changeme",
"database" : "postgres"
}),
"5434" : new Client( {
"host": postgresHostName,
"port" : "5434",
"user" : "postgres",
"password" : "changeme",
"database" : "postgres"
}),
}
connect();
// Postgres Connection Pool
async function connect() {
await clients["5432"].connect();
await clients["5433"].connect();
await clients["5434"].connect();
}
// GET URL from DB
app.get('/:urlId', async (req, res) => {
const urlId = req.params.urlId; // rGu2a
const server = hr.get(urlId);
const result = await clients[server].query("SELECT * FROM URL_TABLE WHERE URL_ID = $1", [urlId]);
if (result.rowCount > 0) {
res.status(200);
res.send({
"hash": urlId,
"URL" : result.rows[0].url,
"server": server
});
} else {
res.status(404);
res.send("URL not found");
}
});
// POST URL into DB
app.post("/", async (req, res) => {
const url = req.query.url; // www.google.com
// Consistently hash to get a port
const hash = crypto.createHash("sha256").update(url).digest("base64");
const urlID = hash.substr(0, 5);
const server = hr.get(urlID);
await clients[server].query("INSERT INTO URL_TABLE (URL, URL_ID) VALUES ($1, $2)", [url, urlID]);
res.status(200);
res.send({
"hash": urlID,
"URL" : url,
"server": server
});
});
app.listen(3000, () => {
console.log("Listening on PORT: 3000");
})