134 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-02-19 22:37:45 +01:00
import { dev } from '$app/env';
2022-02-10 15:47:44 +01:00
import { getDomain, getUserDetails } from '$lib/common';
import * as db from '$lib/database';
import { listSettings, ErrorHandler } from '$lib/database';
2022-02-10 15:47:44 +01:00
import {
checkProxyConfigurations,
2022-02-10 15:47:44 +01:00
configureCoolifyProxyOff,
configureCoolifyProxyOn,
forceSSLOnApplication,
reloadHaproxy,
2022-02-13 22:56:37 +01:00
removeWwwRedirection,
setWwwRedirection,
2022-02-10 15:47:44 +01:00
startCoolifyProxy
} from '$lib/haproxy';
import { letsEncrypt } from '$lib/letsencrypt';
import type { RequestHandler } from '@sveltejs/kit';
2022-02-18 08:48:05 +01:00
import { promises as dns } from 'dns';
2022-02-10 15:47:44 +01:00
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);
2022-02-10 15:47:44 +01:00
}
};
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();
2022-02-19 22:37:45 +01:00
let ip;
try {
ip = await dns.resolve(fqdn);
} catch (error) {
// Do not care.
}
2022-02-10 15:47:44 +01:00
try {
const domain = getDomain(fqdn);
2022-02-13 22:56:37 +01:00
await db.prisma.setting.update({ where: { fqdn }, data: { fqdn: null } });
await configureCoolifyProxyOff(fqdn);
await removeWwwRedirection(domain);
2022-02-10 15:47:44 +01:00
return {
status: 200,
body: {
message: 'Domain removed',
2022-02-19 22:37:45 +01:00
redirect: ip ? `http://${ip[0]}:3000/settings` : undefined
}
2022-02-10 15:47:44 +01:00
};
} catch (error) {
return ErrorHandler(error);
2022-02-10 15:47:44 +01:00
}
};
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 };
2022-02-22 10:35:39 +01:00
const { fqdn, isRegistrationEnabled, dualCerts, minPort, maxPort } = await event.request.json();
2022-02-10 15:47:44 +01:00
try {
await checkProxyConfigurations();
2022-02-10 15:47:44 +01:00
const {
id,
fqdn: oldFqdn,
isRegistrationEnabled: oldIsRegistrationEnabled,
dualCerts: oldDualCerts
2022-02-10 15:47:44 +01:00
} = 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 } });
}
2022-02-10 15:47:44 +01:00
if (oldFqdn && oldFqdn !== fqdn) {
if (oldFqdn) {
2022-02-13 22:56:37 +01:00
const oldDomain = getDomain(oldFqdn);
await configureCoolifyProxyOff(oldFqdn);
await removeWwwRedirection(oldDomain);
2022-02-10 15:47:44 +01:00
}
}
if (fqdn) {
await startCoolifyProxy('/var/run/docker.sock');
2022-02-10 15:47:44 +01:00
const domain = getDomain(fqdn);
const isHttps = fqdn.startsWith('https://');
if (domain) {
2022-02-13 22:56:37 +01:00
await configureCoolifyProxyOn(fqdn);
await setWwwRedirection(fqdn);
if (isHttps) {
2022-02-10 15:47:44 +01:00
await letsEncrypt({ domain, isCoolify: true });
await forceSSLOnApplication(domain);
2022-02-10 15:47:44 +01:00
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 }
});
}
2022-02-22 10:35:39 +01:00
if (minPort && maxPort) {
await db.prisma.setting.update({ where: { id }, data: { minPort, maxPort } });
}
2022-02-10 15:47:44 +01:00
return {
status: 201
};
} catch (error) {
return ErrorHandler(error);
2022-02-10 15:47:44 +01:00
}
};