44 lines
8.0 KiB
HTML
44 lines
8.0 KiB
HTML
<!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 `actix_cors` crate."><meta name="keywords" content="rust, rustlang, rust-lang, actix_cors"><title>actix_cors - 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><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></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">☰</div><a href='../actix_cors/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Crate actix_cors</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all actix_cors's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'actix_cors', ty: 'mod', relpath: '../'};</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"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><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'>−</span>]</a></span><a class='srclink' href='../src/actix_cors/lib.rs.html#1-1173' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>actix_cors</a></span></h1><div class='docblock'><p>Cross-origin resource sharing (CORS) for Actix applications</p>
|
||
<p>CORS middleware could be used with application and with resource.
|
||
Cors middleware could be used as parameter for <code>App::wrap()</code>,
|
||
<code>Resource::wrap()</code> or <code>Scope::wrap()</code> methods.</p>
|
||
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">actix_cors</span>::<span class="ident">Cors</span>;
|
||
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">http</span>, <span class="ident">web</span>, <span class="ident">App</span>, <span class="ident">HttpRequest</span>, <span class="ident">HttpResponse</span>, <span class="ident">HttpServer</span>};
|
||
|
||
<span class="kw">fn</span> <span class="ident">index</span>(<span class="ident">req</span>: <span class="ident">HttpRequest</span>) <span class="op">-</span><span class="op">></span> <span class="kw-2">&</span><span class="lifetime">'static</span> <span class="ident">str</span> {
|
||
<span class="string">"Hello world"</span>
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></span> {
|
||
<span class="ident">HttpServer</span>::<span class="ident">new</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">App</span>::<span class="ident">new</span>()
|
||
.<span class="ident">wrap</span>(
|
||
<span class="ident">Cors</span>::<span class="ident">new</span>() <span class="comment">// <- Construct CORS middleware builder</span>
|
||
.<span class="ident">allowed_origin</span>(<span class="string">"https://www.rust-lang.org/"</span>)
|
||
.<span class="ident">allowed_methods</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="string">"GET"</span>, <span class="string">"POST"</span>])
|
||
.<span class="ident">allowed_headers</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="ident">http</span>::<span class="ident">header</span>::<span class="ident">AUTHORIZATION</span>, <span class="ident">http</span>::<span class="ident">header</span>::<span class="ident">ACCEPT</span>])
|
||
.<span class="ident">allowed_header</span>(<span class="ident">http</span>::<span class="ident">header</span>::<span class="ident">CONTENT_TYPE</span>)
|
||
.<span class="ident">max_age</span>(<span class="number">3600</span>))
|
||
.<span class="ident">service</span>(
|
||
<span class="ident">web</span>::<span class="ident">resource</span>(<span class="string">"/index.html"</span>)
|
||
.<span class="ident">route</span>(<span class="ident">web</span>::<span class="ident">get</span>().<span class="ident">to</span>(<span class="ident">index</span>))
|
||
.<span class="ident">route</span>(<span class="ident">web</span>::<span class="ident">head</span>().<span class="ident">to</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">HttpResponse</span>::<span class="ident">MethodNotAllowed</span>()))
|
||
))
|
||
.<span class="ident">bind</span>(<span class="string">"127.0.0.1:8080"</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</pre></div>
|
||
<p>In this example custom <em>CORS</em> middleware get registered for "/index.html"
|
||
endpoint.</p>
|
||
<p>Cors middleware automatically handle <em>OPTIONS</em> preflight request.</p>
|
||
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table><tr class='module-item'><td><a class="struct" href="struct.Cors.html" title='actix_cors::Cors struct'>Cors</a></td><td class='docblock-short'><p>Structure that follows the builder pattern for building <code>Cors</code> middleware
|
||
structs.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.CorsFactory.html" title='actix_cors::CorsFactory struct'>CorsFactory</a></td><td class='docblock-short'><p><code>Middleware</code> for Cross-origin resource sharing support</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.CorsMiddleware.html" title='actix_cors::CorsMiddleware struct'>CorsMiddleware</a></td><td class='docblock-short'><p><code>Middleware</code> for Cross-origin resource sharing support</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.AllOrSome.html" title='actix_cors::AllOrSome enum'>AllOrSome</a></td><td class='docblock-short'><p>An enum signifying that some of type T is allowed, or <code>All</code> (everything is
|
||
allowed).</p>
|
||
</td></tr><tr class='module-item'><td><a class="enum" href="enum.CorsError.html" title='actix_cors::CorsError enum'>CorsError</a></td><td class='docblock-short'><p>A set of errors that can occur during processing CORS</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "actix_cors";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |