forked from mainmatter/gerust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
68 lines (59 loc) · 2.76 KB
/
lib.rs
File metadata and controls
68 lines (59 loc) · 2.76 KB
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
//! The `{{crate_name}}_web` crate contains the application's web interface which mainly are controllers implementing HTTP endpoints. It also includes the application tests that are black-box tests, interfacing with the application like any other HTTP client.
use anyhow::Context;
use axum::serve;
use {{crate_name}}_config::{Config, get_env, load_config};
use tokio::net::TcpListener;
use tracing::info;
use tracing_panic::panic_hook;
use tracing_subscriber::{filter::EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
/// The application's controllers that implement request handlers.
pub mod controllers;
/// Middlewares that incoming requests are passed through before being passed to [`controllers`].
pub mod middlewares;
/// Contains the application's route definitions.
pub mod routes;
/// Contains the application state definition and functionality to initialize it.
pub mod state;
/// Contains the application's error type and related conversion implementation.
pub mod error;
/// Runs the application.
///
/// This function does all the work to initiatilize and run the application:
///
/// 1. Determine the environment the application is running in (see [`{{crate_name}}_config::get_env`])
/// 2. Load the configuration (see [`{{crate_name}}_config::load_config`])
/// 3. Initialize the application state (see [`state::init_app_state`])
/// 4. Initialize the application's router (see [`routes::init_routes`])
/// 5. Boot the application and start listening for requests on the configured interface and port
pub async fn run() -> anyhow::Result<()> {
let env = get_env().context("Cannot get environment!")?;
let config: Config = load_config(&env).context("Cannot load config!")?;
let app_state = state::init_app_state(config.clone()).await;
let app = routes::init_routes(app_state);
let addr = config.server.addr();
let listener = TcpListener::bind(&addr).await?;
info!("Listening on {}", &addr);
serve(listener, app.into_make_service()).await?;
Ok(())
}
/// Initializes tracing.
///
/// This function
///
/// * registers a [`tracing_subscriber::fmt::Subscriber`]
/// * registers a [`tracing_panic::panic_hook`]
///
/// The function respects the `RUST_LOG` if set or defaults to filtering spans and events with level [`tracing_subscriber::filter::LevelFilter::INFO`] and higher.
pub fn init_tracing() {
let filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.expect("Should be able to parse RUST_LOG or construct default filter from info level");
tracing_subscriber::registry()
.with(fmt::layer())
.with(filter)
.init();
std::panic::set_hook(Box::new(panic_hook));
}
/// Helpers that simplify writing application tests.
#[cfg(feature = "test-helpers")]
pub mod test_helpers;