From 4a95732ea379086f025e791a88ff1c457ad3f95c Mon Sep 17 00:00:00 2001 From: bwees Date: Sat, 14 Nov 2026 14:05:33 -0600 Subject: [PATCH] feat(tofu): serve the web basemap from an r2 bucket on tiles.maroonrides.app --- README.md | 2 +- tofu/config/main.tf | 11 +++--- tofu/modules/pages-web/main.tf | 55 ++++++++++++++++++++++++++++- tofu/modules/pages-web/variables.tf | 4 +++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index bb01192..0e2cc7d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ MaroonRides infrastructure: one Contabo VPS (`prod-server`) running NixOS and a single-node k3s cluster (`prod`) reconciled by Flux. - `tofu/`: Contabo instance + NixOS install, `maroonrides.app` DNS records, - the `web.maroonrides.app` Pages project and its GitHub deploy secrets, + the `web.maroonrides.app` Pages project, `tiles.maroonrides.app` R2 bucket and their GitHub deploy secrets, Cloudflare tokens, R2 backup bucket, Tailscale policy, Flux webhooks - `nixos/`: host configuration - `kubernetes/`: Flux tree. `apps/base` is the platform, `apps/prod` the apps diff --git a/tofu/config/main.tf b/tofu/config/main.tf index d18619c..e880003 100644 --- a/tofu/config/main.tf +++ b/tofu/config/main.tf @@ -38,9 +38,10 @@ module "flux_webhooks" { module "pages_web" { source = "../modules/pages-web" - account_id = local.account_id - zone_id = local.zone_id - domain = local.domain - project_name = "maroonrides-web" - repository = "MaroonRides" + account_id = local.account_id + zone_id = local.zone_id + domain = local.domain + project_name = "maroonrides-web" + tiles_bucket_name = "maroonrides-map-tiles" + repository = "MaroonRides" } diff --git a/tofu/modules/pages-web/main.tf b/tofu/modules/pages-web/main.tf index db494ac..015cfb8 100644 --- a/tofu/modules/pages-web/main.tf +++ b/tofu/modules/pages-web/main.tf @@ -1,5 +1,6 @@ locals { - hostname = "web.${var.domain}" + hostname = "web.${var.domain}" + tiles_hostname = "tiles.${var.domain}" } // Deployments are direct uploads from GitHub Actions, so the project has no git source. @@ -24,6 +25,37 @@ resource "cloudflare_dns_record" "web" { ttl = 1 } +// Pages ignores Range headers, which PMTiles needs, so the web build reads the basemap from R2. +resource "cloudflare_r2_bucket" "tiles" { + account_id = var.account_id + name = var.tiles_bucket_name +} + +resource "cloudflare_r2_custom_domain" "tiles" { + account_id = var.account_id + bucket_name = cloudflare_r2_bucket.tiles.name + zone_id = var.zone_id + domain = local.tiles_hostname + enabled = true +} + +resource "cloudflare_r2_bucket_cors" "tiles" { + account_id = var.account_id + bucket_name = cloudflare_r2_bucket.tiles.name + + rules = [ + { + allowed = { + methods = ["GET", "HEAD"] + origins = ["*"] + headers = ["Range", "If-Match"] + } + expose_headers = ["ETag", "Content-Range", "Content-Length"] + max_age_seconds = 86400 + } + ] +} + resource "cloudflare_account_token" "pages_deploy" { account_id = var.account_id name = "maroonrides pages deploy" @@ -37,6 +69,15 @@ resource "cloudflare_account_token" "pages_deploy" { resources = jsonencode({ "com.cloudflare.api.account.${var.account_id}" = "*" }) + }, + { + effect = "allow" + permission_groups = [ + { id = "2efd5506f9c8494dacb1fa10a3e7d5b6" }, # Workers R2 Storage Bucket Item Write + ] + resources = jsonencode({ + "com.cloudflare.edge.r2.bucket.${var.account_id}_default_${cloudflare_r2_bucket.tiles.name}" = "*" + }) } ] } @@ -58,3 +99,15 @@ resource "github_actions_variable" "pages_project" { variable_name = "CLOUDFLARE_PAGES_PROJECT" value = cloudflare_pages_project.web.name } + +resource "github_actions_variable" "tiles_bucket" { + repository = var.repository + variable_name = "MAP_TILES_BUCKET" + value = cloudflare_r2_bucket.tiles.name +} + +resource "github_actions_variable" "tiles_url" { + repository = var.repository + variable_name = "MAP_TILES_URL" + value = "https://${local.tiles_hostname}" +} diff --git a/tofu/modules/pages-web/variables.tf b/tofu/modules/pages-web/variables.tf index 70c9f78..69c3bb6 100644 --- a/tofu/modules/pages-web/variables.tf +++ b/tofu/modules/pages-web/variables.tf @@ -17,3 +17,7 @@ variable "project_name" { variable "repository" { type = string } + +variable "tiles_bucket_name" { + type = string +}