All jobs

Add Response Compression to an Axum API

Yes. An agent turns on tower-http's gzip and brotli features, adds a CompressionLayer to your Axum router, proves it with a test, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts by reading how the router is assembled, because compression in Axum is a Tower layer and the right place for it depends on what is already stacked. In an API like launchbadge/realworld-axum-sqlx, Cargo.toml pins axum at 0.3.4 with the tower-log feature, tower at 0.4.11 and tower-http at 0.2.0 with only the trace feature. src/http/mod.rs builds the app in serve(): api_router() merges the users, profiles and articles routers, then .layer(ServiceBuilder::new().layer(AddExtensionLayer::new(ApiContext { config, db })).layer(TraceLayer::new_for_http())), and axum::Server::bind listens on 0.0.0.0:8080. There is no reverse proxy in the repository, so every byte of GET /api/articles, GET /api/articles/feed and GET /api/tags leaves the process uncompressed.

The change is small and the agent keeps it that way. Cargo.toml adds compression-gzip and compression-br to the tower-http features, which leaves the version at 0.2.0 so nothing else in Cargo.lock moves except the crates those features bring in. src/http/mod.rs imports tower_http::compression::CompressionLayer and appends .layer(CompressionLayer::new()) to the same ServiceBuilder, after TraceLayer, so the trace span still records the request and the compressed body is what leaves the stack. The layer reads each request's Accept-Encoding, picks br or gzip from what the client lists, sets Content-Encoding on the response and drops the now-wrong Content-Length. A client that sends no Accept-Encoding gets the same bytes it got before.

The agent writes a test that does not need Postgres, because the real router cannot be built without the PgPool in ApiContext. A #[cfg(test)] module in src/http/mod.rs builds a one-route Router whose handler returns a few kilobytes of text, wraps it in the identical CompressionLayer::new(), and drives it with tower::ServiceExt::oneshot: a request with Accept-Encoding: gzip must come back with content-encoding: gzip and a shorter body, and a request without the header must come back with neither. cargo test runs it alongside the existing unit tests without the DATABASE_URL the integration path needs.

Finally the agent checks the CI the repository already has. .github/workflows/rust.yml starts a postgres service, installs sqlx-cli, runs sqlx db setup, cargo build and cargo test, then checks out abonander/realworld at a pinned commit and runs its Postman collection against http://localhost:8080/api. That collection runs under newman, which asks for gzip and decodes it before comparing JSON, so the API tests are unaffected; the agent says so in the pull request rather than editing the workflow. The agent runs cargo build, cargo test and cargo clippy in its sandbox and pastes the output.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add HTTP response compression to this Axum API. Read Cargo.toml, Cargo.lock, src/main.rs and src/http/mod.rs first and find the ServiceBuilder that wraps the router. Enable the compression-gzip and compression-br features on the tower-http version already in use, without bumping its version, and add a CompressionLayer to the existing layer stack after the TraceLayer. Do not add a reverse proxy or change the bind address. Write a test that builds a small Router with the same CompressionLayer and uses tower::ServiceExt::oneshot to assert that a request with Accept-Encoding: gzip gets content-encoding: gzip and a smaller body, and that a request without the header gets neither. Check whether the CI workflow's API test runner decodes gzip and say what you found. Run cargo build, cargo test and cargo clippy and include the output. 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 Cargo.toml (two feature flags on tower-http), Cargo.lock (the compression crates those flags pull in) and src/http/mod.rs (one import, one .layer(...) call and a test module). No route, handler, migration or SQL file is touched, and axum and tower-http stay at the versions the lockfile already had.

The description quotes the cargo test summary with the new test's name, the cargo clippy result, and the reasoning about the Postman collection in rust.yml. The full command output is in the transcript at GET /o/{org}/sessions/{id}/messages, and the existing workflow runs on the pull request with its Postgres service and API tests.

Before merging, a reviewer confirms the layer order in serve() is the one they want, decides whether br should be on for their clients or gzip alone is enough, and, if a CDN or load balancer already compresses in production, whether to keep this for direct access only. A follow-up message to the same session can add a size threshold or exclude a route.

Run it on every new Axum service

A team that stands up Axum services from a shared template sends one POST /o/{org}/sessions per repository, with this prompt as input and the repository in resources. Each run reads that service's own Cargo.toml and layer stack, so a service on a newer tower-http gets the feature names that version uses and a service already behind a compressing proxy gets a pull request that says so instead of a second layer.

A request_id that is a UUID v5 of the repository's full name makes a retried call return the run it already started. Give the API key a webhook_url and run.completed arrives with session_url, so a handler can read the pull request link and post it to the team's channel.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Compression is the kind of change a developer reads about, means to add, and never does, because proving it means a client that sends Accept-Encoding, a body big enough to matter and a look at the headers that come back. The agent does that in a sandbox of its own, with the crate versions the lockfile pins, and keeps the commands and their output.

The result is a pull request with the test that proves the layer works and the build output that proves nothing else broke, so a reviewer reads a short diff instead of setting up a toolchain. Several services can go through at once, each in its own sandbox with its own pull request.

Frequently asked questions

Will compressed responses break the Realworld Postman tests in CI?

No. The workflow runs the collection with newman, which sends Accept-Encoding and decodes the response before the assertions see it, so the JSON compares the same. The agent checks this and notes it in the pull request rather than changing the workflow.

Does this upgrade axum or tower-http?

No. The repository pins axum at 0.3.4 and tower-http at 0.2.0, and the change only adds the compression-gzip and compression-br features to the version already there. If you want a newer tower-http, ask for it in a follow-up message; it is a separate review.

Which encodings does it send?

Brotli when the client lists br, otherwise gzip when it lists gzip, otherwise the original bytes. Each encoding is a feature flag on tower-http, so dropping compression-br from Cargo.toml removes brotli without touching the code.

Does it compress request bodies or responses that are already compressed?

Request bodies are untouched; CompressionLayer only encodes responses. Responses the handler already encodes keep their Content-Encoding. This API returns JSON from every route, so in practice every response with a willing client is compressed.

More Axum jobs

The same job on other stacks

Keep reading