diff --git a/guide/src/qs_2.md b/guide/src/qs_2.md index 80852895e..7cf9212e1 100644 --- a/guide/src/qs_2.md +++ b/guide/src/qs_2.md @@ -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. -## Hello, world! +## Hello, world! Let’s write our first actix web application! Start by creating a new binary-based Cargo project and changing into the new directory: @@ -15,7 +15,7 @@ cargo new hello-world --bin 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: ```toml @@ -26,37 +26,37 @@ actix-web = "0.3" 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`: ```rust -# extern crate actix_web; -# use actix_web::*; + extern crate actix_web; + use actix_web::*; fn index(req: HttpRequest) -> &'static str { "Hello world!" } -# fn main() {} + fn main() {} ``` Next, create an `Application` instance and register the request handler with the application's `resource` on a particular *HTTP method* and *path*:: ```rust -# extern crate actix_web; -# use actix_web::*; -# fn index(req: HttpRequest) -> &'static str { -# "Hello world!" -# } -# fn main() { + extern crate actix_web; + use actix_web::*; + fn index(req: HttpRequest) -> &'static str { + "Hello world!" + } + fn main() { Application::new() .resource("/", |r| r.f(index)); -# } + } ``` After that, application instance can be used with `HttpServer` to listen for incoming connections. Server accepts function that should return `HttpHandler` instance: -```rust,ignore +```rust HttpServer::new( || Application::new() .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: ```rust -# use std::thread; -# extern crate actix_web; +use std::thread; +extern crate actix_web; use actix_web::*; fn index(req: HttpRequest) -> &'static str { @@ -79,13 +79,13 @@ fn index(req: HttpRequest) -> &'static str { } fn main() { -# thread::spawn(|| { + thread::spawn(|| { HttpServer::new( || Application::new() .resource("/", |r| r.f(index))) .bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088") .run(); -# }); + }); } ``` diff --git a/guide/src/qs_3.md b/guide/src/qs_3.md index 7a90c3b5d..487bd6c2b 100644 --- a/guide/src/qs_3.md +++ b/guide/src/qs_3.md @@ -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, but path `/application` would not match. -```rust,ignore -# extern crate actix_web; -# extern crate tokio_core; -# use actix_web::*; -# fn index(req: HttpRequest) -> &'static str { -# "Hello world!" -# } -# fn main() { +```rust +extern crate actix_web; +extern crate tokio_core; +use actix_web::*; +fn index(req: HttpRequest) -> &'static str { + "Hello world!" +} +fn main() { let app = Application::new() .prefix("/app") .resource("/index.html", |r| r.method(Method::GET).f(index)) .finish() -# } +} ``` 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: ```rust -# extern crate actix_web; -# extern crate tokio_core; -# use tokio_core::net::TcpStream; -# use std::net::SocketAddr; +extern crate actix_web; +extern crate tokio_core; +use tokio_core::net::TcpStream; +use std::net::SocketAddr; use actix_web::*; fn main() { @@ -77,9 +77,9 @@ Let's write simple application that uses shared state. We are going to store req in the state: ```rust -# extern crate actix; -# extern crate actix_web; -# +extern crate actix; +extern crate actix_web; + use actix_web::*; use std::cell::Cell; diff --git a/guide/src/qs_3_5.md b/guide/src/qs_3_5.md index 7982cd272..4139429fe 100644 --- a/guide/src/qs_3_5.md +++ b/guide/src/qs_3_5.md @@ -10,8 +10,8 @@ starts ssl server. *HttpServer* is an actix actor, it has to be initialized within properly configured actix system: ```rust -# extern crate actix; -# extern crate actix_web; +extern crate actix; +extern crate actix_web; use actix::*; use actix_web::*; @@ -24,7 +24,7 @@ fn main() { .bind("127.0.0.1:59080").unwrap() .start(); -# actix::Arbiter::system().send(actix::msgs::SystemExit(0)); + actix::Arbiter::system().send(actix::msgs::SystemExit(0)); 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 ```rust -# extern crate futures; -# extern crate actix; -# extern crate actix_web; -# use futures::Future; +extern crate futures; +extern crate actix; +extern crate actix_web; +use futures::Future; use actix_web::*; use std::thread; 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. ```rust -# extern crate actix_web; -# extern crate tokio_core; -# use tokio_core::net::TcpStream; -# use std::net::SocketAddr; +extern crate actix_web; +extern crate tokio_core; +use tokio_core::net::TcpStream; +use std::net::SocketAddr; use actix_web::*; fn main() { @@ -106,7 +106,7 @@ integration and `alpn` is for `openssl`. actix-web = { git = "https://github.com/actix/actix-web", features=["alpn"] } ``` -```rust,ignore +```rust use std::fs::File; use actix_web::*; @@ -141,10 +141,10 @@ connection behavior is defined by server settings. * `None` - Use `SO_KEEPALIVE` socket option. ```rust -# extern crate actix_web; -# extern crate tokio_core; -# use tokio_core::net::TcpStream; -# use std::net::SocketAddr; +extern crate actix_web; +extern crate tokio_core; +use tokio_core::net::TcpStream; +use std::net::SocketAddr; use actix_web::*; 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. ```rust -# extern crate actix_web; -# use actix_web::httpcodes::*; +extern crate actix_web; +use actix_web::httpcodes::*; use actix_web::*; fn index(req: HttpRequest) -> HttpResponse { @@ -174,7 +174,8 @@ fn index(req: HttpRequest) -> HttpResponse { .force_close() // <- Alternative method .finish().unwrap() } -# fn main() {} + +fn main() {} ``` ## Graceful shutdown