# Add Health Checks to a Fiber API

Yes. An agent adds Fiber's healthcheck middleware with /livez and /readyz, makes readiness ping Redis, adds a Docker HEALTHCHECK, tests both, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-health-checks/fiber/). 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 probe. In a Fiber service like the `url-shortener-api` example the Fiber project publishes on GitHub, `api/main.go` loads `.env` with `godotenv`, calls `database.Init()`, builds `fiber.New()`, mounts `logger.New()` and registers two routes in `setupRoutes`: `GET /:url` for `routes.ResolveURL` and `POST /api/v1` for `routes.ShortenURL`, then listens on `APP_PORT`. `api/database/database.go` holds two go-redis v8 clients, `DB0` for the short URLs and `DB1` for the per-IP quota, and `Init()` pings both once at startup and returns an error if either fails. The module is `github.com/amalshaji/stoyle` on `go 1.26.0` with `github.com/gofiber/fiber/v3 v3.6.1`; `docker-compose.yml` builds `api` from `api/` and `db` from `db/Dockerfile` (`redis:alpine`) with a bare `depends_on: db`; `.env.example` documents `DB_ADDR`, `DB_PASS`, `APP_PORT`, `DOMAIN` and `API_QUOTA`. There is no health route, and the `GET /:url` catch-all means a request to `/healthz` today is looked up in Redis and answered 404 with `short not found in database`.

Fiber v3 ships a `healthcheck` middleware in `github.com/gofiber/fiber/v3/middleware/healthcheck`, so no new module is needed. The agent registers two endpoints in `main.go` before `setupRoutes(app)`, so the `/:url` route cannot swallow them: `app.Get(healthcheck.LivenessEndpoint, healthcheck.New())` answers `/livez` with 200 `OK` as long as the process serves requests, and `app.Get(healthcheck.ReadinessEndpoint, healthcheck.New(healthcheck.Config{Probe: ready}))` answers `/readyz` with 200 `OK` or 503 `Service Unavailable` depending on the probe. `ready` is a new `database.Ping(ctx) error` that calls `DB0.Ping(ctx).Err()` and `DB1.Ping(ctx).Err()` with a short `context.WithTimeout` derived from `c.Context()`, so a hung Redis turns the pod unready instead of hanging the probe. The agent uses the middleware's `ResponseFormat: healthcheck.FormatJSON` only if you ask; the default plain-text body is what most probes expect.

The container gets a `HEALTHCHECK` in `api/Dockerfile`: the final stage is `alpine`, whose busybox `wget` is enough for `HEALTHCHECK --interval=30s --timeout=3s --retries=3 CMD wget -qO- http://127.0.0.1:3000/readyz || exit 1`, matching the `EXPOSE 3000` and the `APP_PORT=:3000` default. In `docker-compose.yml` the `db` service gets a `healthcheck` of `redis-cli ping` and `api` waits with `depends_on: db: condition: service_healthy`, which needs the Compose specification rather than the file's `version: "3"` key; the agent removes that obsolete key and says so in the description. `README.md` gains the two endpoints in its table and a line on what each means.

Tests live in `api/main_test.go` and use Fiber's `app.Test` with `httptest.NewRequest`, so no port is opened: with `DB_ADDR` pointing at a closed local port, `/livez` is 200 and `/readyz` is 503; with a Redis reachable, both are 200; and a request to `/abc` still reaches the resolver, proving the ordering. The sandbox has no Docker, so for the reachable case the agent installs `redis-server` with `apt-get` and starts it on the loopback address, then runs `go build ./...`, `go vet ./...` and `go test ./...` from `api/` and keeps the output. The `.env.example`, the rate-limit logic in `routes/shorten.go` and the `db/Dockerfile` are untouched.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add health checks to this Fiber v3 API. Read main.go, the database package, the Dockerfile, docker-compose.yml and .env.example first. Use the healthcheck middleware from the fiber module: /livez always answers while the process serves, and /readyz runs a probe that pings every Redis client the app opens with a short timeout and answers 503 when one fails. Register both before the existing routes so no catch-all route shadows them. Add a Dockerfile HEALTHCHECK against /readyz on the port the image exposes, give the Redis service a compose healthcheck and make the api wait on it, removing the obsolete version key if needed. Write tests with app.Test covering liveness, readiness with Redis down and up, and that the existing routes still match. Run go build, go vet and go test and include the output. Do not change the rate limiting or the env example. Open a pull request.",
  "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 that changes `api/main.go` (two routes and the probe), `api/database/database.go` (the `Ping` helper), `api/Dockerfile` (the `HEALTHCHECK`), `docker-compose.yml` (the Redis healthcheck and the condition) and `README.md`, and adds `api/main_test.go`. `go.mod` and `go.sum` are unchanged, because the middleware is part of the fiber module already required.

The description names the two endpoints and what each proves, quotes the `go test` summary for the Redis-down and Redis-up cases, shows the `HEALTHCHECK` line and the compose change, and notes the removed `version` key. The commands and full output are in the transcript at `GET /o/{org}/sessions/{id}/messages`, and the changed files with their diffs at `GET /o/{org}/p/_/sessions/{id}/changes`.

Before merging, a reviewer confirms the probe timeout is shorter than the orchestrator's probe timeout, decides whether the readiness probe should also check the quota client `DB1` or only the URL store, and runs `docker compose up` once to see the `api` container wait for a healthy `db`. A follow-up message to the same session can wire the same two paths into a Kubernetes `readinessProbe` and `livenessProbe`.

## Run it on every Go service deployed without a readiness probe

A platform team starts one run per service with one `POST /o/{org}/sessions` each, the repository in `resources` and this prompt as `input`. Each run reads what the service depends on, so one that opens Postgres with `pgx` gets a probe that calls `db.PingContext` and one with only an in-memory store gets a liveness endpoint and a readiness endpoint that is always ready.

Send a `request_id`, a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's `webhook_url` reports when each run the key starts finishes, with the run's final answer in `result`.

## Why run it on Rush?

A health endpoint is small, but the mistakes are all in the details: a catch-all route that swallows it, a probe that hangs on a dead dependency, a Dockerfile `HEALTHCHECK` with a tool the final image does not have. The agent reads the route order, the clients and the base image, then proves the 503 and the 200 with a real Redis started in its own sandbox and keeps the output.

The pull request shows the probe, the tests for both states and the container wiring together, so review is a diff rather than a local Redis and a loop of `curl`. Several services can be done at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Why two endpoints instead of one /health?

Liveness says the process is up and should not be restarted; readiness says it can take traffic now. If Redis is down, restarting the API does not help, so `/livez` stays 200 while `/readyz` answers 503 and the orchestrator stops sending requests. Fiber's middleware names them `/livez` and `/readyz` by default, and the agent keeps those names.

### Will the health routes be rate limited or logged?

They are registered after `logger.New()`, so they appear in the access log like any request; say so in the prompt if you want them skipped with the logger's `Next` option. The per-IP quota lives inside the shorten handler, not in a middleware, so the probes are never counted against it.

### Does the readiness probe cost a Redis round trip every time?

Yes, one `PING` per client per probe, bounded by the timeout the agent sets. With the usual probe period of several seconds that is negligible. If you prefer a cached result, say so and the agent keeps the last status for a short interval instead of pinging on every request.

### I deploy to Kubernetes, not Compose. Does this still help?

The endpoints are the same. The Dockerfile `HEALTHCHECK` is ignored by Kubernetes, so ask for `livenessProbe` and `readinessProbe` entries on your Deployment in the prompt, or send that as a follow-up message to the same session, and the agent points them at `/livez` and `/readyz` on the container port.

## More Fiber jobs

- [Add Idempotency Keys to a Fiber API](https://getrush.ai/run/add-idempotency-middleware/fiber/)
- [Add Input Validation to a Fiber API](https://getrush.ai/run/add-input-validation/fiber/)
- [Add JWT Authentication to a Fiber API](https://getrush.ai/run/add-jwt-authentication/fiber/)

## The same job on other stacks

- [Add Health Checks to a Flask App](https://getrush.ai/run/add-health-checks/flask/)
- [Add Health Check Endpoints to a Gin App](https://getrush.ai/run/add-health-checks/gin/)
- [Add Health Check Endpoints to a Hono API](https://getrush.ai/run/add-health-checks/hono/)

## 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/)
