1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-19 14:14:41 +01:00
actix-web/actix_web/struct.App.html

216 lines
47 KiB
HTML
Raw Normal View History

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="The top-level builder for an Actix Web application."><title>App in actix_web - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_web" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0-nightly (bdbbb6c6a 2024-05-26)" data-channel="nightly" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../actix_web/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../actix_web/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_web/index.html">actix_web</a><span class="version">4.6.0</span></h2></div><h2 class="location"><a href="#">App</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.app_data">app_data</a></li><li><a href="#method.configure">configure</a></li><li><a href="#method.data">data</a></li><li><a href="#method.data_factory">data_factory</a></li><li><a href="#method.default_service">default_service</a></li><li><a href="#method.external_resource">external_resource</a></li><li><a href="#method.new">new</a></li><li><a href="#method.route">route</a></li><li><a href="#method.service">service</a></li><li><a href="#method.wrap">wrap</a></li><li><a href="#method.wrap_fn">wrap_fn</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-RefUnwindSafe-for-App%3CT%3E">!RefUnwindSafe</a></li><li><a href="#impl-Send-for-App%3CT%3E">!Send</a></li><li><a href="#impl-Sync-for-App%3CT%3E">!Sync</a></li><li><a href="#impl-UnwindSafe-for-App%3CT%3E">!UnwindSafe</a></li><li><a href="#impl-Freeze-for-App%3CT%3E">Freeze</a></li><li><a href="#impl-Unpin-for-App%3CT%3E">Unpin</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T">Borrow&lt;T&gt;</a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T">BorrowMut&lt;T&gt;</a></li><li><a href="#impl-From%3CT%3E-for-T">From&lt;T&gt;</a></li><li><a href="#impl-Instrument-for-T">Instrument</a></li><li><a href="#impl-Into%3CU%3E-for-T">Into&lt;U&gt;</a></li><li><a href="#impl-Same-for-T">Same</a></li><li><a href="#impl-TryFrom%3CU%3E-for-T">TryFrom&lt;U&gt;</a></li><li><a href="#impl-TryInto%3CU%3E-for-T">TryInto&lt;U&gt;</a></li><li><a href="#impl-VZip%3CV%3E-for-T">VZip&lt;V&gt;</a></li><li><a href="#impl-WithSub
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-App%3CAppEntry%3E" class="impl"><a class="src rightside" href="../src/actix_web/app.rs.html#35-51">source</a><a href="#impl-App%3CAppEntry%3E" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;AppEntry&gt;</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.new" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#38-50">source</a><h4 class="code-header">pub fn <a href="#method.new" class="fn">new</a>() -&gt; Self</h4></section></summary><div class="docblock"><p>Create application builder. Application can be configured with a builder-like pattern.</p>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-App%3CT%3E" class="impl"><a class="src rightside" href="../src/actix_web/app.rs.html#53-435">source</a><a href="#impl-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;<div class="where">where
T: <a class="trait" href="dev/trait.ServiceFactory.html" title="trait actix_web::dev::ServiceFactory">ServiceFactory</a>&lt;<a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>, Config = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Error = <a class="struct" href="error/struct.Error.html" title="struct actix_web::error::Error">Error</a>, InitError = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.app_data" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#115-118">source</a><h4 class="code-header">pub fn <a href="#method.app_data" class="fn">app_data</a>&lt;U: 'static&gt;(self, ext: U) -&gt; Self</h4></section></summary><div class="docblock"><p>Set application (root level) data.</p>
<p>Application data stored with <code>App::app_data()</code> method is available through the
<a href="struct.HttpRequest.html#method.app_data" title="method actix_web::HttpRequest::app_data"><code>HttpRequest::app_data</code></a> method at runtime.</p>
<h5 id="datat"><a class="doc-anchor" href="#datat">§</a><a href="web/struct.Data.html" title="struct actix_web::web::Data"><code>Data&lt;T&gt;</code></a></h5>
<p>Any <a href="web/struct.Data.html" title="struct actix_web::web::Data"><code>Data&lt;T&gt;</code></a> type added here can utilize its extractor implementation in handlers.
Types not wrapped in <code>Data&lt;T&gt;</code> cannot use this extractor. See <a href="web/struct.Data.html" title="struct actix_web::web::Data">its docs</a> for more
about its usage and patterns.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::cell::Cell;
<span class="kw">use </span>actix_web::{web, App, HttpRequest, HttpResponse, Responder};
<span class="kw">struct </span>MyData {
count: std::cell::Cell&lt;usize&gt;,
}
<span class="kw">async fn </span>handler(req: HttpRequest, counter: web::Data&lt;MyData&gt;) -&gt; <span class="kw">impl </span>Responder {
<span class="comment">// note this cannot use the Data&lt;T&gt; extractor because it was not added with it
</span><span class="kw">let </span>incr = <span class="kw-2">*</span>req.app_data::&lt;usize&gt;().unwrap();
<span class="macro">assert_eq!</span>(incr, <span class="number">3</span>);
<span class="comment">// update counter using other value from app data
</span>counter.count.set(counter.count.get() + incr);
HttpResponse::Ok().body(counter.count.get().to_string())
}
<span class="kw">let </span>app = App::new().service(
web::resource(<span class="string">"/"</span>)
.app_data(<span class="number">3usize</span>)
.app_data(web::Data::new(MyData { count: Default::default() }))
.route(web::get().to(handler))
);</code></pre></div>
<h5 id="shared-mutable-state"><a class="doc-anchor" href="#shared-mutable-state">§</a>Shared Mutable State</h5>
<p><a href="struct.HttpServer.html#method.new" title="associated function actix_web::HttpServer::new"><code>HttpServer::new</code></a> accepts an application factory rather than an
application instance; the factory closure is called on each worker thread independently.
Therefore, if you want to share a data object between different workers, a shareable object
needs to be created first, outside the <code>HttpServer::new</code> closure and cloned into it.
<a href="web/struct.Data.html" title="struct actix_web::web::Data"><code>Data&lt;T&gt;</code></a> is an example of such a sharable object.</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested"></a><pre class="rust rust-example-rendered"><code><span class="kw">let </span>counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(<span class="number">0</span>),
});
HttpServer::new(<span class="kw">move </span>|| {
<span class="comment">// move counter object into the closure and clone for each worker
</span>App::new()
.app_data(counter.clone())
.route(<span class="string">"/"</span>, web::get().to(handler))
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.data" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#124-126">source</a><h4 class="code-header">pub fn <a href="#method.data" class="fn">data</a>&lt;U: 'static&gt;(self, data: U) -&gt; Self</h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 4.0.0: Use <code>.app_data(Data::new(val))</code> instead.</span></div></span></summary><div class="docblock"><p>Add application (root) data after wrapping in <code>Data&lt;T&gt;</code>.</p>
<p>Deprecated in favor of <a href="struct.App.html#method.app_data" title="method actix_web::App::app_data"><code>app_data</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.data_factory" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#134-161">source</a><h4 class="code-header">pub fn <a href="#method.data_factory" class="fn">data_factory</a>&lt;F, Out, D, E&gt;(self, data: F) -&gt; Self<div class="where">where
F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>() -&gt; Out + 'static,
Out: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;D, E&gt;&gt; + 'static,
D: 'static,
E: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>,</div></h4></section></summary><div class="docblock"><p>Add application data factory that resolves asynchronously.</p>
<p>Data items are constructed during application initialization, before the server starts
accepting requests.</p>
<p>The returned data value <code>D</code> is wrapped as <a href="web/struct.Data.html" title="struct actix_web::web::Data"><code>Data&lt;D&gt;</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.configure" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#185-202">source</a><h4 class="code-header">pub fn <a href="#method.configure" class="fn">configure</a>&lt;F&gt;(self, f: F) -&gt; Self<div class="where">where
F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(&amp;mut <a class="struct" href="web/struct.ServiceConfig.html" title="struct actix_web::web::ServiceConfig">ServiceConfig</a>),</div></h4></section></summary><div class="docblock"><p>Run external configuration as part of the application building
process</p>
<p>This function is useful for moving parts of configuration to a
different module or even library. For example,
some of the resources configuration could be moved to different module.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{web, App, HttpResponse};
<span class="comment">// this function could be located in different module
</span><span class="kw">fn </span>config(cfg: <span class="kw-2">&amp;mut </span>web::ServiceConfig) {
cfg.service(web::resource(<span class="string">"/test"</span>)
.route(web::get().to(|| HttpResponse::Ok()))
.route(web::head().to(|| HttpResponse::MethodNotAllowed()))
);
}
App::new()
.configure(config) <span class="comment">// &lt;- register resources
</span>.route(<span class="string">"/index.html"</span>, web::get().to(|| HttpResponse::Ok()));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.route" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#221-227">source</a><h4 class="code-header">pub fn <a href="#method.route" class="fn">route</a>(self, path: &amp;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>, route: <a class="struct" href="struct.Route.html" title="struct actix_web::Route">Route</a>) -&gt; Self</h4></section></summary><div class="docblock"><p>Configure route for a specific path.</p>
<p>This is a simplified version of the <code>App::service()</code> method.
This method can be used multiple times with same path, in that case
multiple resources with one route would be registered for same resource path.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{web, App, HttpResponse};
<span class="kw">async fn </span>index(data: web::Path&lt;(String, String)&gt;) -&gt; <span class="kw-2">&amp;</span><span class="lifetime">'static </span>str {
<span class="string">"Welcome!"
</span>}
<span class="kw">let </span>app = App::new()
.route(<span class="string">"/test1"</span>, web::get().to(index))
.route(<span class="string">"/test2"</span>, web::post().to(|| HttpResponse::MethodNotAllowed()));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.service" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#238-245">source</a><h4 class="code-header">pub fn <a href="#method.service" class="fn">service</a>&lt;F&gt;(self, factory: F) -&gt; Self<div class="where">where
F: <a class="trait" href="dev/trait.HttpServiceFactory.html" title="trait actix_web::dev::HttpServiceFactory">HttpServiceFactory</a> + 'static,</div></h4></section></summary><div class="docblock"><p>Register HTTP service.</p>
<p>Http service is any type that implements <code>HttpServiceFactory</code> trait.</p>
<p>Actix Web provides several services implementations:</p>
<ul>
<li><em>Resource</em> is an entry in resource table which corresponds to requested URL.</li>
<li><em>Scope</em> is a set of resources with common root path.</li>
<li>“StaticFiles” is a service for static files support</li>
</ul>
</div></details><details class="toggle method-toggle" open><summary><section id="method.default_service" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#266-280">source</a><h4 class="code-header">pub fn <a href="#method.default_service" class="fn">default_service</a>&lt;F, U&gt;(self, svc: F) -&gt; Self<div class="where">where
F: IntoServiceFactory&lt;U, <a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>&gt;,
U: <a class="trait" href="dev/trait.ServiceFactory.html" title="trait actix_web::dev::ServiceFactory">ServiceFactory</a>&lt;<a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>, Config = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Response = <a class="struct" href="dev/struct.ServiceResponse.html" title="struct actix_web::dev::ServiceResponse">ServiceResponse</a>, Error = <a class="struct" href="error/struct.Error.html" title="struct actix_web::error::Error">Error</a>&gt; + 'static,
U::<a class="associatedtype" href="dev/trait.ServiceFactory.html#associatedtype.InitError" title="type actix_web::dev::ServiceFactory::InitError">InitError</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>,</div></h4></section></summary><div class="docblock"><p>Default service that is invoked when no matching resource could be found.</p>
<p>You can use a <a href="struct.Route.html" title="struct actix_web::Route"><code>Route</code></a> as default service.</p>
<p>If a default service is not registered, an empty <code>404 Not Found</code> response will be sent to
the client instead.</p>
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{web, App, HttpResponse};
<span class="kw">async fn </span>index() -&gt; <span class="kw-2">&amp;</span><span class="lifetime">'static </span>str {
<span class="string">"Welcome!"
</span>}
<span class="kw">let </span>app = App::new()
.service(web::resource(<span class="string">"/index.html"</span>).route(web::get().to(index)))
.default_service(web::to(|| HttpResponse::NotFound()));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.external_resource" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#302-311">source</a><h4 class="code-header">pub fn <a href="#method.external_resource" class="fn">external_resource</a>&lt;N, U&gt;(self, name: N, url: U) -&gt; Self<div class="where">where
N: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>&gt;,
U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>&gt;,</div></h4></section></summary><div class="docblock"><p>Register an external resource.</p>
<p>External resources are useful for URL generation purposes only
and are never considered for matching at request time. Calls to
<code>HttpRequest::url_for()</code> will work as expected.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{web, App, HttpRequest, HttpResponse, <span class="prelude-ty">Result</span>};
<span class="kw">async fn </span>index(req: HttpRequest) -&gt; <span class="prelude-ty">Result</span>&lt;HttpResponse&gt; {
<span class="kw">let </span>url = req.url_for(<span class="string">"youtube"</span>, <span class="kw-2">&amp;</span>[<span class="string">"asdlkjqme"</span>])<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(url.as_str(), <span class="string">"https://youtube.com/watch/asdlkjqme"</span>);
<span class="prelude-val">Ok</span>(HttpResponse::Ok().into())
}
<span class="kw">let </span>app = App::new()
.service(web::resource(<span class="string">"/index.html"</span>).route(
web::get().to(index)))
.external_resource(<span class="string">"youtube"</span>, <span class="string">"https://youtube.com/watch/{video_id}"</span>);</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.wrap" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#340-371">source</a><h4 class="code-header">pub fn <a href="#method.wrap" class="fn">wrap</a>&lt;M, B&gt;(
self,
mw: M
) -&gt; <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;impl <a class="trait" href="dev/trait.ServiceFactory.html" title="trait actix_web::dev::ServiceFactory">ServiceFactory</a>&lt;<a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>, Config = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Response = <a class="struct" href="dev/struct.ServiceResponse.html" title="struct actix_web::dev::ServiceResponse">ServiceResponse</a>&lt;B&gt;, Error = <a class="struct" href="error/struct.Error.html" title="struct actix_web::error::Error">Error</a>, InitError = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;&gt;<div class="where">where
M: <a class="trait" href="dev/trait.Transform.html" title="trait actix_web::dev::Transform">Transform</a>&lt;T::<a class="associatedtype" href="dev/trait.ServiceFactory.html#associatedtype.Service" title="type actix_web::dev::ServiceFactory::Service">Service</a>, <a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>, Response = <a class="struct" href="dev/struct.ServiceResponse.html" title="struct actix_web::dev::ServiceResponse">ServiceResponse</a>&lt;B&gt;, Error = <a class="struct" href="error/struct.Error.html" title="struct actix_web::error::Error">Error</a>, InitError = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt; + 'static,
B: <a class="trait" href="body/trait.MessageBody.html" title="trait actix_web::body::MessageBody">MessageBody</a>,</div></h4></section></summary><div class="docblock"><p>Registers an app-wide middleware.</p>
<p>Registers middleware, in the form of a middleware component (type), that runs during
inbound and/or outbound processing in the request life-cycle (request -&gt; response),
modifying request/response as necessary, across all requests managed by the <code>App</code>.</p>
<p>Use middleware when you need to read or modify <em>every</em> request or response in some way.</p>
<p>Middleware can be applied similarly to individual <code>Scope</code>s and <code>Resource</code>s.
See <a href="struct.Scope.html#method.wrap" title="method actix_web::Scope::wrap"><code>Scope::wrap</code></a> and <a href="struct.Resource.html#method.wrap" title="method actix_web::Resource::wrap"><code>Resource::wrap</code></a>.</p>
<p>For more info on middleware take a look at the <a href="middleware/index.html" title="mod actix_web::middleware"><code>middleware</code> module</a>.</p>
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{middleware, web, App};
<span class="kw">async fn </span>index() -&gt; <span class="kw-2">&amp;</span><span class="lifetime">'static </span>str {
<span class="string">"Welcome!"
</span>}
<span class="kw">let </span>app = App::new()
.wrap(middleware::Logger::default())
.route(<span class="string">"/index.html"</span>, web::get().to(index));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.wrap_fn" class="method"><a class="src rightside" href="../src/actix_web/app.rs.html#408-434">source</a><h4 class="code-header">pub fn <a href="#method.wrap_fn" class="fn">wrap_fn</a>&lt;F, R, B&gt;(
self,
mw: F
) -&gt; <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;impl <a class="trait" href="dev/trait.ServiceFactory.html" title="trait actix_web::dev::ServiceFactory">ServiceFactory</a>&lt;<a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>, Config = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, Response = <a class="struct" href="dev/struct.ServiceResponse.html" title="struct actix_web::dev::ServiceResponse">ServiceResponse</a>&lt;B&gt;, Error = <a class="struct" href="error/struct.Error.html" title="struct actix_web::error::Error">Error</a>, InitError = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>&gt;&gt;<div class="where">where
F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(<a class="struct" href="dev/struct.ServiceRequest.html" title="struct actix_web::dev::ServiceRequest">ServiceRequest</a>, &amp;T::<a class="associatedtype" href="dev/trait.ServiceFactory.html#associatedtype.Service" title="type actix_web::dev::ServiceFactory::Service">Service</a>) -&gt; R + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + 'static,
R: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="struct" href="dev/struct.ServiceResponse.html" title="struct actix_web::dev::ServiceResponse">ServiceResponse</a>&lt;B&gt;, <a class="struct" href="error/struct.Error.html" title="struct actix_web::error::Error">Error</a>&gt;&gt;,
B: <a class="trait" href="body/trait.MessageBody.html" title="trait actix_web::body::MessageBody">MessageBody</a>,</div></h4></section></summary><div class="docblock"><p>Registers an app-wide function middleware.</p>
<p><code>mw</code> is a closure that runs during inbound and/or outbound processing in the request
life-cycle (request -&gt; response), modifying request/response as necessary, across all
requests handled by the <code>App</code>.</p>
<p>Use middleware when you need to read or modify <em>every</em> request or response in some way.</p>
<p>Middleware can also be applied to individual <code>Scope</code>s and <code>Resource</code>s.</p>
<p>See <a href="struct.App.html#method.wrap" title="method actix_web::App::wrap"><code>App::wrap</code></a> for details on how middlewares compose with each other.</p>
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{dev::Service <span class="kw">as _</span>, middleware, web, App};
<span class="kw">use </span>actix_web::http::header::{CONTENT_TYPE, HeaderValue};
<span class="kw">async fn </span>index() -&gt; <span class="kw-2">&amp;</span><span class="lifetime">'static </span>str {
<span class="string">"Welcome!"
</span>}
<span class="kw">let </span>app = App::new()
.wrap_fn(|req, srv| {
<span class="kw">let </span>fut = srv.call(req);
<span class="kw">async </span>{
<span class="kw">let </span><span class="kw-2">mut </span>res = fut.<span class="kw">await</span><span class="question-mark">?</span>;
res.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static(<span class="string">"text/plain"</span>));
<span class="prelude-val">Ok</span>(res)
}
})
.route(<span class="string">"/index.html"</span>, web::get().to(index));</code></pre></div>
</div></details></div></details></div><h2 id="synthetic-implementations" class="section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Freeze-for-App%3CT%3E" class="impl"><a href="#impl-Freeze-for-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;<div class="where">where
T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a>,</div></h3></section><section id="impl-RefUnwindSafe-for-App%3CT%3E" class="impl"><a href="#impl-RefUnwindSafe-for-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; !<a class="trait" href="https://doc.rust-lang.org/nightly/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;</h3></section><section id="impl-Send-for-App%3CT%3E" class="impl"><a href="#impl-Send-for-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; !<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;</h3></section><section id="impl-Sync-for-App%3CT%3E" class="impl"><a href="#impl-Sync-for-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; !<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;</h3></section><section id="impl-Unpin-for-App%3CT%3E" class="impl"><a href="#impl-Unpin-for-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;<div class="where">where
T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</div></h3></section><section id="impl-UnwindSafe-for-App%3CT%3E" class="impl"><a href="#impl-UnwindSafe-for-App%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; !<a class="trait" href="https://doc.rust-lang.org/nightly/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.App.html" title="struct actix_web::App">App</a>&lt;T&gt;</h3></section></div><h2 id="blanket-implementations" class="section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/any.rs.html#140">source</a><a href="#impl-Any-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<div class="where">where
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/any.rs.html#141">source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&amp;self) -&gt; <a class="struct" href="https://doc.rust-lang.org/nightly/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#208">source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a>&lt;T&gt; for T<div class="where">where
T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#210">source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#216">source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a>&lt;T&gt; for T<div class="where">where
T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#217">source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&amp;mut self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#765">source</a><a href="#impl-From%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#768">source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -&gt; T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Instrument-for-T" class="impl"><a href="#impl-Instrument-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; Instrument for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.instrument" class="method trait-impl"><a href="#method.instrument" class="anchor">§</a><h4 class="code-header">fn <a class="fn">instrument</a>(self, span: Span) -&gt; Instrumented&lt;Self&gt;</h4></section></summary><div class='docblock'>Instruments this type with the provided [<code>Span</code>], returning an
<code>Instrumented</code> wrapper. <a>Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.in_current_span" class="method trait-impl"><a href="#method.in_current_span" class="anchor">§</a><h4 class="code-header">fn <a class="fn">in_current_span</a>(self) -&gt; Instrumented&lt;Self&gt;</h4></section></summary><div class='docblock'>Instruments this type with the <a href="super::Span::current()">current</a> <a href="crate::Span"><code>Span</code></a>, returning an
<code>Instrumented</code> wrapper. <a>Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#748-750">source</a><a href="#impl-Into%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#758">source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html#tymethod.into" class="fn">into</a>(self) -&gt; U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
<p>That is, this conversion is whatever the implementation of
<code><a href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for U</code> chooses to do.</p>
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Same-for-T" class="impl"><a class="src rightside" href="https://docs.rs/typenum/1.17.0/src/typenum/type_operators.rs.html#34">source</a><a href="#impl-Same-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://docs.rs/typenum/1.17.0/typenum/type_operators/trait.Same.html" title="trait typenum::type_operators::Same">Same</a> for T</h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Output" class="associatedtype trait-impl"><a href="#associatedtype.Output" class="anchor">§</a><h4 class="code-header">type <a href="https://docs.rs/typenum/1.17.0/typenum/type_operators/trait.Same.html#associatedtype.Output" class="associatedtype">Output</a> = T</h4></section></summary><div class='docblock'>Should always be <code>Self</code></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#805-807">source</a><a href="#impl-TryFrom%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#812">source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, &lt;T as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#790-792">source</a><a href="#impl-TryInto%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = &lt;U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#797">source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;U, &lt;U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-VZip%3CV%3E-for-T" class="impl"><a href="#impl-VZip%3CV%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;V, T&gt; VZip&lt;V&gt; for T<div class="where">where
V: MultiLane&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><section id="method.vzip" class="method trait-impl"><a href="#method.vzip" class="anchor">§</a><h4 class="code-header">fn <a class="fn">vzip</a>(self) -&gt; V</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-WithSubscriber-for-T" class="impl"><a href="#impl-WithSubscriber-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; WithSubscriber for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.with_subscriber" class="method trait-impl"><a href="#method.with_subscriber" class="anchor">§</a><h4 class="code-header">fn <a class="fn">with_subscriber</a>&lt;S&gt;(self, subscriber: S) -&gt; WithDispatch&lt;Self&gt;<div class="where">where
S: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;Dispatch&gt;,</div></h4></section></summary><div class='docblock'>Attaches the provided <a href="super::Subscriber"><code>Subscriber</code></a> to this type, returning a
[<code>WithDispatch</code>] wrapper. <a>Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.with_current_subscriber" class="method trait-impl"><a href="#method.with_current_subscriber" class="anchor">§</a><h4 class="code-header">fn <a class="fn">with_current_subscriber</a>(self) -&gt; WithDispatch&lt;Self&gt;</h4></section></summary><div class='docblock'>Attaches the current <a href="crate::dispatcher#setting-the-default-subscriber">default</a> <a href="super::Subscriber"><code>Subscriber</code></a> to this type, returning a
[<code>WithDispatch</code>] wrapper. <a>Read more</a></div></details></div></details></div></section></div></main></body></html>