This commit is contained in:
Andras Bacsai
2022-03-01 15:22:11 +01:00
parent dbcbac0137
commit f51f7bc82a
3 changed files with 142 additions and 130 deletions

View File

@@ -113,6 +113,7 @@ export async function haproxyInstance() {
} }
export async function configureHAProxy() { export async function configureHAProxy() {
try {
const haproxy = await haproxyInstance(); const haproxy = await haproxyInstance();
await checkHAProxy(haproxy); await checkHAProxy(haproxy);
const data = { const data = {
@@ -128,11 +129,15 @@ export async function configureHAProxy() {
fqdn, fqdn,
id, id,
port, port,
destinationDocker: { engine, network }, destinationDocker,
destinationDockerId,
settings: { previews }, settings: { previews },
updatedAt updatedAt
} = application; } = application;
if (destinationDockerId) {
const { engine, network } = destinationDocker;
const isRunning = await checkContainer(engine, id); const isRunning = await checkContainer(engine, id);
if (fqdn) {
const domain = getDomain(fqdn); const domain = getDomain(fqdn);
const isHttps = fqdn.startsWith('https://'); const isHttps = fqdn.startsWith('https://');
const isWWW = fqdn.includes('www.'); const isWWW = fqdn.includes('www.');
@@ -176,6 +181,8 @@ export async function configureHAProxy() {
} }
} }
} }
}
}
const services = await db.prisma.service.findMany({ const services = await db.prisma.service.findMany({
include: { include: {
destinationDocker: true, destinationDocker: true,
@@ -187,18 +194,15 @@ export async function configureHAProxy() {
}); });
for (const service of services) { for (const service of services) {
const { const { fqdn, id, type, destinationDocker, destinationDockerId, updatedAt } = service;
fqdn, if (destinationDockerId) {
id, const { engine } = destinationDocker;
type,
destinationDocker: { engine },
updatedAt
} = service;
const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type); const found = db.supportedServiceTypesAndVersions.find((a) => a.name === type);
if (found) { if (found) {
const port = found.ports.main; const port = found.ports.main;
const publicPort = service[type]?.publicPort; const publicPort = service[type]?.publicPort;
const isRunning = await checkContainer(engine, id); const isRunning = await checkContainer(engine, id);
if (fqdn) {
const domain = getDomain(fqdn); const domain = getDomain(fqdn);
const isHttps = fqdn.startsWith('https://'); const isHttps = fqdn.startsWith('https://');
const isWWW = fqdn.includes('www.'); const isWWW = fqdn.includes('www.');
@@ -218,6 +222,8 @@ export async function configureHAProxy() {
} }
} }
} }
}
}
const { fqdn, updatedAt } = await db.prisma.setting.findFirst(); const { fqdn, updatedAt } = await db.prisma.setting.findFirst();
if (fqdn) { if (fqdn) {
const domain = getDomain(fqdn); const domain = getDomain(fqdn);
@@ -231,12 +237,13 @@ export async function configureHAProxy() {
isHttps, isHttps,
redirectValue, redirectValue,
redirectTo: isWWW ? domain : 'www.' + domain, redirectTo: isWWW ? domain : 'www.' + domain,
updatedAt updatedAt: updatedAt.getTime()
}); });
} }
const output = mustache.render(template, data); const output = mustache.render(template, data);
const newHash = crypto.createHash('md5').update(output).digest('hex'); const newHash = crypto.createHash('md5').update(output).digest('hex');
const { proxyHash, id } = await db.listSettings(); const { proxyHash, id } = await db.listSettings();
console.log({ proxyHash, newHash, output });
if (proxyHash !== newHash) { if (proxyHash !== newHash) {
await db.prisma.setting.update({ where: { id }, data: { proxyHash: newHash } }); await db.prisma.setting.update({ where: { id }, data: { proxyHash: newHash } });
console.log('HAProxy configuration changed, updating...'); console.log('HAProxy configuration changed, updating...');
@@ -250,6 +257,9 @@ export async function configureHAProxy() {
} }
}); });
} else { } else {
// console.log('HAProxy configuration is up to date'); console.log('HAProxy configuration is up to date');
}
} catch (error) {
throw error;
} }
} }

View File

@@ -70,7 +70,7 @@ export async function deleteProxy({ id }) {
}) })
.json(); .json();
} catch (error) { } catch (error) {
console.log(error.response.body); console.log(error.response?.body || error);
} finally { } finally {
if (transactionId) await completeTransaction(transactionId); if (transactionId) await completeTransaction(transactionId);
} }

View File

@@ -1,3 +1,4 @@
import { dev } from '$app/env';
import { ErrorHandler } from '$lib/database'; import { ErrorHandler } from '$lib/database';
import { configureHAProxy } from '$lib/haproxy/configuration'; import { configureHAProxy } from '$lib/haproxy/configuration';
@@ -5,6 +6,7 @@ export default async function () {
try { try {
return await configureHAProxy(); return await configureHAProxy();
} catch (error) { } catch (error) {
ErrorHandler(error.response.body || error); console.log(error.response?.body || error);
return ErrorHandler(error.response?.body || error);
} }
} }