# Add Health Checks to a Vapor App

Yes. An agent adds liveness and readiness routes that check the database, tests them, wires a Docker HEALTHCHECK and a Compose healthcheck, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-health-checks/vapor/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the service before it adds a route to it. In an app shaped like vapor/template-fluent-postgres, `Package.swift` uses Swift tools 6.3 and pins `vapor` from 4.121.4, `fluent` from 4.13.0, `fluent-postgres-driver` from 2.12.0 and `swift-nio` from 2.101.0, with an executable target and a test target that depends on `VaporTesting`. `Sources/TemplateFluentPostgres/routes.swift` registers `GET /` answering "It works!", `GET /hello`, and a `TodoController` whose `boot` adds `GET` and `POST /todos` and `DELETE /todos/:todoID`. `configure.swift` reads `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USERNAME`, `DATABASE_PASSWORD` and `DATABASE_NAME` from the environment, registers the Postgres database with TLS preferred, and adds the `CreateTodo` migration.

There is no health route, and the container has no check either. The `Dockerfile` builds on `swift:6.3-noble` with jemalloc and a static standard library, runs on `ubuntu:noble` as a `vapor` user with only `libjemalloc2`, `ca-certificates` and `tzdata` installed, exposes 8080 and starts `serve --env production`; it has no `HEALTHCHECK` and no `curl`. `docker-compose.yml` defines `app`, `migrate`, `revert` and a `db` on `postgres:18-alpine`, with `app` depending on `db` by name only, so Compose starts the app before Postgres accepts connections. The tests in `Tests/TemplateFluentPostgresTests` use Swift Testing with a `withApp` helper that configures the app, runs `autoMigrate`, runs the test and reverts, so every test already needs a reachable Postgres.

The agent adds `Controllers/HealthController.swift` as a `RouteCollection` with two routes. `GET /health/live` returns 200 with `{"status": "ok"}` and touches nothing, so a restart loop cannot be caused by a database outage. `GET /health/ready` runs a trivial query through `req.db` with a short timeout and returns 200 with `{"status": "ok", "checks": {"database": "ok"}}`, or 503 with the failing check named, so a load balancer stops routing to an instance that lost its database. `routes.swift` registers the collection beside `TodoController`. In the `Dockerfile` the runtime stage installs `curl` alongside `ca-certificates` and gains a `HEALTHCHECK` that fetches `/health/ready`; in `docker-compose.yml`, `db` gets a `pg_isready` healthcheck and `app` and `migrate` depend on it with `condition: service_healthy`.

Two tests go into the existing suite in the same style: `/health/live` answers 200 without the database, and `/health/ready` answers 200 inside `withApp` with the test database up. The agent starts Postgres in its sandbox with `docker compose up db`, exports the `DATABASE_*` variables the way the Compose file does, and runs `swift build` and `swift test`. Then it runs `docker compose build` and `docker compose up`, waits for `docker inspect` to report the app container `healthy`, fetches both routes, stops the database and confirms `/health/ready` turns 503 while `/health/live` stays 200, and brings everything down before it opens the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add health checks to this Vapor app. Read Package.swift, Sources/TemplateFluentPostgres/routes.swift, configure.swift, entrypoint.swift, Controllers/TodoController.swift, Tests/TemplateFluentPostgresTests/TemplateFluentPostgresTests.swift, Dockerfile, .dockerignore and docker-compose.yml first. Add a HealthController route collection with GET /health/live, which returns 200 and touches nothing, and GET /health/ready, which runs a trivial query through req.db with a short timeout and returns 200 or 503 with a JSON body naming each check. Register it in routes.swift. Add curl to the runtime stage of the Dockerfile and a HEALTHCHECK on /health/ready; in docker-compose.yml give db a pg_isready healthcheck and make app and migrate wait for it to be healthy. Add tests for both routes in the existing suite. Start Postgres with docker compose up db, run swift build and swift test, then docker compose build and up, wait for the app container to report healthy, fetch both routes, stop the database and confirm ready turns 503 while live stays 200. Open a pull request with the routes, their bodies and the commands you ran.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch. It adds `Sources/TemplateFluentPostgres/Controllers/HealthController.swift` and two tests in `Tests/TemplateFluentPostgresTests/TemplateFluentPostgresTests.swift`; it changes `routes.swift` by one registration line, the runtime stage of the `Dockerfile` (`curl` and the `HEALTHCHECK`), and `docker-compose.yml` (the `pg_isready` healthcheck on `db` and `condition: service_healthy` on `app` and `migrate`). `configure.swift`, the migrations and the `TodoController` are untouched. The description shows both routes with their JSON bodies and the statuses each returns.

The record is the proof, since the generated template ships without a workflow. The trajectory at `GET /o/{org}/sessions/{id}/messages` keeps the `swift build` and `swift test` output, the Compose build, the `docker inspect` health status, the fetched bodies, and the 503 that `/health/ready` returns once the database is stopped, beside the commands that produced them, so a reviewer sees the readiness check fail for the right reason rather than reading that it would.

Before merging, a reviewer decides whether the orchestrator they deploy to should probe `/health/live` or `/health/ready` for restarts, since only the first is safe to restart on, and whether the readiness body should name more dependencies as the app grows. Nothing is deployed until the pull request merges.

## Run it on every new Vapor service

A developer starts this from the terminal when a service is created from the template: one `POST /o/{org}/sessions` with the repository in `resources` and this prompt, then walks away. The run clones the service into its own sandbox with its own Docker daemon, builds, tests and exercises the container, and opens the pull request; the sandbox parks when the turn ends, and a follow-up through `POST /o/{org}/sessions/{id}/messages` wakes it, for a Redis check or a different path.

With a `webhook_url` on the API key, `run.completed` arrives with `data.session_url` to read for the pull request link, and a `request_id` per repository keeps a retried request from starting a second run. Several services can get the same routes at once, each in its own sandbox, up to the key's `max_concurrent`.

## Why run it on Rush?

A health route is only proven by taking its dependency away, and that is awkward on a laptop: a Swift release build, a Postgres container, the app container, then stopping the database to watch the status flip, all on ports something else is probably using. In a Rush sandbox the agent builds the image, brings the stack up, reads the health status from `docker inspect`, stops the database, fetches the route again and fixes the timeout or the status code before anyone deploys it, and nothing is left running on your machine.

The pull request carries its proof: the build, the test run and the readiness flip are in the transcript beside the commands that produced them. The same prompt on a second service built from the template is a second sandbox and a second pull request at the same time.

## Frequently asked questions

### Why two routes rather than one?

Because they answer different questions. `/health/live` says the process is up and should not be restarted, so it touches nothing; a database outage must not make a supervisor kill a healthy process. `/health/ready` says the instance can serve traffic, so it checks Postgres and returns 503 when the query fails, which tells a load balancer to route elsewhere. The agent documents which one to probe for which purpose.

### Why install curl in the runtime image?

Because the `Dockerfile`'s runtime stage is `ubuntu:noble` with only `libjemalloc2`, `ca-certificates` and `tzdata`, so there is nothing to make an HTTP request with and a `HEALTHCHECK` needs a command. `curl` is one small package in the same `apt-get install` line. If you would rather not add it, the prompt can ask for a `healthcheck` subcommand on the binary instead.

### Do the tests need a database?

The readiness test does, and so does every existing test: the suite's `withApp` helper calls `configure`, which registers Postgres from the `DATABASE_*` variables, and runs `autoMigrate`. The liveness test is written to run without one. The agent starts `db` from the Compose file before `swift test`, which is what a developer on this template does today.

### What does the Compose change fix?

Start order. `app` depended on `db` by name, which only orders container creation, so the app could start before Postgres accepted connections and fail its first query. With a `pg_isready` healthcheck on `db` and `condition: service_healthy` on `app` and `migrate`, Compose waits until the database answers before starting either.

## More Vapor jobs

- [Add Request Validation to a Vapor API](https://getrush.ai/run/add-input-validation/vapor/)
- [Add JWT Authentication to a Vapor App from a Linear Issue](https://getrush.ai/run/add-jwt-auth/vapor/)
- [Add OpenAPI Docs to a Vapor API](https://getrush.ai/run/add-openapi-docs/vapor/)

## The same job on other stacks

- [Add Health Checks to a Wagtail Site](https://getrush.ai/run/add-health-checks/wagtail/)
- [Add Health Checks to an Actix Web Server](https://getrush.ai/run/add-health-checks/actix/)
- [Add Health Checks to an AdonisJS API](https://getrush.ai/run/add-health-checks/adonisjs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
