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
#![deny(
    clippy::dbg_macro,
    missing_copy_implementations,
    rustdoc::missing_crate_level_docs,
    missing_debug_implementations,
    missing_docs,
    nonstandard_style,
    unused_qualifications
)]

/*!
# Trillium adapter using smol and async-global-executor

## Default / 12-factor applications

```rust,no_run
trillium_smol::run(|conn: trillium::Conn| async move {
    conn.ok("hello smol")
});
```

## Server configuration

For more details, see [trillium_smol::config](crate::config).

```rust
let stopper = trillium_smol::Stopper::new();
# stopper.stop(); // stoppping the server immediately for the test
trillium_smol::config()
    .with_port(0)
    .with_host("127.0.0.1")
    .without_signals()
    .with_nodelay()
    .with_acceptor(()) // see [`trillium_rustls`] and [`trillium_native_tls`]
    .with_stopper(stopper)
    .run(|conn: trillium::Conn| async move {
        conn.ok("hello smol")
    });
```

## Client

```rust
# #[cfg(feature = "smol")]
trillium_testing::with_server("ok", |url| async move {
    use trillium_smol::TcpConnector;
    use trillium_client::{Conn, Client};
    let mut conn = Conn::<TcpConnector>::get(url.clone()).execute().await?;
    assert_eq!(conn.response_body().read_string().await?, "ok");

    let client = Client::<TcpConnector>::new().with_default_pool();
    let mut conn = client.get(url);
    conn.send().await?;
    assert_eq!(conn.response_body().read_string().await?, "ok");
    Ok(())
});
```


*/

use trillium::Handler;
pub use trillium_server_common::{Binding, CloneCounterObserver, Stopper};

mod client;
pub use client::ClientConfig;

mod server;
use server::Config;

mod transport;
pub use transport::SmolTransport;

pub use async_global_executor;
pub use async_io;
pub use async_net;

/**
# Runs a trillium handler in a sync context with default config

Runs a trillium handler on the async-global-executor runtime with
default configuration. See [`crate::config`] for what the defaults are
and how to override them


This function will block the current thread until the server shuts
down
*/
pub fn run(handler: impl Handler) {
    config().run(handler)
}

/**
# Runs a trillium handler in an async context with default config

Run the provided trillium handler on an already-running async-executor
with default settings. The defaults are the same as [`crate::run`]. To
customize these settings, see [`crate::config`].

This function will poll pending until the server shuts down.

*/
pub async fn run_async(handler: impl Handler) {
    config().run_async(handler).await
}

/**
# Configures a server before running it

## Defaults

The default configuration is as follows:

* port: the contents of the `PORT` env var or else 8080
* host: the contents of the `HOST` env var or else "localhost"
* signals handling and graceful shutdown: enabled on cfg(unix) systems
* tcp nodelay: disabled
* tls acceptor: none

## Usage

```rust
let stopper = trillium_smol::Stopper::new();
# stopper.stop(); // stoppping the server immediately for the test
trillium_smol::config()
    .with_port(0)
    .with_host("127.0.0.1")
    .without_signals()
    .with_nodelay()
    .with_acceptor(()) // see [`trillium_rustls`] and [`trillium_native_tls`]
    .with_stopper(stopper)
    .run(|conn: trillium::Conn| async move {
        conn.ok("hello smol")
    });
```

See [`trillium_server_common::Config`] for more details

*/
pub fn config() -> Config<()> {
    Config::new()
}

/// spawn and detach a Future that returns ()
pub fn spawn<Fut: std::future::Future<Output = ()> + Send + 'static>(future: Fut) {
    async_global_executor::spawn(future).detach();
}