-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (87 loc) · 2.78 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
const config = require("./config");
const express = require("express");
const morgan = require("morgan");
const crypto = require("crypto");
const mongo = require("./lib/mongodb-query");
const PgPersistence = require("./lib/pg-persistence");
const app = express();
const port = config.PORT;
const host = config.HOST;
app.set("views", "./views");
app.set("view engine", "pug");
app.use(morgan("common"));
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use((_, res, next) => {
res.locals.store = new PgPersistence();
next();
});
//Route to create new bin
app.post("/createBin", async (_, res) => {
let binPath = crypto.randomBytes(10).toString("hex");
//e.g. binPath is a string - e.g. 40a65c27d6c4a79e
let store = res.locals.store;
await store.createBin(binPath);
res.redirect(`/bin/${binPath}`);
});
//Route to get all bins
app.get("/", async (_, res) => {
let store = res.locals.store;
let allBins = await store.getAllBins();
res.render("homepage", { allBins });
});
//Delete all requests in a bin
app.post("/deleteReqs/bin/:bin_path", async (req, res) => {
let store = res.locals.store;
let binPath = req.params.bin_path;
let stringIds = await store.getMongoIdsByBinPath(binPath);
await mongo.deleteObjects("test", "requests_collection", stringIds);
await store.clearBin(binPath);
res.redirect(`/bin/${binPath}`);
});
//Delete a bin and all requests
app.post("/deleteBin/bin/:bin_path", async (req, res) => {
let store = res.locals.store;
let binPath = req.params.bin_path;
let stringIds = await store.getMongoIdsByBinPath(binPath);
await mongo.deleteObjects("test", "requests_collection", stringIds);
await store.deleteBin(binPath);
res.redirect("/");
});
//Route to get all requests to a bin
app.get("/bin/:bin_path", async (req, res) => {
const hostname = req.get("host");
let store = res.locals.store;
const binPath = req.params.bin_path;
//postgres query for mongo ids that have corresponding binPath
let mongoIds = await store.getMongoIdsByBinPath(binPath);
const allRequests = await mongo.getObjectsById(
"test",
"requests_collection",
mongoIds
);
res.render("bin", { binPath, allRequests, hostname });
});
//Route to store all http requests to a bin
app.all("/bin/:bin_path", async (req, res) => {
let store = res.locals.store;
let binPath = req.params.bin_path;
let { body, url, method } = { ...req };
let newRequest = {
headers: req.headers,
body,
url,
method,
};
let mongoId = await mongo.insertOne(
"test",
"requests_collection",
newRequest
);
await store.createRequest(binPath, mongoId, method, url);
res.sendStatus(200);
});
app.listen(port, host, () => {
console.log(`Server is running on port: ${port} of ${host}`);
});