Minimal Go HTTP service that redirects bare domains to their www subdomain. Deployed on Fly.io.
- Language: Go 1.24
- Deployment: Fly.io (app name:
from-root-to-www, region: GRU) - Docker: Multi-stage build,
scratchbase image (~9MB)
Single main.go with three HTTP handlers:
GET /healthz— health check (returns "ok")GET /status/<domain>— DNS/certificate diagnostics, auto-provisions Fly.io certificatesGET /(catch-all) — redirects bare domains towww.with 301, logs each redirect
The /status/ endpoint uses FLY_API_TOKEN with two Fly.io APIs:
- REST API (
api.machines.dev) — to query certificate status and DNS requirements - GraphQL API (
api.fly.io/graphql) — to create new certificates (addCertificate mutation)
main.go— all application logicfly.toml— Fly.io deployment configDockerfile— multi-stage build (golang:alpine -> scratch + CA certs).github/workflows/docker-publish.yml— CI/CD for ghcr.io
| Variable | Default | Description |
|---|---|---|
PORT |
1234 |
HTTP listen port |
FLY_APP_NAME |
from-root-to-www |
Fly.io app name (auto-set by Fly.io) |
FLY_API_TOKEN |
— | Fly.io API token (set as secret) |
fly deploy --depot=falseJust access /status/<domain> — the certificate is created automatically. Then configure DNS as instructed.
curl https://from-root-to-www.fly.dev/status/example.comfly logsfly secrets set FLY_API_TOKEN=$(fly auth token)go build -o /dev/null ./...
go run main.go
curl http://localhost:1234/ -H "Host: example.com"