# Add Response Compression to an Echo App

Yes. An agent adds Echo's Gzip middleware to your router, keeps the swagger route and small bodies correct, proves it with httptest, and opens a pull request.

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

## What the agent does

The agent reads the app before it compresses anything. In an app laid out like xesina/golang-echo-realworld-example-app, `go.mod` is on `go 1.18` with `github.com/labstack/echo/v4 v4.1.16`, `github.com/jinzhu/gorm v1.9.8`, `github.com/swaggo/echo-swagger v1.0.0`, `github.com/stretchr/testify v1.4.0` and `gopkg.in/go-playground/validator.v9`. `main.go` builds the router with `router.New()`, mounts `echoSwagger.WrapHandler` on `/swagger/*`, opens SQLite at `./realworld.db` through `db.New()`, runs `AutoMigrate`, registers the handler on the `/api` group and calls `r.Start("127.0.0.1:8585")`. `router/router.go` sets the logger to `DEBUG`, adds `RemoveTrailingSlash` as a `Pre` middleware, then `Logger` and a `CORS` config that allows every origin. `handler/routes.go` guards `/user`, `/profiles` and the write side of `/articles` with the JWT middleware and leaves `GET /articles`, `GET /articles/:slug` and `GET /tags` open. The tests in `handler/*_test.go` build a request with `httptest.NewRequest`, a recorder, and `e.NewContext`, then call the handler method directly; `TestMain` creates `./../realworld_test.db` and drops it after. The `Makefile` runs `go test -v -race ./...` and `.github/workflows/go.yml` builds and tests on Go 1.18.

Echo v4.1.16 ships the middleware: `middleware.Gzip()` and `middleware.GzipWithConfig`, whose config in this version has exactly two fields, `Skipper` and `Level` (default `-1`, the library's default compression). The agent reads `middleware/compress.go` in the module cache before using it, because the behaviour that matters is there: the middleware adds `Vary: Accept-Encoding` to every response, wraps the writer only when the request's `Accept-Encoding` contains `gzip`, deletes `Content-Length` on `WriteHeader` since the compressed length is unknown, strips `Content-Encoding` on a `204`, and resets the writer when the handler wrote nothing so an empty body does not become a ten-byte gzip header. There is no minimum-length option in this release; that arrived later, so small JSON answers such as `{"tags": []}` are compressed too, and the pull request says so rather than pretending otherwise.

The change in `router/router.go` is one line after `Logger`: `e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))`, with a `Skipper` that returns true for paths under `/swagger/`, because the swagger UI handler serves embedded files with their own content types and the agent keeps that route exactly as it was. The JWT, CORS and trailing-slash behaviour do not change, and the order matters only in that `Pre` middleware runs before routing while `Use` wraps the handler, which is where compression belongs. `handler/response.go` is untouched: the response structs serialise the same JSON, and the middleware sets `Content-Type` from `http.DetectContentType` only when a handler never set one, which `c.JSON` always does.

The existing tests call handler methods on a context, which bypasses router middleware, so the agent adds `router/router_test.go` that goes through `e.ServeHTTP` instead. One test sends `GET /api/tags` with `Accept-Encoding: gzip` and asserts `Content-Encoding: gzip`, `Vary: Accept-Encoding`, no `Content-Length`, and that `gzip.NewReader` over the body yields the JSON the uncompressed call returns. One sends the same request without the header and asserts no `Content-Encoding`. One hits `/swagger/index.html` with the header and asserts it is not compressed. The agent runs `go test -v -race ./...` through the `Makefile` target and `go vet ./...` before opening the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add gzip response compression to this Echo v4 app. Read go.mod, main.go, router/router.go, handler/routes.go, handler/response.go, handler/handler_test.go, handler/article_test.go, Makefile and .github/workflows/go.yml first, and read middleware/compress.go in the Echo module cache for the version in go.mod before using it. Add middleware.GzipWithConfig in router.New after the Logger middleware with Level 5 and a Skipper for paths under /swagger/. Do not change the JWT, CORS or trailing-slash setup. Add router/router_test.go that serves requests through the Echo instance: with Accept-Encoding gzip on GET /api/tags expect Content-Encoding gzip, Vary Accept-Encoding, no Content-Length, and a body that decodes to the same JSON as the uncompressed call; without the header expect no Content-Encoding; on /swagger/index.html expect no compression. Note in the pull request that this Echo version has no minimum-length option. Run make test and go vet ./..., then open a pull request that quotes the output.",
  "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

The pull request changes `router/router.go` (the middleware and its skipper) and adds `router/router_test.go`. `go.mod` and `go.sum` do not change, because the middleware is part of the Echo module already required. Its description quotes the `go test -v -race ./...` output, shows two `curl -sI` calls against the running app, one with `-H 'Accept-Encoding: gzip'` and one without, and lists the response headers of each so the difference is visible without running anything.

A reviewer checks the three places compression goes wrong in practice: a proxy or CDN in front of the app that already compresses and would now double-encode (the `Vary` header is what lets a cache keep both forms), a streaming handler that needs `Flush` (the wrapper implements `http.Flusher`, and the test file points at the method), and the error path where a handler returns an error after writing nothing, which the middleware's reset handles. They then decide the level: `5` is the agent's choice for JSON, and `-1` or `gzip.BestSpeed` are one edit away.

Nothing about the data or the handlers changes. A client that sends no `Accept-Encoding` header gets exactly the bytes it got before.

## Run it on every new Echo service

A team with several Echo services sends this body once per repository from its own server, each `POST /o/{org}/sessions` naming that repository in `resources` with a `request_id` that is a UUID v5 of the repository name, so a retried call returns the run it already started. Each run reads its own `router.go`, finds where the middleware chain is built, and decides which routes to skip, which differs between an API and a service that also serves files.

An automation on the Project with a `schedule` trigger set to `every: 1d` runs read-only and reports whether any new route serves a body that bypasses the middleware, such as a file handler mounted with `Pre`, posting its findings as a comment. The run that changes code is started by a person or by the handler above.

## Why run it on Rush?

Compression is a one-line change with headers that are easy to get subtly wrong: a `Content-Length` left over from before the body was compressed, a `Content-Encoding` on a `204`, a swagger page that stops rendering. The agent reads the middleware source for the exact version the app pins, runs the real router in its own sandbox, and keeps the test output and the two header listings with the pull request, so the reviewer sees the headers rather than a promise about them.

The same change across a fleet of Go services is the same middleware and a different skipper list each time. One run per repository, started from a script, gives one pull request per service with a test that goes through that service's own router.

## Frequently asked questions

### Does this need a new dependency?

No. `middleware.Gzip` and `GzipWithConfig` are in `github.com/labstack/echo/v4/middleware`, which the app already uses for `Logger`, `CORS` and `RemoveTrailingSlash`.

### Why is there no minimum body size?

Because `GzipConfig` in Echo v4.1.16 has only `Skipper` and `Level`. A minimum-length option exists in later releases; upgrading Echo is a separate change the pull request mentions but does not make. Small JSON bodies are compressed, which costs a few CPU cycles and saves nothing on them.

### Will the swagger UI still work?

Yes, because the skipper excludes `/swagger/`. The swagger handler serves its own embedded files, and the agent keeps that route exactly as `main.go` mounts it.

### What about requests with gzipped bodies?

They are not handled by this change. The middleware package in v4.1.16 has `compress.go` for responses and no request decompressor; the clients of this API send small JSON bodies, and the pull request says so.

### Why do the new tests go through ServeHTTP instead of calling the handler?

The existing tests call `h.Articles(c)` on a context built by `e.NewContext`, which never runs the router's middleware. Compression lives in the middleware chain, so its tests have to send a request into the Echo instance and read the recorder's headers.

## More Echo jobs

- [Add Test Coverage to CI for an Echo API](https://getrush.ai/run/add-coverage-to-ci/echo/)
- [Add Cursor Pagination to an Echo API](https://getrush.ai/run/add-cursor-pagination/echo/)
- [Add Versioned Database Migrations to an Echo App](https://getrush.ai/run/add-database-migrations/echo/)

## The same job on other stacks

- [Add Response Compression to a Fastify App](https://getrush.ai/run/add-compression/fastify/)
- [Add Response Compression to a Fiber App](https://getrush.ai/run/add-compression/fiber/)
- [Add Compression to a Koa App](https://getrush.ai/run/add-compression/koa/)

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