flytrap
Flytrap is a Rust crate for reading the Fly.io runtime environment.
- Read Fly.io environment variables like
$FLY_PUBLIC_IPinto astruct - Query Fly.io internal DNS addresses like
top3.nearest.of.<app>.internal - Query the Fly.io machines API
- Parse Fly.io request headers like
Fly-Client-IP(into anIpAddr) - Turn Fly.io region codes like
ordinto names like ”Chicago” and lat/long coordinates
A demo app is available at flytrap.fly.dev which shows this crate’s capabilities.
Usage
Flytrap can be added to your project with cargo:
Most of the crate’s features are enabled by default, but at your
option, you can enable only what you need by setting default-features = false.
Placement
The Placement type gives access to Fly.io runtime
environment variables like $FLY_PUBLIC_IP and $FLY_REGION.
use
Regions
Flytrap models Fly.io regions as an enum:
use ;
Regions implement Ord, and sort geographically:
If Flytrap receives a region code it doesn’t recognize (i.e., a region which
didn’t exist when your version of Flytrap was built), the raw code like ord
will be stored as a RegionCode.
(If you build Flytrap without the regions feature,
flytrap::Region simply becomes an alias for String.)
HTTP headers
The http module contains typed Header implementations of
the HTTP request headers added by Fly.io edge proxies, like
Fly-Client-IP.
use ;
use ;
async
DNS queries
Create a Resolver in order to query the Fly.io .internal DNS zone.
use Resolver;
async
Machines API requests
Create an api::Client to send requests to the machines API.
[!NOTE]
Theapimodule is not built by default; theapifeature must be enabled first.
use env;
use Client;
async
Features
Flytrap’s compilation can be controlled through a number of Cargo features.
dns: EnableResolverfor querying Fly.io internal DNS, viahickory-dns⭐detect: Enable automaticResolversetup for Wireguard VPN clients, viaif-addrs⭐️environment: Enable code which reads Fly.io environment variables like$FLY_PUBLIC_IP⭐️http: Enable types for HTTPheaderslikeFly-Client-IP⭐️nightly: Enable code which is only accepted by nightly Rust toolchainsregions: Enable theRegiontype andRegionDetailsstructures ⭐️serde: Enable Serde#[derive(Deserialize, Serialize)]on this crate’s typessystem-resolver: Enable theResolver::system()constructor, which reads/etc/resolv.conf
(Features marked with a ⭐️ are enabled by default.)