trillium_async_std/lib.rs
1#![forbid(unsafe_code)]
2#![deny(
3 clippy::dbg_macro,
4 missing_copy_implementations,
5 rustdoc::missing_crate_level_docs,
6 missing_debug_implementations,
7 missing_docs,
8 nonstandard_style,
9 unused_qualifications
10)]
11
12//! # Trillium server adapter for async-std
13//!
14//! ```rust,no_run
15//! # #[allow(clippy::needless_doctest_main)]
16//! fn main() {
17//! trillium_async_std::run(|conn: trillium::Conn| async move { conn.ok("hello async-std") });
18//! }
19//! ```
20//!
21//! ```rust,no_run
22//! # #[allow(clippy::needless_doctest_main)]
23//! #[async_std::main]
24//! async fn main() {
25//! trillium_async_std::run_async(
26//! |conn: trillium::Conn| async move { conn.ok("hello async-std") },
27//! )
28//! .await;
29//! }
30//! ```
31
32#[cfg(test)]
33#[doc = include_str!("../README.md")]
34mod readme {}
35
36use trillium::Handler;
37pub use trillium_server_common::{Binding, Swansong};
38
39mod client;
40pub use client::ClientConfig;
41
42mod server;
43pub use async_std;
44use server::Config;
45
46mod transport;
47pub use transport::AsyncStdTransport;
48
49/// # Runs a trillium handler in a sync context with default config
50///
51/// Runs a trillium handler on the async-std runtime with default
52/// configuration. See [`crate::config`] for what the defaults are and how
53/// to override them
54///
55///
56/// This function will block the current thread until the server shuts
57/// down
58pub fn run(handler: impl Handler) {
59 config().run(handler)
60}
61
62/// # Runs a trillium handler in an async context with default config
63///
64/// Run the provided trillium handler on an already-running async-std
65/// runtime with default settings. the defaults are the same as
66/// [`crate::run`]. To customize these settings, see [`crate::config`].
67///
68/// This function will poll pending until the server shuts down.
69pub async fn run_async(handler: impl Handler) {
70 config().run_async(handler).await
71}
72/// # Configures a server before running it
73///
74/// ## Defaults
75///
76/// The default configuration is as follows:
77///
78/// port: the contents of the `PORT` env var or else 8080
79/// host: the contents of the `HOST` env var or else "localhost"
80/// signals handling and graceful shutdown: enabled on cfg(unix) systems
81/// tcp nodelay: disabled
82/// tls acceptor: none
83///
84/// ## Usage
85///
86/// ```rust
87/// let swansong = trillium_async_std::Swansong::new();
88/// # swansong.shut_down(); // stoppping the server immediately for the test
89/// trillium_async_std::config()
90/// .with_port(0)
91/// .with_host("127.0.0.1")
92/// .without_signals()
93/// .with_nodelay()
94/// .with_acceptor(()) // see [`trillium_rustls`] and [`trillium_native_tls`]
95/// .with_swansong(swansong)
96/// .run(|conn: trillium::Conn| async move { conn.ok("hello async-std") });
97/// ```
98///
99/// See [`trillium_server_common::Config`] for more details
100pub fn config() -> Config<()> {
101 Config::new()
102}
103
104mod runtime;
105pub use runtime::AsyncStdRuntime;
106
107mod udp;
108pub use udp::AsyncStdUdpSocket;