/
domain.js
101 lines (86 loc) · 2.47 KB
/
domain.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
const path = require('path')
const DomainManager = require(path.join(__dirname, '/../models/domain-manager'))
const help = require(path.join(__dirname, '/../help'))
/**
* Accept POST requests for adding domains to the internal domain configuration.
*/
module.exports.post = (req, res) => {
// Don't accept an empty POST
if (!req.body || !Array.isArray(req.body) || req.body.length === 0) {
return help.sendBackJSON(400, {
success: false,
errors: ['Bad Request']
}, res)
}
let domains = req.body
domains.forEach(item => {
if (!DomainManager.getDomain(item.domain)) {
// Add the domain configuration.
DomainManager.addDomain(item.domain, item.data)
}
})
return help.sendBackJSON(201, {
success: true,
domains: DomainManager.getDomains().map(item => item.domain)
}, res)
}
/**
* Accept PUT requests for modifying domains in the internal domain configuration.
*/
module.exports.put = (req, res) => {
// Don't accept an empty body
if (!req.body || !req.body.data) {
return help.sendBackJSON(400, {
success: false,
errors: ['Bad Request']
}, res)
}
let domain = req.params.domain
let configSchema = req.body.data
// Don't accept an empty param.
if (!domain || Object.keys(configSchema).length === 0) {
return help.sendBackJSON(400, {
success: false,
errors: ['Bad Request']
}, res)
}
// Domain not found.
if (!DomainManager.getDomain(domain)) {
return help.sendBackJSON(404, {
success: false,
errors: [`Domain '${domain}' does not exist`]
}, res)
}
// Update the domain configuration.
DomainManager.addDomain(domain, configSchema)
return help.sendBackJSON(200, {
success: true,
domains: DomainManager.getDomains().map(item => item.domain)
}, res)
}
/**
* Accept DELETE requests for removing domains from the internal domain configuration.
*/
module.exports.delete = (req, res) => {
let domain = req.params.domain
// Don't accept an empty param.
if (!domain) {
return help.sendBackJSON(400, {
success: false,
errors: ['Bad Request']
}, res)
}
// Domain not found.
if (!DomainManager.getDomain(domain)) {
return help.sendBackJSON(404, {
success: false,
errors: [`Domain '${domain}' does not exist`]
}, res)
}
// Remove the domain.
DomainManager.removeDomain(domain)
return help.sendBackJSON(200, {
success: true,
domains: DomainManager.getDomains().map(item => item.domain)
}, res)
}