From 598a5c34a2eac0295fd148fd87ea50f5b585d71a Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:11:19 +0000 Subject: [PATCH 1/2] feature: containerize frontend (nginx /api proxy, configurable API_URL, compose stack) and fix vitest harness (zone.js) Co-Authored-By: Parker Duff --- .dockerignore | 7 +++ Dockerfile | 17 +++++++ angular.json | 21 ++++++++ bun.lock | 7 +-- docker-compose.yml | 29 +++++++++++ docker/nginx.conf.template | 16 ++++++ package.json | 3 +- .../core/interceptors/api.interceptor.spec.ts | 51 +++++++++++++++++++ src/app/core/interceptors/api.interceptor.ts | 9 +++- src/environments/environment.docker.ts | 3 ++ src/environments/environment.ts | 3 ++ 11 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 docker/nginx.conf.template create mode 100644 src/app/core/interceptors/api.interceptor.spec.ts create mode 100644 src/environments/environment.docker.ts create mode 100644 src/environments/environment.ts diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..e80677ade --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.git +node_modules +dist +.angular +coverage +playwright-report +test-results diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..625bc3da9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# syntax=docker/dockerfile:1 + +FROM node:22-slim AS build +COPY --from=oven/bun:1.3 /usr/local/bin/bun /usr/local/bin/bun +WORKDIR /app +COPY package.json bun.lock ./ +RUN bun install --frozen-lockfile --ignore-scripts +COPY . . +RUN test -f realworld/assets/theme/styles.css \ + || (echo "realworld submodule missing: run 'git submodule update --init'" && exit 1) +RUN bun run build --configuration docker + +FROM nginxinc/nginx-unprivileged:1.27-alpine +ENV BACKEND_URL=http://backend:8080 +COPY docker/nginx.conf.template /etc/nginx/templates/default.conf.template +COPY --from=build /app/dist/angular-conduit/browser /usr/share/nginx/html +EXPOSE 8080 diff --git a/angular.json b/angular.json index f67bfcdf5..31d260a49 100644 --- a/angular.json +++ b/angular.json @@ -57,6 +57,27 @@ "extractLicenses": false, "sourceMap": true, "namedChunks": true + }, + "docker": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "outputHashing": "all", + "fileReplacements": [ + { + "replace": "src/environments/environment.ts", + "with": "src/environments/environment.docker.ts" + } + ] } }, "defaultConfiguration": "production" diff --git a/bun.lock b/bun.lock index ac785ed3c..13696b208 100644 --- a/bun.lock +++ b/bun.lock @@ -8,7 +8,7 @@ "@angular/animations": "21.1.1", "@angular/common": "21.1.1", "@angular/compiler": "21.1.1", - "@angular/core": "21.1.1", + "@angular/core": "21.2.4", "@angular/forms": "21.1.1", "@angular/platform-browser": "21.1.1", "@angular/platform-browser-dynamic": "21.1.1", @@ -34,6 +34,7 @@ "prettier": "^3.8.1", "typescript": "~5.9.3", "vitest": "^4.0.18", + "zone.js": "^0.16.3", }, }, }, @@ -90,7 +91,7 @@ "@angular/compiler-cli": ["@angular/compiler-cli@21.1.1", "", { "dependencies": { "@babel/core": "7.28.5", "@jridgewell/sourcemap-codec": "^1.4.14", "chokidar": "^5.0.0", "convert-source-map": "^1.5.1", "reflect-metadata": "^0.2.0", "semver": "^7.0.0", "tslib": "^2.3.0", "yargs": "^18.0.0" }, "peerDependencies": { "@angular/compiler": "21.1.1", "typescript": ">=5.9 <6.0" }, "optionalPeers": ["typescript"], "bin": { "ngc": "bundles/src/bin/ngc.js", "ng-xi18n": "bundles/src/bin/ng_xi18n.js" } }, "sha512-CCB8SZS0BzqLOdOaMpPpOW256msuatYCFDRTaT+awYIY1vQp/eLXzkMTD2uqyHraQy8cReeH/P6optRP9A077Q=="], - "@angular/core": ["@angular/core@21.1.1", "", { "dependencies": { "tslib": "^2.3.0" }, "peerDependencies": { "@angular/compiler": "21.1.1", "rxjs": "^6.5.3 || ^7.4.0", "zone.js": "~0.15.0 || ~0.16.0" }, "optionalPeers": ["@angular/compiler", "zone.js"] }, "sha512-KFRCEhsi02pY1EqJ5rnze4mzSaacqh14D8goDhtmARiUH0tefaHR+uKyu4bKSrWga2T/ExG0DJX52LhHRs2qSw=="], + "@angular/core": ["@angular/core@21.2.4", "", { "dependencies": { "tslib": "^2.3.0" }, "peerDependencies": { "@angular/compiler": "21.2.4", "rxjs": "^6.5.3 || ^7.4.0", "zone.js": "~0.15.0 || ~0.16.0" }, "optionalPeers": ["@angular/compiler", "zone.js"] }, "sha512-2+gd67ZuXHpGOqeb2o7XZPueEWEP81eJza2tSHkT5QMV8lnYllDEmaNnkPxnIjSLGP1O3PmiXxo4z8ibHkLZwg=="], "@angular/forms": ["@angular/forms@21.1.1", "", { "dependencies": { "@standard-schema/spec": "^1.0.0", "tslib": "^2.3.0" }, "peerDependencies": { "@angular/common": "21.1.1", "@angular/core": "21.1.1", "@angular/platform-browser": "21.1.1", "rxjs": "^6.5.3 || ^7.4.0" } }, "sha512-NBbJOynLOeMsPo03+3dfdxE0P7SB7SXRqoFJ7WP2sOgOIxODna/huo2blmRlnZAVPTn1iQEB9Q+UeyP5c4/1+w=="], @@ -1214,7 +1215,7 @@ "zod-to-json-schema": ["zod-to-json-schema@3.25.1", "", { "peerDependencies": { "zod": "^3.25 || ^4" } }, "sha512-pM/SU9d3YAggzi6MtR4h7ruuQlqKtad8e9S0fmxcMi+ueAK5Korys/aWcV9LIIHTVbj01NdzxcnXSN+O74ZIVA=="], - "zone.js": ["zone.js@0.16.0", "", {}, "sha512-LqLPpIQANebrlxY6jKcYKdgN5DTXyyHAKnnWWjE5pPfEQ4n7j5zn7mOEEpwNZVKGqx3kKKmvplEmoBrvpgROTA=="], + "zone.js": ["zone.js@0.16.3", "", {}, "sha512-ihXL9+vhYyEhXz4TDNpHeAOZN9FVrbog0Il64OIEI28UP/n5AaI6gsccRuOHBfx+206agzyoK527bYIO0Foy6A=="], "@angular-devkit/core/source-map": ["source-map@0.7.6", "", {}, "sha512-i5uvt8C3ikiWeNZSVZNWcfZPItFQOsYTUAOkcUPGd8DqDy1uOUikjt5dG+uRlwyvR108Fb9DOd4GvXfT0N2/uQ=="], diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..30d31502b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +services: + backend: + build: + context: ${BACKEND_CONTEXT:-../spring-boot-realworld-example-app} + args: + MAVEN_MIRROR: ${MAVEN_MIRROR:-} + volumes: + - conduit-data:/data + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://localhost:8080/tags"] + interval: 5s + timeout: 3s + retries: 30 + start_period: 10s + restart: unless-stopped + + frontend: + build: . + environment: + BACKEND_URL: http://backend:8080 + ports: + - "${CONDUIT_PORT:-8080}:8080" + depends_on: + backend: + condition: service_healthy + restart: unless-stopped + +volumes: + conduit-data: diff --git a/docker/nginx.conf.template b/docker/nginx.conf.template new file mode 100644 index 000000000..8b3264c00 --- /dev/null +++ b/docker/nginx.conf.template @@ -0,0 +1,16 @@ +server { + listen 8080; + server_name _; + root /usr/share/nginx/html; + + location /api/ { + proxy_pass ${BACKEND_URL}/; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location / { + try_files $uri $uri/ /index.html; + } +} diff --git a/package.json b/package.json index 38bd2b29c..27180750c 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,8 @@ "playwright": "^1.58.0", "prettier": "^3.8.1", "typescript": "~5.9.3", - "vitest": "^4.0.18" + "vitest": "^4.0.18", + "zone.js": "^0.16.3" }, "lint-staged": { "*.{ts,html,css,json,md}": "prettier --write" diff --git a/src/app/core/interceptors/api.interceptor.spec.ts b/src/app/core/interceptors/api.interceptor.spec.ts new file mode 100644 index 000000000..2fd8dd233 --- /dev/null +++ b/src/app/core/interceptors/api.interceptor.spec.ts @@ -0,0 +1,51 @@ +import 'zone.js'; +import 'zone.js/testing'; +import { describe, it, expect, afterEach, beforeAll } from 'vitest'; +import { TestBed, getTestBed } from '@angular/core/testing'; +import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; +import { HttpClient, provideHttpClient, withInterceptors } from '@angular/common/http'; +import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; +import { API_URL, apiInterceptor } from './api.interceptor'; +import { environment } from '../../../environments/environment'; + +function setup(apiUrl?: string) { + TestBed.configureTestingModule({ + providers: [ + provideHttpClient(withInterceptors([apiInterceptor])), + provideHttpClientTesting(), + ...(apiUrl === undefined ? [] : [{ provide: API_URL, useValue: apiUrl }]), + ], + }); + return { http: TestBed.inject(HttpClient), httpMock: TestBed.inject(HttpTestingController) }; +} + +describe('apiInterceptor', () => { + beforeAll(() => { + getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); + }); + + afterEach(() => { + TestBed.inject(HttpTestingController).verify(); + TestBed.resetTestingModule(); + }); + + it('prefixes requests with the environment apiUrl by default', () => { + const { http, httpMock } = setup(); + http.get('/tags').subscribe(); + httpMock.expectOne(`${environment.apiUrl}/tags`).flush({ tags: [] }); + }); + + it('prefixes requests with a provided API_URL', () => { + const { http, httpMock } = setup('/api'); + http.get('/articles?limit=10').subscribe(); + httpMock.expectOne('/api/articles?limit=10').flush({ articles: [], articlesCount: 0 }); + }); + + it('supports an absolute backend URL', () => { + const { http, httpMock } = setup('http://backend:8080'); + http.post('/users/login', {}).subscribe(); + const req = httpMock.expectOne('http://backend:8080/users/login'); + expect(req.request.method).toBe('POST'); + req.flush({}); + }); +}); diff --git a/src/app/core/interceptors/api.interceptor.ts b/src/app/core/interceptors/api.interceptor.ts index 32160a1ee..7a408b8ae 100644 --- a/src/app/core/interceptors/api.interceptor.ts +++ b/src/app/core/interceptors/api.interceptor.ts @@ -1,6 +1,13 @@ +import { InjectionToken, inject } from '@angular/core'; import { HttpInterceptorFn } from '@angular/common/http'; +import { environment } from '../../../environments/environment'; + +export const API_URL = new InjectionToken('API_URL', { + providedIn: 'root', + factory: () => environment.apiUrl, +}); export const apiInterceptor: HttpInterceptorFn = (req, next) => { - const apiReq = req.clone({ url: `https://api.realworld.show/api${req.url}` }); + const apiReq = req.clone({ url: `${inject(API_URL)}${req.url}` }); return next(apiReq); }; diff --git a/src/environments/environment.docker.ts b/src/environments/environment.docker.ts new file mode 100644 index 000000000..dc087fc4c --- /dev/null +++ b/src/environments/environment.docker.ts @@ -0,0 +1,3 @@ +export const environment = { + apiUrl: '/api', +}; diff --git a/src/environments/environment.ts b/src/environments/environment.ts new file mode 100644 index 000000000..41a46b3d0 --- /dev/null +++ b/src/environments/environment.ts @@ -0,0 +1,3 @@ +export const environment = { + apiUrl: 'https://api.realworld.show/api', +}; From e43f65a46742a859661407706df7459715a91d6a Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:13:55 +0000 Subject: [PATCH 2/2] bug: re-resolve backend via Docker DNS so recreated backend containers keep working Co-Authored-By: Parker Duff --- Dockerfile | 3 ++- docker/nginx.conf.template | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 625bc3da9..eb4ec6ac5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,8 @@ RUN test -f realworld/assets/theme/styles.css \ RUN bun run build --configuration docker FROM nginxinc/nginx-unprivileged:1.27-alpine -ENV BACKEND_URL=http://backend:8080 +ENV BACKEND_URL=http://backend:8080 \ + NGINX_RESOLVER=127.0.0.11 COPY docker/nginx.conf.template /etc/nginx/templates/default.conf.template COPY --from=build /app/dist/angular-conduit/browser /usr/share/nginx/html EXPOSE 8080 diff --git a/docker/nginx.conf.template b/docker/nginx.conf.template index 8b3264c00..c655455ac 100644 --- a/docker/nginx.conf.template +++ b/docker/nginx.conf.template @@ -3,8 +3,13 @@ server { server_name _; root /usr/share/nginx/html; + # Re-resolve the backend via Docker DNS so a recreated backend container (new IP) keeps working. + resolver ${NGINX_RESOLVER} valid=10s ipv6=off; + set $backend_upstream ${BACKEND_URL}; + location /api/ { - proxy_pass ${BACKEND_URL}/; + rewrite ^/api/(.*)$ /$1 break; + proxy_pass $backend_upstream; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;