Skip to main content

ntex/
lib.rs

1//! A framework for composable network services.
2//!
3//! ntex provides a single-threaded service framework, HTTP client and server
4//! implementations, a web application framework, and WebSocket support.
5//!
6//! The most commonly used entry points are:
7//!
8//! - [`web`] for HTTP applications and servers
9//! - [`client`] for outgoing HTTP requests
10//! - [`http`] for lower-level HTTP services and message types
11//! - [`service`](mod@service) for composing services and middleware
12//! - [`ws`] for WebSocket protocol support (enabled by the `ws` feature)
13//!
14//! The [framework guide](https://docs.ntex.rs/) explains these pieces in depth.
15//! Rules and templates for LLM-generated code are in
16//! [`llms.txt`](https://docs.ntex.rs/llms.txt).
17//!
18//! ## Runtime selection
19//!
20//! ntex uses its native Neon runtime by default. Enable `tokio` or `compio` to
21//! use the corresponding runtime backend. The `neon-polling` and `neon-uring`
22//! features select a specific native I/O reactor, Windows always uses IOCP.
23//!
24//! ## Crate features
25//!
26//! - `tokio` selects the Tokio runtime backend.
27//! - `compio` selects the Compio runtime backend.
28//! - `neon-polling` and `neon-uring` select the Neon runtime with the polling
29//!   reactor on Unix or the io-uring reactor on Linux.
30//! - `ws` (default) enables WebSocket support.
31//! - `openssl` enables TLS support through OpenSSL.
32//! - `rustls` enables TLS support through rustls.
33//! - `compress` enables HTTP content compression and decompression (gzip,
34//!   deflate and zstd).
35//! - `cookie` enables HTTP cookie support.
36//! - `no-test-logging` disables automatic logging configuration in
37//!   [`test`](macro@test).
38//! - `trace` enables additional trace-level logging.
39#![deny(clippy::pedantic)]
40#![allow(
41    type_alias_bounds,
42    missing_debug_implementations,
43    clippy::cast_possible_truncation,
44    clippy::missing_errors_doc,
45    clippy::missing_fields_in_debug,
46    clippy::missing_panics_doc,
47    clippy::must_use_candidate,
48    clippy::too_many_lines,
49    clippy::type_complexity,
50    clippy::unused_async_trait_impl
51)]
52// Used for fake variadics
53#![cfg_attr(any(docsrs, docsrs_dep), allow(internal_features))]
54#![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))]
55
56#[cfg(not(test))] // Work around for rust-lang/rust#62127
57pub use ntex_macros::{rt_main as main, rt_test as test};
58
59#[cfg(test)]
60pub(crate) use ntex_macros::rt_test_internal as rt_test;
61
62pub use ntex_service::forward;
63#[doc(hidden)]
64pub use ntex_service::{forward_bind, forward_ready, forward_shutdown};
65
66pub mod client;
67pub mod http;
68pub mod web;
69
70#[cfg(feature = "ws")]
71pub mod ws;
72
73pub use urly as url;
74
75pub use self::service::{
76    BindCtx, Ctx, IntoService, IntoServiceFactory, Middleware, Service, ServiceFactory, cfg::Cfg,
77    cfg::SharedCfg, factory, fn_service, pipeline::Pipeline, pipeline::PipelineBinding,
78    pipeline::PipelineFactory, service,
79};
80pub use ntex_error::Error;
81
82pub use ntex_util::{channel, task};
83
84pub mod codec {
85    //! Utilities for encoding and decoding frames.
86    pub use ntex_codec::*;
87}
88
89pub mod connect {
90    //! TCP connector services.
91    pub use ntex_net::connect::*;
92
93    #[cfg(feature = "openssl")]
94    pub mod openssl {
95        //! TLS connector support through OpenSSL.
96        pub use ntex_tls::openssl::*;
97    }
98
99    #[cfg(feature = "rustls")]
100    pub mod rustls {
101        //! TLS connector support through rustls.
102        pub use ntex_tls::rustls::*;
103    }
104}
105
106pub mod router {
107    //! Resource path matching library.
108    pub use ntex_router::*;
109}
110
111pub mod rt {
112    //! Runtime APIs for running tasks on the current thread.
113    pub use ntex_rt::*;
114
115    pub use ntex_net::*;
116}
117
118pub mod service {
119    //! Service, service factory, and middleware abstractions.
120    pub use ntex_service::*;
121}
122
123pub mod server {
124    //! General-purpose TCP server APIs.
125    pub use ntex_server::net::*;
126
127    #[cfg(feature = "openssl")]
128    pub use ntex_tls::openssl;
129
130    #[cfg(feature = "rustls")]
131    pub use ntex_tls::rustls;
132
133    pub use ntex_tls::{TlsConfig, TlsError};
134}
135
136pub mod time {
137    //! Utilities for tracking time.
138    pub use ntex_util::time::*;
139}
140
141pub mod io {
142    //! IO streaming utilities.
143    pub use ntex_dispatcher::*;
144    pub use ntex_io::*;
145}
146
147pub mod testing {
148    //! IO testing utilities.
149    pub use ntex_io::testing::IoTest;
150}
151
152pub mod tls {
153    //! TLS support for ntex ecosystem.
154    pub use ntex_tls::*;
155}
156
157pub mod error {
158    //! Error types and helpers.
159    pub use ntex_error::*;
160}
161
162pub mod util {
163    //! Common byte, collection, future, and service utilities.
164    pub use ntex_bytes::{Buf, BufMut, ByteString, Bytes, BytesMut};
165    pub use ntex_bytes::{BytePage, BytePageSize, BytePages};
166    #[cfg(feature = "tracker")]
167    pub use ntex_bytes::{BytesTracker, BytesTrackerSnapshot};
168    pub use ntex_error::{ErrorMessage, ErrorMessageChained, fmt_err, fmt_err_string};
169    pub use ntex_util::{
170        Equivalent, HashMap, HashRandomState, HashSet, clone_io_error, dyn_err, dyn_rc_err,
171        future::*, hash_map, hash_set, services::*, str_rc_err,
172    };
173
174    #[doc(hidden)]
175    pub fn enable_test_logging() {
176        #[cfg(not(feature = "no-test-logging"))]
177        if std::env::var("NTEX_NO_TEST_LOG").is_err() {
178            if std::env::var("RUST_LOG").is_err() {
179                // SAFETY: This requires initialization before other threads access the environment.
180                unsafe {
181                    std::env::set_var("RUST_LOG", "trace");
182                }
183            }
184            let _ = env_logger::builder().is_test(true).try_init();
185        }
186    }
187}