New in fly-replay: request transformation and JSON configuration

If you have been using Fly.io for any length of time, you have probably come across fly-replay. It’s the multi-tool that allows your app to reroute a request somewhere else, without having to build a proxy behind our proxy.

Until now, to use fly-replay, you would add one (or more) headers to your HTTP response. This isn’t going away, but we do have a shiny new way for you to configure that replay using the body of your response instead of the headers. To do so, set the content type of your response to application/vnd.fly.replay+json, and add everything else in a JSON object in the body.

If you’re programmatically building up a replay, this new format should be easier to construct and respond with. For example, if you’re responding from a simple app:

const replay = {
  "app": "example-app",
  "region": "syd",
  "cache": {
    "prefix": "/some/path/*",
    "ttl": 30
  }
};
return new Response(JSON.stringify(replay), {
  headers: {
    "Content-Type": "application/vnd.fly.replay+json"
  }
});

With this new format, from today, you can make some modifications to the request you’re replaying. To get things rolling, we have added the ability to change the request path, and modify the request headers.

This is great if you’re using fly-replay as a “router”. You might want to route /api/* from your web app to a dedicated API, or route the subdomains of your platform to an app for each customer. These things are possible already, but you have to get creative if you don’t want to replay the exact same request to the target app. Often, you’ll instead make a subrequest from your router app, forcing it to act as a full proxy that handles the entire request/response cycle. This is pretty heavy-handed if all you wanted to do was change the path of the request. So, now fly-replay can do that for you.

For the app replaying /api/* requests to a dedicated API, you can now strip the /api prefix. When your app receives a request to /api/users/1, it can replay it as /users/1:

{
    "app": "example-api-app",
    "transform": {
        "path": "/users/1"
    }
}

And for the app replaying customer.example.com to a customer-specific app, you could delete the incoming authentication header, and add a new internal header to authenticate to the customer:

{
    "app": "example-customer-app",
    "transform": {
        "delete_headers": ["cookie", "authorization"],
        "set_headers": [
            {"name": "x-example-token", "value": "internal_service_token" }
        ]
    }
}

The fly-replay docs have been updated to cover this new form and function.

10 Likes