1
0
mirror of https://github.com/actix/actix-website synced 2024-12-03 20:22:13 +01:00
actix-website/static/api/actix-web/stable/actix_web/server/index.html
2018-10-10 08:38:33 -07:00

215 lines
19 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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="API documentation for the Rust `server` mod in crate `actix_web`."><meta name="keywords" content="rust, rustlang, rust-lang, server"><title>actix_web::server - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><p class='location'>Module server</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>actix_web</a></p><script>window.sidebarCurrent = {name: 'server', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press S to search, ? for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/actix_web/server/mod.rs.html#1-367' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>actix_web</a>::<wbr><a class="mod" href=''>server</a></span></h1><div class='docblock'><p>Http server module</p>
<p>The module contains everything necessary to setup
HTTP server.</p>
<p>In order to start HTTP server, first you need to create and configure it
using factory that can be supplied to <a href="fn.new.html">new</a>.</p>
<h2 id="factory" class="section-header"><a href="#factory">Factory</a></h2>
<p>Factory is a function that returns Application, describing how
to serve incoming HTTP requests.</p>
<p>As the server uses worker pool, the factory function is restricted to trait bounds
<code>Send + Clone + 'static</code> so that each worker would be able to accept Application
without a need for synchronization.</p>
<p>If you wish to share part of state among all workers you should
wrap it in <code>Arc</code> and potentially synchronization primitive like
<a href="https://doc.rust-lang.org/std/sync/struct.RwLock.html">RwLock</a>
If the wrapped type is not thread safe.</p>
<p>Note though that locking is not advisable for asynchronous programming
and you should minimize all locks in your request handlers</p>
<h2 id="https-support" class="section-header"><a href="#https-support">HTTPS Support</a></h2>
<p>Actix-web provides support for major crates that provides TLS.
Each TLS implementation is provided with <a href="trait.AcceptorService.html">AcceptorService</a>
that describes how HTTP Server accepts connections.</p>
<p>For <code>bind</code> and <code>listen</code> there are corresponding <code>bind_ssl|tls|rustls</code> and <code>listen_ssl|tls|rustls</code> that accepts
these services.</p>
<p><strong>NOTE:</strong> <code>native-tls</code> doesn't support <code>HTTP2</code> yet</p>
<h2 id="signal-handling-and-shutdown" class="section-header"><a href="#signal-handling-and-shutdown">Signal handling and shutdown</a></h2>
<p>By default HTTP Server listens for system signals
and, gracefully shuts down at most after 30 seconds.</p>
<p>Both signal handling and shutdown timeout can be controlled
using corresponding methods.</p>
<p>If worker, for some reason, unable to shut down within timeout
it is forcibly dropped.</p>
<h2 id="example" class="section-header"><a href="#example">Example</a></h2>
<div class='information'><div class='tooltip ignore'><span class='tooltiptext'>This example is not tested</span></div></div><pre class="rust rust-example-rendered ignore">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">actix</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">actix_web</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">rustls</span>;
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">http</span>, <span class="ident">middleware</span>, <span class="ident">server</span>, <span class="ident">App</span>, <span class="ident">Error</span>, <span class="ident">HttpRequest</span>, <span class="ident">HttpResponse</span>, <span class="ident">Responder</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">BufReader</span>;
<span class="kw">use</span> <span class="ident">rustls</span>::<span class="ident">internal</span>::<span class="ident">pemfile</span>::{<span class="ident">certs</span>, <span class="ident">rsa_private_keys</span>};
<span class="kw">use</span> <span class="ident">rustls</span>::{<span class="ident">NoClientAuth</span>, <span class="ident">ServerConfig</span>};
<span class="kw">fn</span> <span class="ident">index</span>(<span class="ident">req</span>: <span class="kw-2">&amp;</span><span class="ident">HttpRequest</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">HttpResponse</span>, <span class="ident">Error</span><span class="op">&gt;</span> {
<span class="prelude-val">Ok</span>(<span class="ident">HttpResponse</span>::<span class="prelude-val">Ok</span>().<span class="ident">content_type</span>(<span class="string">&quot;text/plain&quot;</span>).<span class="ident">body</span>(<span class="string">&quot;Welcome!&quot;</span>))
}
<span class="kw">fn</span> <span class="ident">load_ssl</span>() <span class="op">-&gt;</span> <span class="ident">ServerConfig</span> {
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">BufReader</span>;
<span class="kw">const</span> <span class="ident">CERT</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> [<span class="ident">u8</span>] <span class="op">=</span> <span class="macro">include_bytes</span><span class="macro">!</span>(<span class="string">&quot;../cert.pem&quot;</span>);
<span class="kw">const</span> <span class="ident">KEY</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> [<span class="ident">u8</span>] <span class="op">=</span> <span class="macro">include_bytes</span><span class="macro">!</span>(<span class="string">&quot;../key.pem&quot;</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cert</span> <span class="op">=</span> <span class="ident">BufReader</span>::<span class="ident">new</span>(<span class="ident">CERT</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">key</span> <span class="op">=</span> <span class="ident">BufReader</span>::<span class="ident">new</span>(<span class="ident">KEY</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">config</span> <span class="op">=</span> <span class="ident">ServerConfig</span>::<span class="ident">new</span>(<span class="ident">NoClientAuth</span>::<span class="ident">new</span>());
<span class="kw">let</span> <span class="ident">cert_chain</span> <span class="op">=</span> <span class="ident">certs</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">cert</span>).<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">keys</span> <span class="op">=</span> <span class="ident">rsa_private_keys</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">key</span>).<span class="ident">unwrap</span>();
<span class="ident">config</span>.<span class="ident">set_single_cert</span>(<span class="ident">cert_chain</span>, <span class="ident">keys</span>.<span class="ident">remove</span>(<span class="number">0</span>)).<span class="ident">unwrap</span>();
<span class="ident">config</span>
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">sys</span> <span class="op">=</span> <span class="ident">actix</span>::<span class="ident">System</span>::<span class="ident">new</span>(<span class="string">&quot;http-server&quot;</span>);
<span class="comment">// load ssl keys</span>
<span class="kw">let</span> <span class="ident">config</span> <span class="op">=</span> <span class="ident">load_ssl</span>();
<span class="comment">// create and start server at once</span>
<span class="ident">server</span>::<span class="ident">new</span>(<span class="op">||</span> {
<span class="ident">App</span>::<span class="ident">new</span>()
<span class="comment">// register simple handler, handle all methods</span>
.<span class="ident">resource</span>(<span class="string">&quot;/index.html&quot;</span>, <span class="op">|</span><span class="ident">r</span><span class="op">|</span> <span class="ident">r</span>.<span class="ident">f</span>(<span class="ident">index</span>))
}))
}).<span class="ident">bind_rustls</span>(<span class="string">&quot;127.0.0.1:8443&quot;</span>, <span class="ident">config</span>)
.<span class="ident">unwrap</span>()
.<span class="ident">start</span>();
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Started http server: 127.0.0.1:8080&quot;</span>);
<span class="comment">//Run system so that server would start accepting connections</span>
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="ident">sys</span>.<span class="ident">run</span>();
}</pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.HttpServer.html"
title='struct actix_web::server::HttpServer'>HttpServer</a></td>
<td class='docblock-short'>
<p>An HTTP Server</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.OpensslAcceptor.html"
title='struct actix_web::server::OpensslAcceptor'>OpensslAcceptor</a></td>
<td class='docblock-short'>
<p>Support <code>SSL</code> connections via openssl package</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.PauseServer.html"
title='struct actix_web::server::PauseServer'>PauseServer</a></td>
<td class='docblock-short'>
<p>Pause accepting incoming connections</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Request.html"
title='struct actix_web::server::Request'>Request</a></td>
<td class='docblock-short'>
<p>Request's context</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.ResumeServer.html"
title='struct actix_web::server::ResumeServer'>ResumeServer</a></td>
<td class='docblock-short'>
<p>Resume accepting incoming connections</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.ServerFlags.html"
title='struct actix_web::server::ServerFlags'>ServerFlags</a></td>
<td class='docblock-short'>
<p>Flags that can be used to configure HTTP Server.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.ServerSettings.html"
title='struct actix_web::server::ServerSettings'>ServerSettings</a></td>
<td class='docblock-short'>
<p>Various server settings</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.StopServer.html"
title='struct actix_web::server::StopServer'>StopServer</a></td>
<td class='docblock-short'>
<p>Stop incoming connection processing, stop all workers and exit.</p>
</td>
</tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table>
<tr class=' module-item'>
<td><a class="enum" href="enum.AcceptorError.html"
title='enum actix_web::server::AcceptorError'>AcceptorError</a></td>
<td class='docblock-short'>
<p>Errors produced by <code>AcceptorError</code> service.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.HttpDispatchError.html"
title='enum actix_web::server::HttpDispatchError'>HttpDispatchError</a></td>
<td class='docblock-short'>
<p>A set of errors that can occur during dispatching http requests</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.KeepAlive.html"
title='enum actix_web::server::KeepAlive'>KeepAlive</a></td>
<td class='docblock-short'>
<p>Server keep-alive setting</p>
</td>
</tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table>
<tr class=' module-item'>
<td><a class="trait" href="trait.HttpHandler.html"
title='trait actix_web::server::HttpHandler'>HttpHandler</a></td>
<td class='docblock-short'>
<p>Low level http request handler</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.HttpHandlerTask.html"
title='trait actix_web::server::HttpHandlerTask'>HttpHandlerTask</a></td>
<td class='docblock-short'>
<p>Low level http request handler</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.IntoHttpHandler.html"
title='trait actix_web::server::IntoHttpHandler'>IntoHttpHandler</a></td>
<td class='docblock-short'>
<p>Conversion helper trait</p>
</td>
</tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table>
<tr class=' module-item'>
<td><a class="fn" href="fn.new.html"
title='fn actix_web::server::new'>new</a></td>
<td class='docblock-short'>
<p>Create new http server with application factory.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="fn" href="fn.openssl_acceptor_with_flags.html"
title='fn actix_web::server::openssl_acceptor_with_flags'>openssl_acceptor_with_flags</a></td>
<td class='docblock-short'>
<p>Configure <code>SslAcceptorBuilder</code> with custom server flags.</p>
</td>
</tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd></kbd></dt><dd>Move up in search results</dd><dt><kbd></kbd></dt><dd>Move down in search results</dd><dt><kbd></kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "actix_web";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>