fix guide examples code, rust not support '#' comment

This commit is contained in:
Alexander Andreev 2018-01-22 22:05:46 +03:00
parent 1957469061
commit 9b79083b26
3 changed files with 54 additions and 53 deletions

View File

@ -5,7 +5,7 @@ that depends on actix web and then run the application.
In previous section we already installed required rust version. Now let's create new cargo projects. In previous section we already installed required rust version. Now let's create new cargo projects.
## Hello, world! ## Hello, world!
Lets write our first actix web application! Start by creating a new binary-based Lets write our first actix web application! Start by creating a new binary-based
Cargo project and changing into the new directory: Cargo project and changing into the new directory:
@ -15,7 +15,7 @@ cargo new hello-world --bin
cd hello-world cd hello-world
``` ```
Now, add actix and actix web as dependencies of your project by ensuring your Cargo.toml Now, add actix and actix web as dependencies of your project by ensuring your Cargo.toml
contains the following: contains the following:
```toml ```toml
@ -26,37 +26,37 @@ actix-web = "0.3"
In order to implement a web server, first we need to create a request handler. In order to implement a web server, first we need to create a request handler.
A request handler is a function that accepts a `HttpRequest` instance as its only parameter A request handler is a function that accepts a `HttpRequest` instance as its only parameter
and returns a type that can be converted into `HttpResponse`: and returns a type that can be converted into `HttpResponse`:
```rust ```rust
# extern crate actix_web; extern crate actix_web;
# use actix_web::*; use actix_web::*;
fn index(req: HttpRequest) -> &'static str { fn index(req: HttpRequest) -> &'static str {
"Hello world!" "Hello world!"
} }
# fn main() {} fn main() {}
``` ```
Next, create an `Application` instance and register the Next, create an `Application` instance and register the
request handler with the application's `resource` on a particular *HTTP method* and *path*:: request handler with the application's `resource` on a particular *HTTP method* and *path*::
```rust ```rust
# extern crate actix_web; extern crate actix_web;
# use actix_web::*; use actix_web::*;
# fn index(req: HttpRequest) -> &'static str { fn index(req: HttpRequest) -> &'static str {
# "Hello world!" "Hello world!"
# } }
# fn main() { fn main() {
Application::new() Application::new()
.resource("/", |r| r.f(index)); .resource("/", |r| r.f(index));
# } }
``` ```
After that, application instance can be used with `HttpServer` to listen for incoming After that, application instance can be used with `HttpServer` to listen for incoming
connections. Server accepts function that should return `HttpHandler` instance: connections. Server accepts function that should return `HttpHandler` instance:
```rust,ignore ```rust
HttpServer::new( HttpServer::new(
|| Application::new() || Application::new()
.resource("/", |r| r.f(index))) .resource("/", |r| r.f(index)))
@ -70,8 +70,8 @@ Head over to ``http://localhost:8088/`` to see the results.
Here is full source of main.rs file: Here is full source of main.rs file:
```rust ```rust
# use std::thread; use std::thread;
# extern crate actix_web; extern crate actix_web;
use actix_web::*; use actix_web::*;
fn index(req: HttpRequest) -> &'static str { fn index(req: HttpRequest) -> &'static str {
@ -79,13 +79,13 @@ fn index(req: HttpRequest) -> &'static str {
} }
fn main() { fn main() {
# thread::spawn(|| { thread::spawn(|| {
HttpServer::new( HttpServer::new(
|| Application::new() || Application::new()
.resource("/", |r| r.f(index))) .resource("/", |r| r.f(index)))
.bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088") .bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088")
.run(); .run();
# }); });
} }
``` ```

View File

@ -16,19 +16,19 @@ Prefix should consists of value path segments. i.e for application with prefix `
any request with following paths `/app`, `/app/` or `/app/test` would match, any request with following paths `/app`, `/app/` or `/app/test` would match,
but path `/application` would not match. but path `/application` would not match.
```rust,ignore ```rust
# extern crate actix_web; extern crate actix_web;
# extern crate tokio_core; extern crate tokio_core;
# use actix_web::*; use actix_web::*;
# fn index(req: HttpRequest) -> &'static str { fn index(req: HttpRequest) -> &'static str {
# "Hello world!" "Hello world!"
# } }
# fn main() { fn main() {
let app = Application::new() let app = Application::new()
.prefix("/app") .prefix("/app")
.resource("/index.html", |r| r.method(Method::GET).f(index)) .resource("/index.html", |r| r.method(Method::GET).f(index))
.finish() .finish()
# } }
``` ```
In this example application with `/app` prefix and `index.html` resource In this example application with `/app` prefix and `index.html` resource
@ -39,10 +39,10 @@ For more information check
Multiple applications could be served with one server: Multiple applications could be served with one server:
```rust ```rust
# extern crate actix_web; extern crate actix_web;
# extern crate tokio_core; extern crate tokio_core;
# use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
# use std::net::SocketAddr; use std::net::SocketAddr;
use actix_web::*; use actix_web::*;
fn main() { fn main() {
@ -77,9 +77,9 @@ Let's write simple application that uses shared state. We are going to store req
in the state: in the state:
```rust ```rust
# extern crate actix; extern crate actix;
# extern crate actix_web; extern crate actix_web;
#
use actix_web::*; use actix_web::*;
use std::cell::Cell; use std::cell::Cell;

View File

@ -10,8 +10,8 @@ starts ssl server. *HttpServer* is an actix actor, it has to be initialized
within properly configured actix system: within properly configured actix system:
```rust ```rust
# extern crate actix; extern crate actix;
# extern crate actix_web; extern crate actix_web;
use actix::*; use actix::*;
use actix_web::*; use actix_web::*;
@ -24,7 +24,7 @@ fn main() {
.bind("127.0.0.1:59080").unwrap() .bind("127.0.0.1:59080").unwrap()
.start(); .start();
# actix::Arbiter::system().send(actix::msgs::SystemExit(0)); actix::Arbiter::system().send(actix::msgs::SystemExit(0));
let _ = sys.run(); let _ = sys.run();
} }
``` ```
@ -42,10 +42,10 @@ address of the started http server. Actix http server accept several messages:
* `StopServer` - Stop incoming connection processing, stop all workers and exit * `StopServer` - Stop incoming connection processing, stop all workers and exit
```rust ```rust
# extern crate futures; extern crate futures;
# extern crate actix; extern crate actix;
# extern crate actix_web; extern crate actix_web;
# use futures::Future; use futures::Future;
use actix_web::*; use actix_web::*;
use std::thread; use std::thread;
use std::sync::mpsc; use std::sync::mpsc;
@ -78,10 +78,10 @@ this number is equal to number of logical cpu in the system. This number
could be overridden with `HttpServer::threads()` method. could be overridden with `HttpServer::threads()` method.
```rust ```rust
# extern crate actix_web; extern crate actix_web;
# extern crate tokio_core; extern crate tokio_core;
# use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
# use std::net::SocketAddr; use std::net::SocketAddr;
use actix_web::*; use actix_web::*;
fn main() { fn main() {
@ -106,7 +106,7 @@ integration and `alpn` is for `openssl`.
actix-web = { git = "https://github.com/actix/actix-web", features=["alpn"] } actix-web = { git = "https://github.com/actix/actix-web", features=["alpn"] }
``` ```
```rust,ignore ```rust
use std::fs::File; use std::fs::File;
use actix_web::*; use actix_web::*;
@ -141,10 +141,10 @@ connection behavior is defined by server settings.
* `None` - Use `SO_KEEPALIVE` socket option. * `None` - Use `SO_KEEPALIVE` socket option.
```rust ```rust
# extern crate actix_web; extern crate actix_web;
# extern crate tokio_core; extern crate tokio_core;
# use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
# use std::net::SocketAddr; use std::net::SocketAddr;
use actix_web::*; use actix_web::*;
fn main() { fn main() {
@ -164,8 +164,8 @@ and is on for *HTTP/1.1* and "HTTP/2.0".
*Connection type* could be change with `HttpResponseBuilder::connection_type()` method. *Connection type* could be change with `HttpResponseBuilder::connection_type()` method.
```rust ```rust
# extern crate actix_web; extern crate actix_web;
# use actix_web::httpcodes::*; use actix_web::httpcodes::*;
use actix_web::*; use actix_web::*;
fn index(req: HttpRequest) -> HttpResponse { fn index(req: HttpRequest) -> HttpResponse {
@ -174,7 +174,8 @@ fn index(req: HttpRequest) -> HttpResponse {
.force_close() // <- Alternative method .force_close() // <- Alternative method
.finish().unwrap() .finish().unwrap()
} }
# fn main() {}
fn main() {}
``` ```
## Graceful shutdown ## Graceful shutdown