# poorBird reverse proxy. Install to /etc/caddy/Caddyfile.
#
# Ports, so nothing collides:
#   127.0.0.1:8090  uvicorn (POORBIRD_API_PORT) -- never exposed
#   :8080           plain HTTP for the camera (SPEC C2, firmware/API.md)
#   :443            TLS for humans
#
# TLS: Caddy gets a real Let's Encrypt certificate for a LAN-only hostname via
# DNS-01, so there is no port forwarding and no browser warning. That needs a
# Caddy build with your DNS provider's plugin:
#
#   caddy add-package github.com/caddy-dns/cloudflare
#
# For a first test on any Linux box you can skip the whole human-facing block
# and just use the :8080 one.

(protected_files) {
	# Auth-checked image serving (SPEC 3.3).
	#
	# An unguessable URL is not access control. The app authorizes the
	# request, then replies with an empty body and
	#   X-Accel-Redirect: /images/2026/09/08/<uuid>.jpg
	# and Caddy serves the bytes from disk. The browser never receives a
	# directly fetchable URL.
	#
	# The root below is the PARENT of POORBIRD_IMAGE_ROOT and
	# POORBIRD_THUMB_ROOT in /etc/poorbird/poorbird.env. One value, two
	# readers -- keep them in step.
	@accel header X-Accel-Redirect *
	handle_response @accel {
		rewrite * {rp.header.X-Accel-Redirect}
		file_server {
			root /srv/poorbird
		}
	}
}

# --- humans -----------------------------------------------------------------
poorbird.example.internal {
	tls {
		dns cloudflare {env.CLOUDFLARE_API_TOKEN}
	}

	encode zstd gzip

	# The API, and with it the auth-checked image handoff.
	handle /api/* {
		reverse_proxy 127.0.0.1:8090 {
			header_up X-Real-IP {remote_host}
			import protected_files
		}
	}

	# The WebUI: built on the dev machine with `npm run build` and copied here
	# as static files. Nothing in web/ runs on the server (SPEC 6), so this is
	# a file_server and not a second application.
	handle {
		root * /srv/poorbird/web

		# The router does client-side routing, so a reload on /devices or a
		# pasted deep link arrives as a path with no file behind it. Without
		# this fallback those are 404s and only the entry page ever works.
		try_files {path} /index.html
		file_server
	}
}

# --- the camera --------------------------------------------------------------
# Plain HTTP on the LAN, deliberately (SPEC C2): TLS on the ESP32 means a CA
# baked into firmware, and a mounted camera that silently stops uploading when
# the chain rotates.
:8080 {
	handle /api/v1/* {
		request_body {
			# A burst frame at UXGA can be a few hundred KB; an OTA image is
			# ~1-2 MB. The default limit is too small for both.
			max_size 12MB
		}
		reverse_proxy 127.0.0.1:8090 {
			# Required, not cosmetic. Rate limiting keys enrollment on the
			# caller's address, and without this every camera arrives as
			# 127.0.0.1 and they all share one bucket - one noisy device
			# would lock the rest out. The app trusts this header only
			# because uvicorn binds loopback and nothing else can reach it.
			header_up X-Real-IP {remote_host}
		}
	}

	handle /healthz {
		reverse_proxy 127.0.0.1:8090
	}

	handle {
		respond 404
	}
}
