From 08704c289a1ef979f1f815fc804cc4eaa301a1ca Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 12 Apr 2022 10:12:46 +0200 Subject: [PATCH] fix: Proxy --- src/lib/haproxy/configuration.ts | 46 +++++++-------------- src/routes/destinations/[id]/index.json.ts | 1 - src/routes/sources/[id]/newGithubApp.svelte | 1 - 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/lib/haproxy/configuration.ts b/src/lib/haproxy/configuration.ts index f93a34838..8a967befd 100644 --- a/src/lib/haproxy/configuration.ts +++ b/src/lib/haproxy/configuration.ts @@ -1,6 +1,8 @@ import { dev } from '$app/env'; import got, { type Got } from 'got'; import * as db from '$lib/database'; +import mustache from 'mustache'; +import crypto from 'crypto'; import { checkContainer, checkHAProxy } from '.'; import { asyncExecShell, getDomain, getEngine } from '$lib/common'; import { supportedServiceTypesAndVersions } from '$lib/components/common'; @@ -262,36 +264,20 @@ export async function configureHAProxy(): Promise { redirectValue, redirectTo: isWWW ? domain.replace('www.', '') : 'www.' + domain }); - for (const service of services) { - const { fqdn, id, type, destinationDocker, destinationDockerId, updatedAt } = service; - if (destinationDockerId) { - const { engine } = destinationDocker; - const found = supportedServiceTypesAndVersions.find((a) => a.name === type); - if (found) { - const port = found.ports.main; - const publicPort = service[type]?.publicPort; - const isRunning = await checkContainer(engine, id); - if (fqdn) { - const domain = getDomain(fqdn); - const isHttps = fqdn.startsWith('https://'); - const isWWW = fqdn.includes('www.'); - const redirectValue = `${isHttps ? 'https://' : 'http://'}${domain}%[capture.req.uri]`; - if (isRunning) { - data.services.push({ - id, - port, - publicPort, - domain, - isRunning, - isHttps, - redirectValue, - redirectTo: isWWW ? domain.replace('www.', '') : 'www.' + domain, - updatedAt: updatedAt.getTime() - }); - } - } - } + } + const output = mustache.render(template, data); + const newHash = crypto.createHash('md5').update(output).digest('hex'); + const { proxyHash, id } = await db.listSettings(); + if (proxyHash !== newHash) { + await db.prisma.setting.update({ where: { id }, data: { proxyHash: newHash } }); + await haproxy.post(`v2/services/haproxy/configuration/raw`, { + searchParams: { + skip_version: true + }, + body: output, + headers: { + 'Content-Type': 'text/plain' } - } + }); } } diff --git a/src/routes/destinations/[id]/index.json.ts b/src/routes/destinations/[id]/index.json.ts index d64df8c08..aa9be6aea 100644 --- a/src/routes/destinations/[id]/index.json.ts +++ b/src/routes/destinations/[id]/index.json.ts @@ -8,7 +8,6 @@ import type { RequestHandler } from '@sveltejs/kit'; export const get: RequestHandler = async (event) => { const { teamId, status, body } = await getUserDetails(event); if (status === 401) return { status, body }; - console.log(teamId); const { id } = event.params; try { const destination = await db.getDestination({ id, teamId }); diff --git a/src/routes/sources/[id]/newGithubApp.svelte b/src/routes/sources/[id]/newGithubApp.svelte index b6a74c3ba..c8cb4f6f3 100644 --- a/src/routes/sources/[id]/newGithubApp.svelte +++ b/src/routes/sources/[id]/newGithubApp.svelte @@ -36,7 +36,6 @@ export let settings; onMount(() => { const { organization, id, htmlUrl } = source; - console.log(source); const { fqdn } = settings; const host = dev ? 'http://localhost:3000'