Skip to main content

ntex/web/
mod.rs

1//! Web application framework.
2//!
3//! ```rust,no_run
4//! use ntex::{web, SharedCfg};
5//!
6//! async fn index(info: web::types::Path<(String, u32)>) -> String {
7//!     format!("Hello {}! id:{}", info.0, info.1)
8//! }
9//!
10//! #[ntex::main]
11//! async fn main() -> std::io::Result<()> {
12//!     web::server(async |_| web::App::new().service(
13//!         web::resource("/{name}/{id}/index.html").to(index))
14//!     )
15//!         .bind("127.0.0.1:8080", SharedCfg::default())?
16//!         .run()
17//!         .await
18//! }
19//! ```
20//!
21//! ## Documentation and community resources
22//!
23//! Additional resources:
24//!
25//! - [User guide](https://ntex.rs)
26//! - [GitHub repository](https://github.com/ntex-rs/ntex)
27//! - [Cargo package](https://crates.io/crates/ntex)
28//!
29//! Useful API entry points include:
30//!
31//! - [`App`] configures routes, application state, and middleware.
32//! - [`HttpServer`] creates and configures HTTP server instances.
33//! - [`HttpRequest`] provides request metadata and application state.
34//! - [`HttpResponse`] and [`WebResponse`] represent HTTP responses.
35//! - [`resource()`] and [`route()`] configure resource and route matching.
36//!
37//! ## Features
38//!
39//! - HTTP/1.x and HTTP/2
40//! - Streaming and pipelining
41//! - Keep-alive connections and slow-request handling
42//! - WebSocket clients and servers
43//! - Transparent zstd, gzip, and deflate content encoding
44//! - Configurable request routing
45//! - TLS through OpenSSL or rustls
46//! - Composable middleware
47//!
48//! ## Crate features
49//!
50//! - `cookie` enables HTTP cookie support.
51//! - `compress` enables gzip, deflate and zstd content compression and
52//!   decompression.
53//! - `openssl` enables TLS support through OpenSSL.
54//! - `rustls` enables TLS support through rustls.
55//! - `ws` enables the [`ws`] module.
56#![allow(clippy::unused_async_trait_impl, clippy::mismatching_type_param_order)]
57mod app;
58mod app_service;
59mod config;
60pub mod error;
61mod error_default;
62mod extract;
63pub mod guard;
64mod handler;
65mod httprequest;
66mod info;
67pub mod middleware;
68mod request;
69mod resource;
70mod responder;
71mod response;
72mod rmap;
73mod route;
74mod scope;
75mod server;
76mod service;
77pub mod stack;
78mod state;
79pub mod test;
80pub mod types;
81mod util;
82
83#[cfg(feature = "ws")]
84pub mod ws;
85
86// re-export proc macro
87pub use ntex_macros::web_connect as connect;
88pub use ntex_macros::web_delete as delete;
89pub use ntex_macros::web_get as get;
90pub use ntex_macros::web_head as head;
91pub use ntex_macros::web_options as options;
92pub use ntex_macros::web_patch as patch;
93pub use ntex_macros::web_post as post;
94pub use ntex_macros::web_put as put;
95pub use ntex_macros::web_query as query;
96pub use ntex_macros::web_trace as trace;
97
98pub use crate::http::Response as HttpResponse;
99pub use crate::http::ResponseBuilder as HttpResponseBuilder;
100
101pub use self::app::{App, AppServices};
102pub use self::config::{ServiceConfig, WebAppConfig};
103pub use self::error::{DefaultError, InternalError, WebError, WebResponseError};
104pub use self::extract::FromRequest;
105pub use self::handler::{Handler, HandlerSt};
106pub use self::httprequest::HttpRequest;
107pub use self::info::ForwardedPolicy;
108pub use self::request::WebRequest;
109pub use self::resource::{Resource, ResourceServices};
110pub use self::responder::{CustomResponder, Responder};
111pub use self::response::WebResponse;
112pub use self::route::Route;
113pub use self::scope::{Scope, ScopeServices};
114pub use self::server::HttpServer;
115pub use self::service::WebServiceFactory;
116pub use self::state::{AppState, State};
117pub use self::util::*;
118
119use crate::error::Failure;
120use crate::service::boxed::{BoxService, BoxServiceFactory};
121
122pub(crate) type HttpHandler<St: State, In> =
123    BoxService<St, WebRequest<In>, WebResponse, WebError<St, St::Error>>;
124pub(crate) type HttpService<St: State, In> =
125    BoxServiceFactory<St, WebRequest<In>, WebResponse, WebError<St, St::Error>, Failure>;
126
127pub mod dev {
128    //! Internal web framework types commonly needed by library authors.
129    //!
130    //! Importing this module's contents can reduce repetitive imports in
131    //! libraries that build abstractions on top of [`crate::web`].
132
133    pub use crate::web::app_service::AppService;
134    pub use crate::web::info::ConnectionInfo;
135    pub use crate::web::rmap::ResourceMap;
136    pub use crate::web::route::IntoRoutes;
137    pub use crate::web::service::{WebServiceAdapter, WebServiceConfig, WebServiceFactory};
138
139    /// Application state used by `App::default()`.
140    pub type DefaultState = ();
141
142    use crate::web::{Handler, HandlerSt};
143
144    pub(crate) fn insert_slash(mut patterns: Vec<String>) -> Vec<String> {
145        for path in &mut patterns {
146            if !path.is_empty() && !path.starts_with('/') {
147                path.insert(0, '/');
148            }
149        }
150        patterns
151    }
152
153    #[doc(hidden)]
154    #[inline]
155    pub fn __assert_handler<St, In, Fun, Res>(f: Fun) -> impl Handler<St, (), Output = Res>
156    where
157        St: super::State,
158        Fun: AsyncFn() -> Res + 'static,
159        Res: super::Responder<St>,
160    {
161        f
162    }
163
164    #[doc(hidden)]
165    #[inline]
166    pub fn __assert_handler_st<St, In, Fun, Res>(f: Fun) -> impl HandlerSt<St, In, (), Output = Res>
167    where
168        St: super::State,
169        Fun: AsyncFn(&St, In) -> Res + 'static,
170        Res: super::Responder<St>,
171    {
172        f
173    }
174
175    macro_rules! assert_handler ({ $name:ident, $($T:ident),+} => {
176        #[doc(hidden)]
177        #[inline(always)]
178        pub fn $name<St, Fun, Res, $($T,)+>(
179            f: Fun,
180        ) -> impl Handler<St, ($($T,)+), Output = Res>
181        where
182            St: $crate::web::State,
183            Fun: AsyncFn($($T,)+) -> Res + 'static,
184            Res: super::Responder<St> + 'static,
185           $($T: $crate::web::FromRequest<St>),+,
186        {
187            f
188        }
189    });
190
191    assert_handler!(__assert_handler1, A);
192    assert_handler!(__assert_handler2, A, B);
193    assert_handler!(__assert_handler3, A, B, C);
194    assert_handler!(__assert_handler4, A, B, C, D);
195    assert_handler!(__assert_handler5, A, B, C, D, E);
196    assert_handler!(__assert_handler6, A, B, C, D, E, F);
197    assert_handler!(__assert_handler7, A, B, C, D, E, F, G);
198    assert_handler!(__assert_handler8, A, B, C, D, E, F, G, H);
199    assert_handler!(__assert_handler9, A, B, C, D, E, F, G, H, I);
200    assert_handler!(__assert_handler10, A, B, C, D, E, F, G, H, I, J);
201
202    macro_rules! assert_handler_st ({ $name:ident, $($T:ident),+} => {
203        #[doc(hidden)]
204        #[inline(always)]
205        pub fn $name<St, In, Fun, Res, $($T,)+>(
206            f: Fun,
207        ) -> impl HandlerSt<St, In, ($($T,)+), Output = Res>
208        where
209            St: $crate::web::State,
210            Fun: AsyncFn(&St, In, $($T,)+) -> Res + 'static,
211            Res: super::Responder<St> + 'static,
212           $($T: $crate::web::FromRequest<St>),+,
213        {
214            f
215        }
216    });
217
218    assert_handler_st!(__assert_handler_st1, A);
219    assert_handler_st!(__assert_handler_st2, A, B);
220    assert_handler_st!(__assert_handler_st3, A, B, C);
221    assert_handler_st!(__assert_handler_st4, A, B, C, D);
222    assert_handler_st!(__assert_handler_st5, A, B, C, D, E);
223    assert_handler_st!(__assert_handler_st6, A, B, C, D, E, F);
224    assert_handler_st!(__assert_handler_st7, A, B, C, D, E, F, G);
225    assert_handler_st!(__assert_handler_st8, A, B, C, D, E, F, G, H);
226    assert_handler_st!(__assert_handler_st9, A, B, C, D, E, F, G, H, I);
227    assert_handler_st!(__assert_handler_st10, A, B, C, D, E, F, G, H, I, J);
228}