Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.git
node_modules
dist
.angular
coverage
playwright-report
test-results
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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 \
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
21 changes: 21 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
21 changes: 21 additions & 0 deletions docker/nginx.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {
listen 8080;
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/ {
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;
}

location / {
try_files $uri $uri/ /index.html;
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
51 changes: 51 additions & 0 deletions src/app/core/interceptors/api.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -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({});
});
});
9 changes: 8 additions & 1 deletion src/app/core/interceptors/api.interceptor.ts
Original file line number Diff line number Diff line change
@@ -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<string>('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);
};
3 changes: 3 additions & 0 deletions src/environments/environment.docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const environment = {
apiUrl: '/api',
};
3 changes: 3 additions & 0 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const environment = {
apiUrl: 'https://api.realworld.show/api',
};
Loading