1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Flytrap is a crate for reading the [Fly.io][] runtime [environment][].
//!
//! [Fly.io]: https://fly.io/
//! [environment]: https://fly.io/docs/reference/runtime-environment/
//!
//! - Read Fly.io [environment variables][env-vars] like `$FLY_PUBLIC_IP` into a `struct`
//! - Query Fly.io [internal DNS][dns] addresses like `top3.nearest.of.<app>.internal`
//! - Parse Fly.io [request headers][] like `Fly-Client-IP` (into an [`IpAddr`][std::net::IpAddr])
//! - Turn Fly.io [region][regions] codes like `ord` into names like ”Chicago” and lat/long coordinates
//!
//! [env-vars]: https://fly.io/docs/reference/runtime-environment/#environment-variables
//! [dns]: https://fly.io/docs/reference/private-networking/#fly-internal-addresses
//! [request headers]: https://fly.io/docs/reference/runtime-environment/#request-headers
//! [regions]: https://fly.io/docs/reference/regions/
//!
//! A [demo app][] is available at [**flytrap.fly.dev**](https://flytrap.fly.dev) which shows this crate’s capabilities.
//!
//! [demo app]: https://github.com/silverlyra/flytrap/blob/main/examples/server.rs
//!
//! ## Usage
//!
//! ### Placement
//!
//! The [`Placement`] type gives access to Fly.io runtime [environment
//! variables][env-vars] like `$FLY_PUBLIC_IP` and `$FLY_REGION`.
//!
//! ```no_run
//! use flytrap::{Placement, Machine};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = Placement::current()?;
//!
//! println!("Fly.io app: {}", runtime.app);
//! println!(" region: {}", runtime.location);
//!
//! if let Some(Machine{ id, memory: Some(memory), image: Some(image), .. }) = runtime.machine {
//! println!(" machine: {id} ({memory} MB) running {image}");
//! }
//!
//! if let Some(public_ip) = runtime.public_ip {
//! println!(" public IP: {}", public_ip);
//! }
//! println!("private IP: {}", runtime.private_ip);
//!
//! Ok(())
//! }
//! ```
//!
//! #### Regions
//!
//! Flytrap models Fly.io [regions][] as an `enum`:
//!
//! ```no_run
//! use flytrap::{City, Placement, Region};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = Placement::current()?;
//! let region = runtime.region().unwrap_or(Region::Guadalajara);
//!
//! show(region);
//! Ok(())
//! }
//!
//! fn show(region: Region) {
//! let City { name, country, geo } = region.city;
//! println!("Running in {name} ({country}) @ {}, {}", geo.x(), geo.y());
//! }
//! ```
//!
//! Regions implement [`Ord`], and sort geographically:
//!
//! ```rust
//! # fn main() {
//! use flytrap::Region::*;
//!
//! let mut regions = [Bucharest, Chicago, HongKong, Johannesburg,
//! LosAngeles, Madrid, Santiago, Tokyo];
//! regions.sort();
//!
//! assert_eq!(regions, [LosAngeles, Chicago, Santiago, Madrid,
//! Bucharest, Johannesburg, HongKong, Tokyo]);
//! # }
//! ```
//!
//! ### DNS queries
//!
//! Create a [`Resolver`] in order to query the Fly.io [`.internal` DNS zone][dns].
//!
//! ```no_run
//! use flytrap::Resolver;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let resolver = Resolver::new()?;
//!
//! // Discover all instances of the currently-running app
//! let peers = resolver.current()?.peers().await?;
//!
//! for peer in peers {
//! println!("peer {} in {} @ {}", peer.id, peer.location, peer.private_ip);
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Features
//!
//! Flytrap’s compilation can be controlled through a number of [Cargo features][].
//!
//! [Cargo features]: https://doc.rust-lang.org/cargo/reference/features.html
//!
//! - **`dns`**: Enable [`Resolver`] for querying Fly.io [internal DNS][dns], via [`hickory-dns`][hickory] ⭐
//! - **`detect`**: Enable automatic [`Resolver`] setup for Wireguard VPN clients, via [`if-addrs`][if-addrs] ⭐️
//! - **`environment`**: Enable code which reads Fly.io environment variables like `$FLY_PUBLIC_IP` ⭐️
//! - **`http`**: Enable types for HTTP [`headers`][headers] like [`Fly-Client-IP`][http::FlyClientIp] ⭐️
//! - **`nightly`**: Enable code which is only accepted by nightly Rust toolchains
//! - **`regions`**: Enable the [`Region`] type and [`RegionDetails`] structures ⭐️
//! - **`serde`**: Enable [Serde][serde] `#[derive(Deserialize, Serialize)]` on this crate’s types
//! - **`system-resolver`**: Enable the [`Resolver::system()`][Resolver::system] constructor, which reads `/etc/resolv.conf`
//!
//! _(Features marked with a ⭐️ are enabled by default.)_
//!
//! [headers]: https://docs.rs/headers/latest/headers/trait.Header.html
//! [hickory]: https://lib.rs/crates/hickory-resolver
//! [if-addrs]: https://lib.rs/crates/if-addrs
//! [serde]: https://serde.rs/
pub use AppResolver;
pub use Error;
pub use ;
pub use ;
pub use ;
pub type Location = String;
pub type Region = String;