-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
108 lines (99 loc) · 3.67 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
const wptutils = require('./wpt-utils');
const express = require('express');
const path = require('path');
const fs = require('fs-extra');
const bodyParser = require('body-parser');
const compression = require('compression');
const url = require('url');
const {NODE_ENV} = process.env;
if (NODE_ENV == "development") {
require('dotenv').config();
}
const defaultServer = process.env.WPT_SERVER;
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(compression());
app.use(express.static('public'));
let port = process.env.PORT;
const errMsg = {"statusCode":400,"statusText":"Generic ERROR"};
app.listen(port, () => {
app.get('/locations', (req,res) => {
let server = (req.query.server?req.query.server:defaultServer);
wptutils.getLocations(server).then((locations) => {
res.json(locations);
});
});
app.get('/status', (req, res) => {
server = (req.query.server?req.query.server:defaultServer);
if (!req.query.test) {
res.json(errMsg);
} else {
let testId = req.query.test;
wptutils.getResultSummary(testId,server).then((response)=>{
res.json(response);
});
}
});
app.get('/results', (req,res) => {
if (!req.query.test) {
res.json(errMsg);
} else {
server = (req.query.server?req.query.server:defaultServer);
requests = (req.query.requests?parseInt(req.query.requests):1);
wptutils.getResultSummary(req.query.test,server,requests).then((result) => {
res.json(result);
});
}
});
app.post('/submit', (req, res) => {
if (!(req.body.url && req.body.location && req.body.server)) {
res.json(errMsg);
} else {
let url = req.body.url;
let server = req.body.host;
let location = req.body.server+":"+req.body.location;
wptutils.submitTest(url,server,location).then((response)=>{
res.json(response);
});
}
});
app.get('/analyze', (req,res) => {
if (!req.query.test) {
res.json({"statusCode":400,"statusText":"Test ID not supplied"});
}
server = (req.query.server?req.query.server:defaultServer);
wptutils.analyze(req.query.test,path.join('public','tests')).then(()=>{
res.json({"statusCode":200,"statusText":`Analysis for ${req.query.test} completed`});
});
});
app.post('/submitDomains', (req, res) => {
if (!(req.body["selected-domains"] && req.body["original-url"] && req.body.location)) {
res.json(errMsg);
}
server = (req.body.server?req.body.server:defaultServer);
wptutils.submitBlockTests(req.body).then((response) => {
let testDir = path.join('public','tests');
let dir = path.join(testDir,req.body["original-testid"]);
if (fs.existsSync(dir)){
fs.removeSync(dir);
}
fs.mkdirSync(dir);
jsonData = {
"testId": req.body["original-testid"],
"testUrl": req.body["origina-url"],
"tests": [{"blocked":"","testId":req.body["original-testid"]}].concat(response)
};
fs.writeFile(
path.join(dir,"data.json"),
JSON.stringify(jsonData),
(err)=>{
if (err) {
throw new Error(err);
}
res.redirect("/result/?test="+req.body["original-testid"]);
}
);
});
});
});