From 7ddac5000849782f19547cf63a72c891ce17057f Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 17 Jan 2023 11:12:42 +0100 Subject: [PATCH] feat: http + h2c paralel --- .../src/routes/webhooks/traefik/handlers.ts | 63 ++++++++++++++++--- .../src/routes/applications/[id]/+page.svelte | 4 +- .../src/routes/applications/[id]/index.svelte | 4 +- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/apps/api/src/routes/webhooks/traefik/handlers.ts b/apps/api/src/routes/webhooks/traefik/handlers.ts index 25057defc..ed055bebf 100644 --- a/apps/api/src/routes/webhooks/traefik/handlers.ts +++ b/apps/api/src/routes/webhooks/traefik/handlers.ts @@ -4,17 +4,29 @@ import { getTemplates } from '../../../lib/services'; import { OnlyId } from '../../../types'; function generateServices(serviceId, containerId, port, isHttp2 = false) { - return { + const payload = { [serviceId]: { loadbalancer: { servers: [ { - url: `${isHttp2 ? 'h2c' : 'http'}://${containerId}:${port}` + url: `http://${containerId}:${port}` } ] } } }; + if (isHttp2) { + payload[`${serviceId}-http2`] = { + loadbalancer: { + servers: [ + { + url: `h2c://${containerId}:${port}` + } + ] + } + }; + } + return payload; } function generateRouters( serviceId, @@ -24,18 +36,20 @@ function generateRouters( isHttps, isWWW, isDualCerts, - isCustomSSL + isCustomSSL, + isHttp2 = false ) { + let rule = `Host(\`${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`; let http: any = { entrypoints: ['web'], - rule: `Host(\`${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, + rule, service: `${serviceId}`, priority: 2, middlewares: [] }; let https: any = { entrypoints: ['websecure'], - rule: `Host(\`${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, + rule, service: `${serviceId}`, priority: 2, tls: { @@ -45,14 +59,14 @@ function generateRouters( }; let httpWWW: any = { entrypoints: ['web'], - rule: `Host(\`www.${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, + rule, service: `${serviceId}`, priority: 2, middlewares: [] }; let httpsWWW: any = { entrypoints: ['websecure'], - rule: `Host(\`www.${nakedDomain}\`)${pathPrefix ? ` && PathPrefix(\`${pathPrefix}\`)` : ''}`, + rule, service: `${serviceId}`, priority: 2, tls: { @@ -137,6 +151,38 @@ function generateRouters( } } } + if (isHttp2) { + let http2 = { + ...http, + service: `${serviceId}-http2`, + rule: `${rule} && HeadersRegexp(\`Content-Type\`, \`application/grpc*\`)` + }; + let http2WWW = { + ...httpWWW, + service: `${serviceId}-http2`, + rule: `${rule} && HeadersRegexp(\`Content-Type\`, \`application/grpc*\`)` + }; + let https2 = { + ...https, + service: `${serviceId}-http2`, + rule: `${rule} && HeadersRegexp(\`Content-Type\`, \`application/grpc*\`)` + }; + let https2WWW = { + ...httpsWWW, + service: `${serviceId}-http2`, + rule: `${rule} && HeadersRegexp(\`Content-Type\`, \`application/grpc*\`)` + }; + return { + [`${serviceId}-${pathPrefix}`]: { ...http }, + [`${serviceId}-${pathPrefix}-http2`]: { ...http2 }, + [`${serviceId}-${pathPrefix}-secure`]: { ...https }, + [`${serviceId}-${pathPrefix}-secure-http2`]: { ...https2 }, + [`${serviceId}-${pathPrefix}-www`]: { ...httpWWW }, + [`${serviceId}-${pathPrefix}-www-http2`]: { ...http2WWW }, + [`${serviceId}-${pathPrefix}-secure-www`]: { ...httpsWWW }, + [`${serviceId}-${pathPrefix}-secure-www-http2`]: { ...https2WWW } + }; + } return { [`${serviceId}-${pathPrefix}`]: { ...http }, [`${serviceId}-${pathPrefix}-secure`]: { ...https }, @@ -384,7 +430,8 @@ export async function proxyConfiguration(request: FastifyRequest, remote isHttps, isWWW, dualCerts, - isCustomSSL + isCustomSSL, + isHttp2 ) }; traefik.http.services = { diff --git a/apps/client/src/routes/applications/[id]/+page.svelte b/apps/client/src/routes/applications/[id]/+page.svelte index 1e1adbab2..1bd70cfe4 100644 --- a/apps/client/src/routes/applications/[id]/+page.svelte +++ b/apps/client/src/routes/applications/[id]/+page.svelte @@ -734,8 +734,8 @@ id="isHttp2" isCenter={false} bind:setting={isHttp2} - title="Enable HTTP/2 (h2c only)" - description="Enable HTTP/2 protocol.

HTTP/2 is a major revision of the HTTP network protocol used by the World Wide Web that allows faster web page loading by reducing the number of requests needed to load a web page.

Useful for gRPC and other HTTP/2 based services." + title="Enable h2c (HTTP/2 without TLS)" + description="Enable h2c protocol.

HTTP/2 is a major revision of the HTTP network protocol used by the World Wide Web that allows faster web page loading by reducing the number of requests needed to load a web page.

Useful for gRPC and other HTTP/2 based services." on:click={() => changeSettings('isHttp2')} /> diff --git a/apps/ui/src/routes/applications/[id]/index.svelte b/apps/ui/src/routes/applications/[id]/index.svelte index bae564c46..519518cd8 100644 --- a/apps/ui/src/routes/applications/[id]/index.svelte +++ b/apps/ui/src/routes/applications/[id]/index.svelte @@ -756,8 +756,8 @@ id="isHttp2" isCenter={false} bind:setting={isHttp2} - title="Enable HTTP/2 (h2c only)" - description="Enable HTTP/2 protocol.

HTTP/2 is a major revision of the HTTP network protocol used by the World Wide Web that allows faster web page loading by reducing the number of requests needed to load a web page.

Useful for gRPC and other HTTP/2 based services." + title="Enable h2c (HTTP/2 without TLS)" + description="Enable h2c protocol.

HTTP/2 is a major revision of the HTTP network protocol used by the World Wide Web that allows faster web page loading by reducing the number of requests needed to load a web page.

Useful for gRPC and other HTTP/2 based services." on:click={() => changeSettings('isHttp2')} />