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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*!
Byre is opinionated. It is a shallow wrapper around collection of crates.
Use byre as a means to avoid bikeshedding and bootstrap programs more quickly.
It provides:
* command line parsing (via clap)
* config file generation and loading (via Doku & Figment)
* environment variable overrides for configs (via Doku)
* logging & tracing & metrics (via tracing & opentelemetry)
### Tutorial
1. Start by adding the *latest* version of byre, doku, serde, and tokio to your Cargo.toml dependencies. (**NOTE:** check for *latest* versions)
```toml
[dependencies]
byre = "0.2"
doku = "0.21"
serde = "1"
tokio = "1"
```
2. Create a Settings struct that will be used to hold your application settings and the telemetry settings.
```rust
use doku::Document;
use serde::Deserialize;
/// Settings container
#[derive(Document, Deserialize)]
pub struct Settings {
/// App Settings
pub application: Application,
/// Telemetry settings.
pub telemetry: byre::telemetry::TelemetrySettings,
}
#[derive(Document, Deserialize)]
pub struct Application {
/// Port to listen on
#[doku(example = "8080")]
pub listen_port: u16,
/// Hostname to listen to
#[doku(example = "localhost")]
pub listen_host: String,
/// Directory where the application databases are located
#[doku(example = "/var/db/app_dbs")]
pub application_db_dir: std::path::PathBuf,
}
```
3. Have `byre` handle the CLI argument parsing, config, and env overrides:
```rust,no_run
# use doku::Document;
# use serde::Deserialize;
# #[derive(Document, Deserialize)]
# pub struct Settings {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse command line arguments. Add additional command line option that allows checking
// the config without running the server.
let service_info = byre::service_info!();
let cli = byre::cli::Cli::<Settings>::new(&service_info, "MYAPP_");
// ...
Ok(())
}
```
4. Initialize the `byre` telemetry
```rust,no_run
# use doku::Document;
# use serde::Deserialize;
# #[derive(Document, Deserialize)]
# pub struct Settings {
# /// Telemetry settings.
# pub telemetry: byre::telemetry::TelemetrySettings,
# }
# #[tokio::main]
# async fn main() -> Result<(), Box<dyn std::error::Error>> {
# // Parse command line arguments. Add additional command line option that allows checking
# // the config without running the server.
# let service_info = byre::service_info!();
let cli = byre::cli::Cli::<Settings>::new(&service_info, "MYAPP_");
let _telemetry = byre::telemetry::init(&service_info, &cli.config.telemetry)?;
#
# // ...
#
# Ok(())
# }
```
### Override config value via environment values
Environment variables can be used to override a setting from a config file.
Overrides can be nested. For example, to override the `application.listen_port` one would set an environment value like so, replacing the dot (`.`) with double underscores (`__`):
```sh
MYAPP_APPLICATION__LISTEN_PORT=8080 ./test_app --config ./test_app.toml
```
### Additional Command line arguments
Create a struct that represents the Arguments you want to check for. `clap` will need to be added to your Cargo.toml dependencies since `clap::Parser` is used.
```rust,no_run
# use doku::Document;
# #[derive(Document, Deserialize)]
# pub struct Settings {
# /// Telemetry settings.
# pub telemetry: byre::telemetry::TelemetrySettings,
# }
use clap::Parser;
use serde::{Deserialize, Serialize};
#[derive(Parser, Deserialize, Serialize)]
/// A NEW description, not using the one from Cargo.toml!
pub struct Arguments {
/// world peace, careful, has consequences
#[arg(short, long)]
pub enable_world_peace: bool,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse command line arguments. Add additional command line option that allows checking
// the config without running the server.
let service_info = byre::service_info!();
let cli = byre::cli::Cli::<Settings, Arguments>::new(&service_info, "MYAPP_");
let _telemetry = byre::telemetry::init(&service_info, &cli.config.telemetry)?;
// Check if world peace has been enabled
if cli.args.enable_world_peace {
// yay!
}
Ok(())
}
```
Notice that the description is overridden and there is an option to enable world peace.
```sh
❯ test_app --help
A NEW description, not using the one from Cargo.toml!
Usage: test_app [OPTIONS]
Options:
-e, --enable-world-peace world peace, careful, has consequences
-c, --config <config> Specifies the toml config file to run the service with
-g, --generate <generate> Generates a new default toml config file for the service
-h, --help Print help
-V, --version Print version
```
### Examples
There is a `full` example in the [source tree](https://github.com/halzy/byre/tree/main/examples).
*/
// Document ALL THE THINGS!
/// Cli initialization related errors
/// Global memory allocator backed by [jemalloc].
///
/// This static variable is exposed solely for the documentation purposes and don't need to be used
/// directly. If **jemalloc** feature is enabled then the service will use jemalloc for all the
/// memory allocations implicitly.
///
/// If no Foundations API is being used by your project, you will need to explicitly link foundations crate
/// to your project by adding `extern crate foundations;` to your `main.rs` or `lib.rs`, for jemalloc to
/// be embedded in your binary.
///
/// [jemalloc]: https://github.com/jemalloc/jemalloc
pub static JEMALLOC_MEMORY_ALLOCATOR: Jemalloc = Jemalloc;
/// Service information collected from the build.
// # #[tokio::main] async fn main() -> anyhow::Result<()> {
//
/**
Creates [`ServiceInfo`] from the information in `Cargo.toml` manifest of the service.
ServiceInfo is used to populate the client name for Telemetry and the CLI help.
```rust,no_run
use doku::Document;
use serde::Deserialize;
#[derive(Deserialize, Document)]
/// Data Archive Settings
pub(crate) struct Settings {
/// Server Settings
pub(crate) application: Application,
// Telemetry settings.
pub(crate) telemetry: byre::telemetry::TelemetrySettings,
}
#[derive(Deserialize, Document)]
pub(crate) struct Application {
/// DNS resolve this host and bind to its IP, ie: localhost
#[doku(example = "localhost")]
pub(crate) listen_host: String,
/// port to bind to
#[doku(example = "8080")]
pub(crate) listen_port: u16,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let service_info = byre::service_info!();
let cli = byre::cli::Cli::<Settings>::new(&service_info, "MYAPP_");
let _telemetry = byre::telemetry::init(&service_info, &cli.config.telemetry)?;
// ...
Ok(())
}
```
[`ServiceInfo::name_in_metrics`] is the same as the package name, with hyphens (`-`) replaced
by underscores (`_`).
*/