1
0
mirror of https://github.com/actix/actix-website synced 2025-02-20 03:14:22 +01:00

begin update of docs to actix-web 1.0

This commit is contained in:
Cameron Dershem 2019-06-12 04:51:45 -04:00
parent ad45f9e95a
commit 8aa39e1afb
9 changed files with 43 additions and 24 deletions

View File

@ -16,4 +16,4 @@ baseURL = "https://actix.rs"
[params] [params]
actixVersion = "0.7" actixVersion = "0.7"
actixWebVersion = "0.7" actixWebVersion = "1.0"

View File

@ -27,6 +27,7 @@ actix-web = "{{< actix-version "actix-web" >}}"
In order to implement a web server, we first need to create a request handler. In order to implement a web server, we first need to create a request handler.
<!-- TODO: Handler has been deprecated -->
A request handler is a function that accepts an `HttpRequest` instance as its only parameter A request handler is a function that accepts an `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`:
@ -34,12 +35,12 @@ Filename: `src/main.rs`
{{< include-example example="getting-started" section="setup" >}} {{< include-example example="getting-started" section="setup" >}}
<!-- TODO: Return ->
Next, create an `Application` instance and register the request handler with Next, create an `Application` instance and register the request handler with
the application's `resource` on a particular *HTTP method* and *path* and the application's `resource` on a particular *HTTP method* and *path* and
after that, the application instance can be used with `HttpServer` to listen after that, the application instance can be used with `HttpServer` to listen
for incoming connections. The server accepts a function that should return an for incoming connections. The server accepts a function that should return an
`HttpHandler` instance. For simplicity `server::new` could be used, this `HttpResponse`.
function is shortcut for `HttpServer::new`:
{{< include-example example="getting-started" section="main" >}} {{< include-example example="getting-started" section="main" >}}

View File

@ -9,7 +9,7 @@ weight: 110
Since `actix-web` is a Rust framework you will need Rust to get started with it. Since `actix-web` is a Rust framework you will need Rust to get started with it.
If you don't have it yet we recommend you use `rustup` to manage your Rust If you don't have it yet we recommend you use `rustup` to manage your Rust
installation. The [official rust installation. The [official rust
guide](https://doc.rust-lang.org/book/second-edition/ch01-01-installation.html) guide](https://doc.rust-lang.org/book/ch01-01-installation.html)
has a wonderful section on getting started. has a wonderful section on getting started.
We currently require at least Rust 1.24 so make sure you run `rustup update` We currently require at least Rust 1.24 so make sure you run `rustup update`
@ -18,7 +18,7 @@ guide will assume that you actually run Rust 1.26 or later.
# Installing `actix-web` # Installing `actix-web`
Thank's to Rust's `cargo` package manager you won't need to explicitly install Thanks to Rust's `cargo` package manager you won't need to explicitly install
`actix-web`. Just depend on it and you're ready to go. For the unlikely `actix-web`. Just depend on it and you're ready to go. For the unlikely
case that you want to use the development version of actix-web you can case that you want to use the development version of actix-web you can
depend on the git repository directly. depend on the git repository directly.

View File

@ -4,4 +4,4 @@ version = "0.7.0"
workspace = "../" workspace = "../"
[dependencies] [dependencies]
actix-web = "0.7" actix-web = "1.0"

View File

@ -4,4 +4,4 @@ version = "0.7.0"
workspace = "../" workspace = "../"
[dependencies] [dependencies]
actix-web = "0.7" actix-web = "1.0"

View File

@ -1,16 +1,34 @@
// // <setup>
// extern crate actix_web;
// use actix_web::{server, App, HttpRequest};
// fn index(_req: &HttpRequest) -> &'static str {
// "Hello world!"
// }
// // </setup>
// // <main>
// fn main() {
// server::new(|| App::new().resource("/", |r| r.f(index)))
// .bind("127.0.0.1:8088")
// .unwrap()
// .run();
// }
// // </main>
// <setup> // <setup>
extern crate actix_web; extern crate actix_web;
use actix_web::{server, App, HttpRequest}; use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
fn index(_req: &HttpRequest) -> &'static str { fn index(_req: HttpRequest) -> HttpResponse {
"Hello world!" HttpResponse::Ok().body("Hello world!")
} }
// </setup> // </setup>
// <main> // <main>
fn main() { fn main() {
server::new(|| App::new().resource("/", |r| r.f(index))) HttpServer::new(|| App::new().service(web::resource("/").to(index)))
.bind("127.0.0.1:8088") .bind("127.0.0.1:8000")
.unwrap() .unwrap()
.run(); .run()
.unwrap();
} }
// </main> // </main>

View File

@ -5,6 +5,6 @@ workspace = "../"
[dependencies] [dependencies]
actix = "0.7" actix = "0.7"
actix-web = { version="0.7", features=["alpn"] } actix-web = "1.0"
futures = "0.1" futures = "0.1"
openssl = "0.10" openssl = "0.10"

View File

@ -5,7 +5,7 @@ workspace = "../"
[dependencies] [dependencies]
actix = "0.7" actix = "0.7"
actix-web = "0.7" actix-web = "1.0"
futures = "0.1" futures = "0.1"
openssl = "0.10" openssl = "0.10"
serde = "1.0" serde = "1.0"

View File

@ -39,23 +39,23 @@
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<div class="actix-content"> <div class="actix-content">
{{ highlight `extern crate actix_web; {{ highlight `use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use actix_web::{server, App, HttpRequest, Responder};
fn greet(req: &HttpRequest) -> impl Responder { fn greet(req: HttpRequest) -> HttpResponse {
let to = req.match_info().get("name").unwrap_or("World"); let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", to) HttpResponse::Ok().body(format!("Hello {}!", &name))
} }
fn main() { fn main() {
server::new(|| { HttpServer::new(|| {
App::new() App::new()
.resource("/", |r| r.f(greet)) .service(web::resource("/").to(greet))
.resource("/{name}", |r| r.f(greet)) .service(web::resource("/{name}").to(greet))
}) })
.bind("127.0.0.1:8000") .bind("127.0.0.1:8000")
.expect("Can not bind to port 8000") .expect("Can not bind to port 8000")
.run(); .run()
.unwrap();
}` "rust" "" }} }` "rust" "" }}
</div> </div>
</div> </div>