kayrx 0.7.5

The Kayrx Framework
Documentation
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="icon" href="https://avatars0.githubusercontent.com/u/58360786?s=200&v=4" title="Kayrx">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.css">
    <script src="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
    <link rel="stylesheet" type="text/css" href="https://kayrx.xyz/home.css" />
    <title>Kayrx / start</title>
  </head>
  <body class="vscode-light">
    <div id="page" class="page-section-doc">
      <div id="mnav">
    
        <div id="lnav">
          <div id="left">
            <a id="front" href="/"><img src="/kayrx.png"/></a>
            <span id="dlnav">
              <a class="tail" href="/kayrx/" >Kayrx</a>
              <a class="tail" href="/yarx" >Yarx</a>
              <a class="tail" href="/blog" >Blog</a>
            </span>
          </div>
          <label ><a href="#" id="menu">M</a></label>
        </div>
        <div id="rnav">
            <li > <a href="https://spectrum.chat/kayrx" target="_black" style="color:green; font-weight: bold;" >群聊</a></li>
            <li ><a  href="/community" >社区</a></li>
            <li > <a href="/funding">赞助</a></li>
        </div> 
      </div>

      <div id="content">
        <div class="container"><div class="doc-page doc-section-basic-start">
  <div class="doc-nav">
      
  <details id="pc" open>
      <summary id="dir"><strong>目录指南</strong></summary>

      <details open>
        <summary id="title"><strong>介绍</strong></summary>
        <a class="doc-section-link-introduction-index" href="/kayrx/introduction/index.html">欢迎</a>
        <a class="doc-section-link-introduction-whatis" href="/kayrx/introduction/whatis.html">何为Actix</a>
        <a class="doc-section-link-introduction-install" href="/kayrx/introduction/install.html">安装</a>
      </details>
      
      <details open>
        <summary id="title"><strong>基本</strong></summary>
        <a class="doc-section-link-basic-start" href="/kayrx/basic/start.html">入门</a>
        <a class="doc-section-link-basic-app" href="/kayrx/basic/app.html">应用</a>
        <a class="doc-section-link-basic-server" href="/kayrx/basic/server.html">服务器</a>
        <a class="doc-section-link-basic-handler" href="/kayrx/basic/handler.html">处理器</a>
        <a class="doc-section-link-basic-extractor" href="/kayrx/basic/extractor.html">提取器</a>
      </details>
      
      <details open>
        <summary id="title"><strong>高级</strong></summary>
        <a class="doc-section-link-advance-error" href="/kayrx/advance/error.html">错误</a>
        <a class="doc-section-link-advance-url-dispatch" href="/kayrx/advance/url-dispatch.html">路由分发</a>
        <a class="doc-section-link-advance-request" href="/kayrx/advance/request.html">请求</a>
        <a class="doc-section-link-advance-response" href="/kayrx/advance/response.html">响应</a>
        <a class="doc-section-link-advance-test" href="/kayrx/advance/test.html">测试</a>
        <a class="doc-section-link-advance-middleware" href="/kayrx/advance/middleware.html">中间件</a>
        <a class="doc-section-link-advance-static-file" href="/kayrx/advance/static-file.html">静态文件</a>
      </details>
      
      <details open>
        <summary id="title"><strong>协议</strong></summary>
        <a class="doc-section-link-protocol-websocket" href="/kayrx/protocol/websocket.html">Websocket</a>
        <a class="doc-section-link-protocol-http2" href="/kayrx/protocol/http2.html">HTTP2</a>
      </details>
      
      <details open>
        <summary id="title"><strong>模式</strong></summary>
        <a class="doc-section-link-pattern-autoreload" href="/kayrx/pattern/autoreload.html">自动重载</a>
        <a class="doc-section-link-pattern-database" href="/kayrx/pattern/database.html">数据库</a>
      </details>
      
  </details>

  <!-- ===============================Mobile==================================== -->

  <details id="mobile">
      <summary id="dir"><strong>目录指南</strong></summary>

      <details open>
        <summary id="title"><strong>介绍</strong></summary>
        <a class="doc-section-link-introduction-index" href="/kayrx/introduction/index.html">欢迎</a>
        <a class="doc-section-link-introduction-whatis" href="/kayrx/introduction/whatis.html">何为Actix</a>
        <a class="doc-section-link-introduction-install" href="/kayrx/introduction/install.html">安装</a>
      </details>
      
      <details open>
        <summary id="title"><strong>基本</strong></summary>
        <a class="doc-section-link-basic-start" href="/kayrx/basic/start.html">入门</a>
        <a class="doc-section-link-basic-app" href="/kayrx/basic/app.html">应用</a>
        <a class="doc-section-link-basic-server" href="/kayrx/basic/server.html">服务器</a>
        <a class="doc-section-link-basic-handler" href="/kayrx/basic/handler.html">处理器</a>
        <a class="doc-section-link-basic-extractor" href="/kayrx/basic/extractor.html">提取器</a>
      </details>
      
      <details open>
        <summary id="title"><strong>高级</strong></summary>
        <a class="doc-section-link-advance-error" href="/kayrx/advance/error.html">错误</a>
        <a class="doc-section-link-advance-url-dispatch" href="/kayrx/advance/url-dispatch.html">路由分发</a>
        <a class="doc-section-link-advance-request" href="/kayrx/advance/request.html">请求</a>
        <a class="doc-section-link-advance-response" href="/kayrx/advance/response.html">响应</a>
        <a class="doc-section-link-advance-test" href="/kayrx/advance/test.html">测试</a>
        <a class="doc-section-link-advance-middleware" href="/kayrx/advance/middleware.html">中间件</a>
        <a class="doc-section-link-advance-static-file" href="/kayrx/advance/static-file.html">静态文件</a>
      </details>
      
      <details open>
        <summary id="title"><strong>协议</strong></summary>
        <a class="doc-section-link-protocol-websocket" href="/kayrx/protocol/websocket.html">Websocket</a>
        <a class="doc-section-link-protocol-http2" href="/kayrx/protocol/http2.html">HTTP2</a>
      </details>
      
      <details open>
        <summary id="title"><strong>模式</strong></summary>
        <a class="doc-section-link-pattern-autoreload" href="/kayrx/pattern/autoreload.html">自动重载</a>
        <a class="doc-section-link-pattern-database" href="/kayrx/pattern/database.html">数据库</a>
      </details>
      
  </details>


  </div>
  <div class="doc-content">        <h1 id="getting-started">Getting Started</h1>
<p>Let’s write our first <code>actix-web</code> application!</p>
<h2 id="hello-world">Hello, world!</h2>
<p>Start by creating a new binary-based Cargo project and changing into the new directory:</p>
<pre><code class="language-bash"><div>cargo new hello-world
<span class="hljs-built_in">cd</span> hello-world
</div></code></pre>
<p>Now, add <code>actix-web</code> as a dependency of your project by ensuring your <code>Cargo.toml</code>
contains the following:</p>
<pre><code class="language-ini"><div><span class="hljs-section">[dependencies]</span>
<span class="hljs-attr">actix-web</span> = <span class="hljs-string">"{{&lt; actix-version "</span>actix-web<span class="hljs-string">" &gt;}}"</span>
</div></code></pre>
<p>If you want to use the <code>#[actix_rt::main]</code> macro, you have to add <code>actix-rt</code> to your dependency.
Now your <code>Cargo.toml</code> should look like following:</p>
<pre><code class="language-ini"><div><span class="hljs-section">[dependencies]</span>
<span class="hljs-attr">actix-web</span> = <span class="hljs-string">"{{&lt; actix-version "</span>actix-web<span class="hljs-string">" &gt;}}"</span>
<span class="hljs-attr">actix-rt</span> = <span class="hljs-string">"{{&lt; actix-version "</span>actix-rt<span class="hljs-string">" &gt;}}"</span>
</div></code></pre>
<p>In order to implement a web server, we first need to create a request handler.</p>
<p>A request handler is an async function that accepts zero or more parameters that can be
extracted from a request (ie, <code>impl FromRequest</code>) and returns a type that can be
converted into an <code>HttpResponse</code> (ie, <code>impl Responder</code>):</p>
<p>{{&lt; include-example example=&quot;getting-started&quot; section=&quot;setup&quot; &gt;}}</p>
<p>Next, create an <code>App</code> instance and register the request handler with the application's
<code>route</code> on a <em>path</em> and with a particular <em>HTTP method</em>. After that, the application
instance can be used with <code>HttpServer</code> to listen for incoming connections. The server
accepts a function that should return an application factory.</p>
<p>{{&lt; include-example example=&quot;getting-started&quot; section=&quot;main&quot; &gt;}}</p>
<p>That's it! Now, compile and run the program with <code>cargo run</code>.
Head over to <code>http://localhost:8088/</code> to see the results.</p>
<p><strong>Note</strong>: You could notice <code>#[actix_rt::main]</code> attribute macro. This
macro executes marked async function in actix runtime. Any async function
could be marked and executed by this macro.</p>
<h3 id="using-attribute-macros-to-define-routes">Using Attribute Macros to Define Routes</h3>
<p>Alternatively, you can define routes using macro attributes which
allow you to specify the routes above your functions like so:</p>
<p>{{&lt; include-example example=&quot;getting-started&quot; section=&quot;macro-attributes&quot;&gt;}}</p>
<p>You can then register the route using <code>service()</code>:</p>
<pre><code class="language-rust"><div>App::new()
    .service(index3)
</div></code></pre>
<p>For consistency reasons, this documentation only uses the explicit syntax shown at the
beginning of this page. However, if you prefer this syntax you should feel free to
use it any time you declare a route as it's only syntactic sugar.</p>
<p>To learn more, see <a href="https://docs.rs/actix-web-codegen/">actix-web-codegen</a>.</p>
<h3 id="auto-reloading">Auto-reloading</h3>
<p>If you want, you can have an automatically reloading server during development
that recompiles on demand. This isn't necessary, but it makes rapid prototyping
more convenient as you can see changes instantly upon saving.
To see how this can be accomplished, have a look at the <a href="../autoreload/">autoreload pattern</a>.</p>



</div>
</div>

</div>
      </div>

      <div id="footer">
        &copy; 2019  <a href="https://github.com/kayrx">Kayrx</a>
      </div>
    </div>
    <script async>
      window.onload = function() {
            let menu = document.getElementById('menu');
            menu.addEventListener('click', function() {
                let nav = document.getElementById('rnav');
                if (nav.style.height == 'auto') {
                        nav.style.height = '0';
                }else{
                        nav.style.height = 'auto';
                }
            }, false)
    }
</script>
  </body>
</html>