Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
437 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const path = require('path') | ||
const DomainManager = require(path.join(__dirname, '/../models/domain-manager')) | ||
const help = require(path.join(__dirname, '/../help')) | ||
|
||
const configTemplate = { | ||
images: { | ||
directory: { | ||
enabled: false | ||
}, | ||
remote: { | ||
enabled: true, | ||
path: null | ||
} | ||
}, | ||
assets: { | ||
directory: { | ||
enabled: false | ||
}, | ||
remote: { | ||
enabled: true, | ||
path: null | ||
} | ||
} | ||
} | ||
/** | ||
* 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, index) => { | ||
if (!DomainManager.getDomain(item.domain)) { | ||
// Prepare the domain configuration. | ||
let configContent = Object.assign({}, configTemplate) | ||
configContent.images.remote.path = item.data.remote.path | ||
configContent.assets.remote.path = item.data.remote.path | ||
|
||
// Add the domain configuration. | ||
DomainManager.addDomain(item.domain, configContent) | ||
} | ||
|
||
if (index === domains.length - 1) { | ||
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) => { | ||
let domain = req.params.domain | ||
let payload = req.body | ||
|
||
// Don't accept an empty param. | ||
if (!domain || !Object.keys(payload).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) | ||
} | ||
|
||
// Prepare the domain configuration. | ||
let configContent = Object.assign({}, configTemplate) | ||
configContent.images.remote.path = payload.remote.path | ||
configContent.assets.remote.path = payload.remote.path | ||
|
||
// Update the domain configuration. | ||
DomainManager.addDomain(domain, configContent) | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.