-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
82 lines (70 loc) · 2.27 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
const e = require('express');
const express = require('express');
const fetch = require('node-fetch');
require('dotenv').config()
const app = express();
app.listen(3000, () => console.log('server started @ 3000'));
// function mid(req,res,next){
// console.log(req.params);
// console.log(req.query);
// }
app.get('/:id', async (req, res) => {
try {
//requested site id
const siteID = parseInt(req.params.id);
//request type
const requetType = req.query.requetType;
//console.log(requetType);
//if blocks
if (isNaN(siteID)) throw new Error('Site ID must be a number');
//if (siteID > 10) throw new Error('invalid site ID');
//funcitons
if(requetType === 'siteStatus'){
//single site status
res.json(await getSiteStatus(siteID));
}
else if(requetType === 'siteInfo'){
//single site/server info
res.json(await getSiteInfo(siteID));
}
else if(requetType === 'siteHealth'){
//single site/server info
res.json(await getSiteHealth(siteID));
}
else if(requetType === 'securityStatus'){
//single security status
res.json(await getSecurityStatus(siteID));
}
//res.json(await getSiteDetails(siteID));
} catch (error) {
res.json({ success: false, message: error.message });
}
});
//MainWP Domain and
const domain = process.env.DOMAIN;
const consumerKey = process.env.CONSUMER_KEY;
const consumerSecret = process.env.CONSUMER_SECRET;
//single site status
function getSiteStatus(id) {
return fetch(
`https://${domain}/wp-json/mainwp/v1/site/site?site_id=${id}&consumer_key=${consumerKey}&consumer_secret=${consumerSecret}`
).then((r) => r.json());
}
//single site/server info
function getSiteInfo(id) {
return fetch(
`https://${domain}/wp-json/mainwp/v1/site/site-info?site_id=${id}&consumer_key=${consumerKey}&consumer_secret=${consumerSecret}`
).then((r) => r.json());
}
//single site healt
function getSiteHealth(id) {
return fetch(
`https://${domain}/wp-json/mainwp/v1/site/site-health-score?site_id=${id}&consumer_key=${consumerKey}&consumer_secret=${consumerSecret}`
).then((r) => r.json());
}
//single site healt
function getSecurityStatus(id) {
return fetch(
`https://${domain}/wp-json/mainwp/v1/site//site-security-issues?site_id=${id}&consumer_key=${consumerKey}&consumer_secret=${consumerSecret}`
).then((r) => r.json());
}