Files
AndreaSlopFotografie/web/config/serverConfig.ts
T
SluijsensandClaude Opus 5.5 e6436601a4
Continuous Integration / check (pull_request) Failing after 6m55s
Continuous Integration / lighthouse (pull_request) Skipped
Continuous Integration / deploy-test (pull_request) Skipped
Continuous Integration / deploy-production (pull_request) Skipped
Adds Gitea Actions CI/CD for test and production
The pipeline runs lint, format, tests, build, dependency audit and
Lighthouse (mobile and desktop, Playwright Chromium on the arm64 runner)
on every pull request, push to master and manual run.

Pushes to master deploy to test automatically, and any branch can be
deployed to test manually. Test is a Raspberry Pi with nginx next to the
current WordPress site; the deploy refuses folders containing WordPress.
Production deploys are manual and only from release/* branches, over
FTP with TLS to the IIS webhost.

The IIS web.config and both nginx configs are generated from the same
security header list as the Vite servers, with a test that fails when
the generated files drift. Adds .gitattributes for LF line endings and
pins pnpm through packageManager.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 00:18:51 +02:00

162 lines
6.1 KiB
TypeScript

// Genereert de webserverconfiguratie voor test (nginx op de Raspberry Pi) en
// productie (IIS bij de webhoster) uit dezelfde securityheaders als de Vite
// dev- en preview-server. Zo bestaat de headerset maar op één plek.
// Schrijven: `pnpm server-config`. Een test controleert dat de bestanden in
// deploy/ gelijk zijn aan wat deze functies opleveren.
import { securityHeaders } from './securityHeaders.ts';
const generatedNotice =
'Gegenereerd door web/config/serverConfig.ts (pnpm server-config). Niet handmatig aanpassen.';
const xmlAttribute = (value: string) =>
value
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
// IIS (productie): security headers, HTTPS-redirect, SPA-fallback en caching.
// Bestanden in /assets hebben een hash in de naam en mogen lang gecachet
// worden; index.html nooit, zodat een nieuwe release direct zichtbaar is.
export function iisWebConfig(): string {
const headers = Object.entries(securityHeaders('production'))
.map(
([name, value]) =>
` <add name="${name}" value="${xmlAttribute(value)}" />`,
)
.join('\n');
return `<?xml version="1.0" encoding="utf-8"?>
<!-- ${generatedNotice} -->
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="SPA fallback" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
${headers}
</customHeaders>
</httpProtocol>
<staticContent>
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff2" mimeType="font/woff2" />
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
<location path="assets">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
</location>
</configuration>
`;
}
// nginx (test): dezelfde headers, plus noindex zodat de testsite nooit in
// zoekmachines komt. nginx erft add_header niet als een location er zelf een
// zet, daarom staan de headers in elk location-blok.
export function nginxTestConf(): string {
const headers = [
...Object.entries(securityHeaders('production')).map(
([name, value]) => `add_header ${name} "${value}" always;`,
),
'add_header X-Robots-Tag "noindex, nofollow" always;',
];
const block = (indent: string, extra: string[] = []) =>
[...headers, ...extra].map((line) => indent + line).join('\n');
return `# ${generatedNotice}
# Test-omgeving op de Raspberry Pi, achter de reverse-proxy-Pi (die TLS afhandelt
# met certbot). De proxy moet de Host-header doorgeven (proxy_set_header Host
# $host), anders kiest nginx hier het standaardblok (WordPress).
# Eigen server-blok naast de WordPress-site: bewust geen default_server, zodat
# dit blok alleen de testhostname beantwoordt.
# Pas server_name en root aan aan de server; de rest komt uit de generator.
server {
listen 80;
server_name test.andreaslop.nl;
root /var/www/html/andreaslop-test;
index index.html;
# Achter de reverse proxy: doorverwijzingen relatief houden, anders stuurt
# nginx naar zijn eigen http://-adres in plaats van de https-hostname
absolute_redirect off;
${block(' ', ['add_header Cache-Control "no-cache" always;'])}
# Testsite nooit indexeren
location = /robots.txt {
${block(' ')}
default_type text/plain;
return 200 "User-agent: *\\nDisallow: /\\n";
}
# Bestanden met een hash in de naam: lang cachen
location /assets/ {
${block(' ', ['add_header Cache-Control "public, max-age=31536000, immutable" always;'])}
try_files $uri =404;
}
# SPA-fallback: onbekende paden krijgen index.html (de router toont de 404)
location / {
${block(' ', ['add_header Cache-Control "no-cache" always;'])}
try_files $uri $uri/ /index.html;
}
}
`;
}
// nginx op de reverse-proxy-Pi (test): alleen HTTP. certbot voegt daarna
// HTTPS en de redirect toe (certbot --nginx -d test.andreaslop.nl). Geen
// security headers hier: die zet de hosting-Pi al, dubbel geeft dubbele headers.
// Intern adres van de hosting-Pi (waar het testblok en WordPress draaien)
const hostingPiAddress = '192.168.1.103';
export function nginxProxyConf(): string {
return `# ${generatedNotice}
# Reverse proxy voor de testsite naar de hosting-Pi. certbot past dit bestand
# aan voor HTTPS; kopieer het daarna dus niet opnieuw zonder certbot opnieuw te
# draaien.
server {
listen 80;
server_name test.andreaslop.nl;
location / {
proxy_pass http://${hostingPiAddress};
# Verplicht: de hosting-Pi kiest op basis van de hostname tussen het
# testblok en WordPress
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
`;
}
export const serverConfigFiles = {
'deploy/iis/web.config': iisWebConfig,
'deploy/nginx/andreaslop-test.conf': nginxTestConf,
'deploy/nginx/test.andreaslop.nl.proxy.conf': nginxProxyConf,
} as const;