|
| 1 | +import * as pulumi from '@pulumi/pulumi'; |
| 2 | +import * as cloudflare from '@pulumi/cloudflare'; |
| 3 | + |
| 4 | +const config = new pulumi.Config(); |
| 5 | +const cfConfig = new pulumi.Config('cloudflare'); |
| 6 | + |
| 7 | +const accountId = cfConfig.require('accountId'); |
| 8 | +const workerName = config.require('workerName'); |
| 9 | +const allowedOrigins = config.require('allowedOrigins'); |
| 10 | +const r2BucketName = config.require('r2BucketName'); |
| 11 | +const domainName = config.require('domainName'); |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// R2 Bucket — Pulumi state backend |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +const stateBucket = new cloudflare.R2Bucket('pulumi-state', { |
| 17 | + accountId, |
| 18 | + name: r2BucketName, |
| 19 | +}); |
| 20 | + |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | +// DNS Zone — adopt existing nsheaps.dev zone |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | +// The zone already exists in Cloudflare. Import it with: |
| 25 | +// pulumi import cloudflare:index/zone:Zone nsheaps-dev <zone-id> |
| 26 | +const zone = new cloudflare.Zone('nsheaps-dev', { |
| 27 | + accountId, |
| 28 | + zone: domainName, |
| 29 | +}); |
| 30 | + |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | +// DNS Records — app subdomains pointing to GitHub Pages |
| 33 | +// --------------------------------------------------------------------------- |
| 34 | +const privatePagesDns = new cloudflare.Record('private-pages-cname', { |
| 35 | + zoneId: zone.id, |
| 36 | + name: 'private-pages', |
| 37 | + type: 'CNAME', |
| 38 | + content: 'nsheaps.github.io', |
| 39 | + proxied: true, |
| 40 | +}); |
| 41 | + |
| 42 | +const ceptDns = new cloudflare.Record('cept-cname', { |
| 43 | + zoneId: zone.id, |
| 44 | + name: 'cept', |
| 45 | + type: 'CNAME', |
| 46 | + content: 'nsheaps.github.io', |
| 47 | + proxied: true, |
| 48 | +}); |
| 49 | + |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | +// CORS Proxy Worker |
| 52 | +// --------------------------------------------------------------------------- |
| 53 | +// The worker source is built in nsheaps/cors-proxy and published to ghcr.io. |
| 54 | +// For initial bootstrap, inline a minimal script. In production, CI pulls from |
| 55 | +// the cors-proxy repo's dist/ artifact. |
| 56 | +// |
| 57 | +// The worker script content is passed via the CORS_PROXY_SCRIPT env var or |
| 58 | +// read from the local build output. |
| 59 | +const workerScriptContent = process.env['CORS_PROXY_SCRIPT'] ?? |
| 60 | + 'export default { async fetch() { return new Response("cors-proxy not deployed yet", { status: 503 }); } };'; |
| 61 | + |
| 62 | +const workerScript = new cloudflare.WorkersScript(workerName, { |
| 63 | + accountId, |
| 64 | + name: workerName, |
| 65 | + content: workerScriptContent, |
| 66 | + module: true, |
| 67 | + plainTextBindings: [ |
| 68 | + { |
| 69 | + name: 'ALLOWED_ORIGINS', |
| 70 | + text: allowedOrigins, |
| 71 | + }, |
| 72 | + ], |
| 73 | +}); |
| 74 | + |
| 75 | +// Route the worker on a subdomain (optional: auth.nsheaps.dev) |
| 76 | +const authDns = new cloudflare.Record('auth-cname', { |
| 77 | + zoneId: zone.id, |
| 78 | + name: 'auth', |
| 79 | + type: 'CNAME', |
| 80 | + content: `${workerName}.workers.dev`, |
| 81 | + proxied: true, |
| 82 | +}); |
| 83 | + |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +// Outputs |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | +export const stateBucketName = stateBucket.name; |
| 88 | +export const zoneId = zone.id; |
| 89 | +export const privatePagesDomain = pulumi.interpolate`https://private-pages.${domainName}`; |
| 90 | +export const ceptDomain = pulumi.interpolate`https://cept.${domainName}`; |
| 91 | +export const authDomain = pulumi.interpolate`https://auth.${domainName}`; |
| 92 | +export const workerScriptName = workerScript.name; |
| 93 | + |
| 94 | +// Suppress unused variable warnings — these resources have side effects |
| 95 | +void privatePagesDns; |
| 96 | +void ceptDns; |
| 97 | +void authDns; |
0 commit comments