2021-03-24 21:11:14 +00:00
|
|
|
|
|
|
|
|
const { setDefaultConfiguration } = require('../../../libs/applications/configuration')
|
|
|
|
|
const { docker } = require('../../../libs/docker')
|
2021-04-19 07:46:05 +00:00
|
|
|
const { saveServerLog } = require('../../../libs/logging')
|
2021-03-24 21:11:14 +00:00
|
|
|
|
|
|
|
|
module.exports = async function (fastify) {
|
|
|
|
|
fastify.post('/', async (request, reply) => {
|
2021-04-15 20:40:44 +00:00
|
|
|
try {
|
|
|
|
|
const configuration = setDefaultConfiguration(request.body)
|
2021-03-24 21:11:14 +00:00
|
|
|
|
2021-04-15 20:40:44 +00:00
|
|
|
const services = (await docker.engine.listServices()).filter(r => r.Spec.Labels.managedBy === 'coolify' && r.Spec.Labels.type === 'application')
|
|
|
|
|
let foundDomain = false
|
2021-03-24 21:11:14 +00:00
|
|
|
|
2021-04-15 20:40:44 +00:00
|
|
|
for (const service of services) {
|
|
|
|
|
const running = JSON.parse(service.Spec.Labels.configuration)
|
|
|
|
|
if (running) {
|
|
|
|
|
if (
|
|
|
|
|
running.publish.domain === configuration.publish.domain &&
|
|
|
|
|
running.repository.id !== configuration.repository.id &&
|
|
|
|
|
running.publish.path === configuration.publish.path
|
|
|
|
|
) {
|
|
|
|
|
foundDomain = true
|
|
|
|
|
}
|
2021-03-24 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 20:40:44 +00:00
|
|
|
if (fastify.config.DOMAIN === configuration.publish.domain) foundDomain = true
|
|
|
|
|
if (foundDomain) {
|
|
|
|
|
reply.code(500).send({ message: 'Domain already in use.' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return { message: 'OK' }
|
|
|
|
|
} catch (error) {
|
2021-04-19 07:46:05 +00:00
|
|
|
await saveServerLog(error)
|
|
|
|
|
throw new Error(error)
|
2021-03-24 21:11:14 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|