actix_web/
rt.rs

1//! A selection of re-exports from [`tokio`] and [`actix-rt`].
2//!
3//! Actix Web runs on [Tokio], providing full[^compat] compatibility with its huge ecosystem of
4//! crates. Each of the server's workers uses a single-threaded runtime. Read more about the
5//! architecture in [`actix-rt`]'s docs.
6//!
7//! # Running Actix Web Without Macros
8//!
9//! ```no_run
10//! use actix_web::{middleware, rt, web, App, HttpRequest, HttpServer};
11//!
12//! async fn index(req: HttpRequest) -> &'static str {
13//!     println!("REQ: {:?}", req);
14//!     "Hello world!\r\n"
15//! }
16//!
17//! fn main() -> std::io::Result<()> {
18//!     rt::System::new().block_on(
19//!         HttpServer::new(|| {
20//!             App::new().service(web::resource("/").route(web::get().to(index)))
21//!         })
22//!         .bind(("127.0.0.1", 8080))?
23//!         .run()
24//!     )
25//! }
26//! ```
27//!
28//! # Running Actix Web Using `#[tokio::main]`
29//!
30//! If you need to run something that uses Tokio's work stealing functionality alongside Actix Web,
31//! you can run Actix Web under `#[tokio::main]`. The [`Server`](crate::dev::Server) object returned
32//! from [`HttpServer::run`](crate::HttpServer::run) can also be [`spawn`]ed, if preferred.
33//!
34//! Note that `actix` actor support (and therefore WebSocket support through `actix-web-actors`)
35//! still require `#[actix_web::main]` since they require a [`System`] to be set up.
36//!
37//! Also note that calls to this module's [`spawn()`] re-export require an `#[actix_web::main]`
38//! runtime (or a manually configured `LocalSet`) since it makes calls into to the current thread's
39//! `LocalSet`, which `#[tokio::main]` does not set up.
40//!
41//! ```no_run
42//! use actix_web::{get, middleware, rt, web, App, HttpRequest, HttpServer};
43//!
44//! #[get("/")]
45//! async fn index(req: HttpRequest) -> &'static str {
46//!     println!("REQ: {:?}", req);
47//!     "Hello world!\r\n"
48//! }
49//!
50//! #[tokio::main]
51//! async fn main() -> std::io::Result<()> {
52//!     HttpServer::new(|| {
53//!         App::new().service(index)
54//!     })
55//!     .bind(("127.0.0.1", 8080))?
56//!     .run()
57//!     .await
58//! }
59//! ```
60//!
61//! [^compat]: Crates that use Tokio's [`block_in_place`] will not work with Actix Web. Fortunately,
62//!   the vast majority of Tokio-based crates do not use it.
63//!
64//! [`actix-rt`]: https://docs.rs/actix-rt
65//! [`tokio`]: https://docs.rs/tokio
66//! [Tokio]: https://docs.rs/tokio
67//! [`spawn`]: https://docs.rs/tokio/1/tokio/fn.spawn.html
68//! [`block_in_place`]: https://docs.rs/tokio/1/tokio/task/fn.block_in_place.html
69
70// In particular:
71// - Omit the `Arbiter` types because they have limited value here.
72// - Re-export but hide the runtime macros because they won't work directly but are required for
73//   `#[actix_web::main]` and `#[actix_web::test]` to work.
74
75#[cfg(feature = "macros")]
76#[doc(hidden)]
77pub use actix_macros::{main, test};
78pub use actix_rt::{net, pin, signal, spawn, task, time, Runtime, System, SystemRunner};