Files
coolify/src/routes/settings/index.json.ts
Andras Bacsai bb6d1fd6a3 cleanup
2022-02-23 10:40:34 +01:00

132 lines
3.5 KiB
TypeScript

import { dev } from '$app/env';
import { getDomain, getUserDetails } from '$lib/common';
import * as db from '$lib/database';
import { listSettings, ErrorHandler } from '$lib/database';
import {
configureCoolifyProxyOff,
configureCoolifyProxyOn,
forceSSLOnApplication,
reloadHaproxy,
removeWwwRedirection,
setWwwRedirection,
startCoolifyProxy
} from '$lib/haproxy';
import { letsEncrypt } from '$lib/letsencrypt';
import type { RequestHandler } from '@sveltejs/kit';
import { promises as dns } from 'dns';
export const get: RequestHandler = async (event) => {
const { status, body } = await getUserDetails(event);
if (status === 401) return { status, body };
try {
const settings = await listSettings();
return {
status: 200,
body: {
settings
}
};
} catch (error) {
return ErrorHandler(error);
}
};
export const del: RequestHandler = async (event) => {
const { teamId, status, body } = await getUserDetails(event);
if (teamId !== '0')
return {
status: 401,
body: {
message: 'You do not have permission to do this. \nAsk an admin to modify your permissions.'
}
};
if (status === 401) return { status, body };
const { fqdn } = await event.request.json();
let ip;
try {
ip = await dns.resolve(fqdn);
} catch (error) {
// Do not care.
}
try {
const domain = getDomain(fqdn);
await db.prisma.setting.update({ where: { fqdn }, data: { fqdn: null } });
await configureCoolifyProxyOff(fqdn);
await removeWwwRedirection(domain);
return {
status: 200,
body: {
message: 'Domain removed',
redirect: ip ? `http://${ip[0]}:3000/settings` : undefined
}
};
} catch (error) {
return ErrorHandler(error);
}
};
export const post: RequestHandler = async (event) => {
const { teamId, status, body } = await getUserDetails(event);
if (teamId !== '0')
return {
status: 401,
body: {
message: 'You do not have permission to do this. \nAsk an admin to modify your permissions.'
}
};
if (status === 401) return { status, body };
const { fqdn, isRegistrationEnabled, dualCerts, minPort, maxPort } = await event.request.json();
try {
const {
id,
fqdn: oldFqdn,
isRegistrationEnabled: oldIsRegistrationEnabled,
dualCerts: oldDualCerts
} = await db.listSettings();
if (oldIsRegistrationEnabled !== isRegistrationEnabled) {
await db.prisma.setting.update({ where: { id }, data: { isRegistrationEnabled } });
}
if (oldDualCerts !== dualCerts) {
await db.prisma.setting.update({ where: { id }, data: { dualCerts } });
}
if (oldFqdn && oldFqdn !== fqdn) {
if (oldFqdn) {
const oldDomain = getDomain(oldFqdn);
await configureCoolifyProxyOff(oldFqdn);
await removeWwwRedirection(oldDomain);
}
}
if (fqdn) {
await startCoolifyProxy('/var/run/docker.sock');
const domain = getDomain(fqdn);
const isHttps = fqdn.startsWith('https://');
if (domain) {
await configureCoolifyProxyOn(fqdn);
await setWwwRedirection(fqdn);
if (isHttps) {
await letsEncrypt({ domain, isCoolify: true });
await forceSSLOnApplication(domain);
await reloadHaproxy('/var/run/docker.sock');
}
}
await db.prisma.setting.update({ where: { id }, data: { fqdn } });
await db.prisma.destinationDocker.updateMany({
where: { engine: '/var/run/docker.sock' },
data: { isCoolifyProxyUsed: true }
});
}
if (minPort && maxPort) {
await db.prisma.setting.update({ where: { id }, data: { minPort, maxPort } });
}
return {
status: 201
};
} catch (error) {
return ErrorHandler(error);
}
};