From e78cd6fe430316728be07db86e1f294bab3f0743 Mon Sep 17 00:00:00 2001 From: Roshan Piyush Date: Tue, 7 May 2024 20:41:17 +0530 Subject: [PATCH] Add mailhog proxy for single lb support --- deploy/docker/docker-compose.yml | 1 + deploy/helm/templates/web/configmap.yaml | 1 + services/web/nginx-wrapper.sh | 2 +- services/web/nginx.conf.template | 34 ++++++++++++++++++++++ services/web/nginx.ssl.conf.template | 36 ++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/deploy/docker/docker-compose.yml b/deploy/docker/docker-compose.yml index e552f18c..0bf8c43f 100755 --- a/deploy/docker/docker-compose.yml +++ b/deploy/docker/docker-compose.yml @@ -155,6 +155,7 @@ services: - COMMUNITY_SERVICE=crapi-community:${COMMUNITY_SERVER_PORT:-8087} - IDENTITY_SERVICE=crapi-identity:${IDENTITY_SERVER_PORT:-8080} - WORKSHOP_SERVICE=crapi-workshop:${WORKSHOP_SERVER_PORT:-8000} + - MAILHOG_WEB_SERVICE=mailhog:8025 - TLS_ENABLED=${TLS_ENABLED:-false} depends_on: crapi-community: diff --git a/deploy/helm/templates/web/configmap.yaml b/deploy/helm/templates/web/configmap.yaml index 031f08af..2e06da9a 100644 --- a/deploy/helm/templates/web/configmap.yaml +++ b/deploy/helm/templates/web/configmap.yaml @@ -9,4 +9,5 @@ data: COMMUNITY_SERVICE: {{ .Values.community.service.name }}:{{ .Values.community.port }} IDENTITY_SERVICE: {{ .Values.identity.service.name }}:{{ .Values.identity.port }} WORKSHOP_SERVICE: {{ .Values.workshop.service.name }}:{{ .Values.workshop.port }} + MAILHOG_WEB_SERVICE: {{ .Values.mailhog.service.name }}:{{ .Values.mailhog.webPort }} TLS_ENABLED: {{ .Values.tlsEnabled | quote }} diff --git a/services/web/nginx-wrapper.sh b/services/web/nginx-wrapper.sh index c90aef66..5e363989 100755 --- a/services/web/nginx-wrapper.sh +++ b/services/web/nginx-wrapper.sh @@ -23,6 +23,6 @@ else fi ls -al /app/certs env -envsubst '${HTTP_PROTOCOL} ${COMMUNITY_SERVICE} ${IDENTITY_SERVICE} ${WORKSHOP_SERVICE}' < $NGINX_TEMPLATE > /etc/nginx/conf.d/default.conf +envsubst '${HTTP_PROTOCOL} ${COMMUNITY_SERVICE} ${IDENTITY_SERVICE} ${WORKSHOP_SERVICE} ${MAILHOG_WEB_SERVICE}' < $NGINX_TEMPLATE > /etc/nginx/conf.d/default.conf openresty exec "$@" \ No newline at end of file diff --git a/services/web/nginx.conf.template b/services/web/nginx.conf.template index e01de758..6b468808 100644 --- a/services/web/nginx.conf.template +++ b/services/web/nginx.conf.template @@ -66,6 +66,23 @@ server { proxy_pass ${HTTP_PROTOCOL}://${IDENTITY_SERVICE}/identity/api/auth/jwks.json; } + location /mailhog/ { + rewrite_by_lua_block { + ngx.req.read_body() -- explicitly read the req body + local body = ngx.req.get_body_data() + if body then + body = ngx.re.gsub(body, ngx.var.scheme.."://"..ngx.var.http_host, "http://${MAILHOG_WEB_SERVICE}") + ngx.req.set_body_data(body) + end + } + proxy_pass http://${MAILHOG_WEB_SERVICE}/; + proxy_set_header Host ${MAILHOG_WEB_SERVICE}; + proxy_set_header X-Forwarded-Host $http_host; + sub_filter_types application/json text/html; + sub_filter "http://${MAILHOG_WEB_SERVICE}" "$scheme://$http_host"; + sub_filter_once off; + } + location / { try_files $uri /index.html =404; add_header Last-Modified $date_gmt; @@ -139,6 +156,23 @@ server { proxy_pass ${HTTP_PROTOCOL}://${IDENTITY_SERVICE}/identity/api/auth/jwks.json; } + location /mailhog/ { + rewrite_by_lua_block { + ngx.req.read_body() -- explicitly read the req body + local body = ngx.req.get_body_data() + if body then + body = ngx.re.gsub(body, ngx.var.scheme.."://"..ngx.var.http_host, "http://${MAILHOG_WEB_SERVICE}") + ngx.req.set_body_data(body) + end + } + proxy_pass http://${MAILHOG_WEB_SERVICE}/; + proxy_set_header Host ${MAILHOG_WEB_SERVICE}; + proxy_set_header X-Forwarded-Host $http_host; + sub_filter_types application/json text/html; + sub_filter "http://${MAILHOG_WEB_SERVICE}" "$scheme://$http_host"; + sub_filter_once off; + } + location / { try_files $uri /index.html =404; add_header Last-Modified $date_gmt; diff --git a/services/web/nginx.ssl.conf.template b/services/web/nginx.ssl.conf.template index 71fc4e20..d4e1defc 100644 --- a/services/web/nginx.ssl.conf.template +++ b/services/web/nginx.ssl.conf.template @@ -73,6 +73,25 @@ server { proxy_ssl_trusted_certificate /app/certs/server.crt; } + location /mailhog/ { + rewrite_by_lua_block { + ngx.req.read_body() -- explicitly read the req body + local body = ngx.req.get_body_data() + if body then + body = ngx.re.gsub(body, ngx.var.scheme.."://"..ngx.var.http_host, "http://${MAILHOG_WEB_SERVICE}") + ngx.req.set_body_data(body) + end + } + proxy_pass http://${MAILHOG_WEB_SERVICE}/; + proxy_set_header Host ${MAILHOG_WEB_SERVICE}; + proxy_set_header X-Forwarded-Host $http_host; + sub_filter_types application/json text/html; + sub_filter "http://${MAILHOG_WEB_SERVICE}" "$scheme://$http_host"; + sub_filter_once off; + proxy_ssl_verify off; + proxy_ssl_trusted_certificate /app/certs/server.crt; + } + location / { try_files $uri /index.html =404; add_header Last-Modified $date_gmt; @@ -146,6 +165,23 @@ server { proxy_pass ${HTTP_PROTOCOL}://${IDENTITY_SERVICE}/identity/api/auth/jwks.json; } + location /mailhog/ { + rewrite_by_lua_block { + ngx.req.read_body() -- explicitly read the req body + local body = ngx.req.get_body_data() + if body then + body = ngx.re.gsub(body, ngx.var.scheme.."://"..ngx.var.http_host, "http://${MAILHOG_WEB_SERVICE}") + ngx.req.set_body_data(body) + end + } + proxy_pass http://${MAILHOG_WEB_SERVICE}/; + proxy_set_header Host ${MAILHOG_WEB_SERVICE}; + proxy_set_header X-Forwarded-Host $http_host; + sub_filter_types application/json text/html; + sub_filter "http://${MAILHOG_WEB_SERVICE}" "$scheme://$http_host"; + sub_filter_once off; + } + location / { try_files $uri /index.html =404; add_header Last-Modified $date_gmt;