-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (97 loc) · 3.26 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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const networkHelper = require(__dirname + '/public/modules/network_helper.js');
const app = express();
const port = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
let userToken = "";
app.get("/", function (req, res) {
res.redirect("auth");
})
app.get("/auth", function (req, res) {
res.render("auth");
})
app.post("/auth", async function (req, res) {
// get credentials from page form
const loginCredentials = { username: req.body.username, password: req.body.password };
try {
userToken = await networkHelper.authenticate(loginCredentials); // pass the credentials
res.redirect("/audit"); // redirects to audit
} catch (e) {
console.error('Error:', e);
res.redirect("/auth"); // refresh page with some message
}
});
app.get("/audit", function (req, res) {
res.render("audit", { results: [] });
});
// handles post request on /audit endpoint
app.post("/audit", async function (req, res) {
const domains = req.body.urls.split(",");
console.log("Requested domains: " + domains);
console.log(domains);
if (domains[0] == '') { // check if the input is empty -> refresh
res.redirect("/audit");
return;
};
try {
const results = await networkHelper.audit(domains, userToken);
res.render('audit', { results: results });
} catch (error) {
// if the request failed for whatever reason -> redirects to auth for new token
res.redirect("/auth");
}
});
app.get("/inventory", async function (req, res) {
try {
const publisherData = await networkHelper.getPublisher(userToken);
res.render("inventory", { publishers: publisherData });
} catch (error) {
console.log(error);
res.redirect("/auth");
}
})
app.post("/inventory", async function (req, res) {
const requestBody = req.body;
// if there is no name input, refresh the page
if (req.body.placementNames === "") {
res.redirect("/inventory");
return;
}
try {
// placement data is array of { id + name }
const placementData = await networkHelper.createPlacements(userToken, requestBody);
res.render("inventory_result", { placements: placementData });
} catch (error) {
console.log(error);
res.redirect("/auth");
}
})
app.post("/sites", async function (req, res) {
const publisherId = req.body.publisher;
try {
const sitesData = await networkHelper.getSites(userToken, publisherId);
res.json({ sites: sitesData });
} catch (error) {
console.log(error);
res.redirect("/auth");
}
})
app.post("/create-site", async function (req, res) {
const publisherId = req.body.publisherId;
const siteName = req.body.newSiteName;
try {
const siteData = await networkHelper.createSite(userToken, publisherId, siteName);
res.json({ site: siteData });
} catch (error) {
console.log(error);
res.redirect("/auth");
}
})
app.listen(port, function () {
console.log(`Server started on port ${port}`);
});