Skip to content

Commit

Permalink
Merge 9e46dc7 into 5dedef8
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Nov 8, 2018
2 parents 5dedef8 + 9e46dc7 commit 58b44dd
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config.js
Expand Up @@ -748,6 +748,11 @@ Config.prototype.get = function (path, domain) {
return this.domainConfigs[domain].get(path)
}

Config.prototype.loadDomainConfig = function (domain, domainConfig) {
this.domainConfigs[domain] = convict(this.domainSchema)
this.domainConfigs[domain].load(domainConfig)
}

/**
* Builds a hash map with a Convict instance for each configured
* domain.
Expand Down
125 changes: 125 additions & 0 deletions dadi/lib/controller/domain.js
@@ -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)
}
5 changes: 5 additions & 0 deletions dadi/lib/controller/index.js
Expand Up @@ -10,6 +10,7 @@ const urlParser = require('url')
const zlib = require('zlib')

const config = require(path.join(__dirname, '/../../../config'))
const DomainController = require(path.join(__dirname, '/domain'))
const help = require(path.join(__dirname, '/../help'))
const logger = require('@dadi/logger')
const HandlerFactory = require(path.join(__dirname, '/../handlers/factory'))
Expand Down Expand Up @@ -185,6 +186,10 @@ const Controller = function (router) {
router.post('/api/routes', function (req, res) {
return RouteController.post(req, res)
})

router.use('/_dadi/domains/:domain?', function (req, res) {
return DomainController[req.method.toLowerCase()](req, res)
})
}

Controller.prototype.addContentTypeHeader = function (res, handler) {
Expand Down
35 changes: 35 additions & 0 deletions dadi/lib/models/domain-manager.js
Expand Up @@ -5,6 +5,41 @@ const DomainManager = function () {
this.domains = []
}

/**
* Adds a domain and it's configuration to the internal domain configs.
*
* @param {String} domain
* @param {Object} domainConfig
*/
DomainManager.prototype.addDomain = function (domain, domainConfig) {
let config = require('./../../../config')

if (!this.getDomain(domain)) {
config.loadDomainConfig(domain, domainConfig)

this.domains.push({
domain
})
} else {
config.loadDomainConfig(domain, domainConfig)
}
}

/**
* Removes a domain from the internal domain configs.
*
* @param {String} domain
*/
DomainManager.prototype.removeDomain = function (domain) {
let config = require('./../../../config')

if (this.getDomain(domain)) {
delete config.domainConfigs[domain]

this.domains = this.domains.filter(item => item.domain !== domain)
}
}

/**
* Returns a domain by name.
*
Expand Down
12 changes: 12 additions & 0 deletions dadi/lib/storage/http.js
Expand Up @@ -127,6 +127,18 @@ HTTPStorage.prototype.get = function ({
reject(httpError)
}
}
}).on('error', (err) => {
let httpError

if (err.code && err.code === 'ENOTFOUND') {
httpError = new Error(`Remote server not found for URL: ${this.getFullUrl()} ${err.message}`)
} else {
httpError = new Error(`ERROR: ${err.message} CODE: ${err.code}`)
}

httpError.statusCode = 500

reject(httpError)
})
})
}
Expand Down

0 comments on commit 58b44dd

Please sign in to comment.