This commit is contained in:
Alexander Andreev 2018-01-22 19:07:04 +00:00 committed by GitHub
commit 3b3bf5e9a0
3 changed files with 54 additions and 53 deletions

View File

@ -30,33 +30,33 @@ A request handler is a function that accepts a `HttpRequest` instance as its onl
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();
# });
});
}
```

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,
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;

View File

@ -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