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
#![forbid(unsafe_code)]
#![deny(
    clippy::dbg_macro,
    missing_copy_implementations,
    rustdoc::missing_crate_level_docs,
    missing_debug_implementations,
    missing_docs,
    nonstandard_style,
    unused_qualifications
)]
/*!
# Trillium server adapter for tokio

```rust,no_run
# #[allow(clippy::needless_doctest_main)]
fn main() {
    trillium_tokio::run(|conn: trillium::Conn| async move {
        conn.ok("hello tokio")
    });
}
```

```rust,no_run
# #[allow(clippy::needless_doctest_main)]
#[tokio::main]
async fn main() {
    trillium_tokio::run_async(|conn: trillium::Conn| async move {
        conn.ok("hello tokio")
    }).await;
}
```
*/

use std::future::Future;

use trillium::Handler;

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

mod client;
pub use client::ClientConfig;

mod server;
use server::Config;

pub use async_compat;
pub use tokio;
pub use tokio_stream;

mod transport;
pub use transport::TokioTransport;

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

Runs a trillium handler on the tokio 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 tokio runtime
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_tokio::Stopper::new();
# stopper.stop(); // stoppping the server immediately for the test
trillium_tokio::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 tokio")
    });
```

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

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

/**
reexport tokio runtime block_on
*/
pub fn block_on<Fut: Future<Output = T>, T>(future: Fut) -> T {
    tokio::runtime::Runtime::new().unwrap().block_on(future)
}

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