1
0
mirror of https://github.com/actix/actix-website synced 2025-01-22 16:15:56 +01:00

Update to actix-web 2.0 (#131)

This commit is contained in:
Dominic 2019-12-29 14:57:11 +01:00 committed by Yuki Okushi
parent 4b216230d5
commit b04e76e678
6 changed files with 39 additions and 34 deletions

View File

@ -17,5 +17,6 @@ baseURL = "https://actix.rs"
[params] [params]
actixVersion = "0.9" actixVersion = "0.9"
actixWebVersion = "2.0" actixWebVersion = "2.0"
actixRtVersion = "1.0"
actixWebMinRustVersion = "1.39" actixWebMinRustVersion = "1.39"
actixMinRustVersion = "1.39" actixMinRustVersion = "1.39"

View File

@ -1,3 +1,3 @@
--- ---
title: Actix - Actor System and Web Framework for Rust title: rust's powerful actor system and most fun web framework
--- ---

View File

@ -23,25 +23,26 @@ contains the following:
```ini ```ini
[dependencies] [dependencies]
actix-web = "{{< actix-version "actix-web" >}}" actix-web = "{{< actix-version "actix-web" >}}"
actix-rt = "{{< actix-version "actix-rt" >}}"
``` ```
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.
A request handler is a function that accepts zero or more parameters that can be A request handler is an async function that accepts zero or more parameters that can be
extracted from a request (ie, `impl FromRequest`) and returns a type that can be extracted from a request (ie, `impl FromRequest`) and returns a type that can be
converted into an `HttpResponse` (ie, `impl Responder`): converted into an `HttpResponse` (ie, `impl Responder`):
{{< include-example example="getting-started" section="setup" >}} {{< include-example example="getting-started" section="setup" >}}
Next, create an `App` instance and register the request handler with the application's Next, create an `App` instance and register the request handler with the application's
`route` on a *path* and with a particular *HTTP method*. After that, the application `route` on a _path_ and with a particular _HTTP method_. After that, the application
instance can be used with `HttpServer` to listen for incoming connections. The server instance can be used with `HttpServer` to listen for incoming connections. The server
accepts a function that should return an application factory. accepts a function that should return an application factory.
{{< include-example example="getting-started" section="main" >}} {{< include-example example="getting-started" section="main" >}}
That's it! Now, compile and run the program with `cargo run`. That's it! Now, compile and run the program with `cargo run`.
Head over to ``http://localhost:8088/`` to see the results. Head over to `http://localhost:8088/` to see the results.
### Using Attribute Macros to Define Routes ### Using Attribute Macros to Define Routes

View File

@ -6,4 +6,4 @@ workspace = "../"
[dependencies] [dependencies]
actix-web = "2.0" actix-web = "2.0"
actix-rt = "1.0" actix-rt = "1.0.0"

View File

@ -4,7 +4,8 @@
<div class="jumbotron"> <div class="jumbotron">
<div class="actix-jumbotron"> <div class="actix-jumbotron">
<img src="/img/logo-large.png" class="align-middle actix-logo" alt=""> <img src="/img/logo-large.png" class="align-middle actix-logo" alt="">
<p class="lead">rust's powerful actor system and most fun web framework</p> <p class="lead">rust's powerful actor system and most fun web framework
</p>
</div> </div>
</div> </div>
@ -30,32 +31,33 @@
</h2> </h2>
<p>Easily create your own libraries that any Actix application can use.</p> <p>Easily create your own libraries that any Actix application can use.</p>
<h2> <h2>
<i class="fa fa-fw fa-dashboard" aria-hidden="true"></i> <i class="fa fa-fw fa-dashboard" aria-hidden="true"></i>
Blazingly Fast Blazingly Fast
</h2> </h2>
<p>Actix is blazingly fast. Don't take our word for it -- <a href="https://www.techempower.com/benchmarks/#section=data-r18">see for yourself!</a></p> <p>Actix is blazingly fast. Don't take our word for it -- <a
href="https://www.techempower.com/benchmarks/#section=data-r18">see for yourself!</a></p>
</div>
</div> </div>
</div> <div class="col-md-8">
<div class="col-md-8"> <div class="actix-content">
<div class="actix-content"> {{ highlight `use actix_web::{web, App, HttpRequest, HttpServer, Responder};
{{ highlight `use actix_web::{web, App, HttpRequest, HttpServer, Responder};
fn greet(req: HttpRequest) -> impl Responder { async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World"); let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name) format!("Hello {}!", &name)
} }
fn main() { #[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.route("/", web::get().to(greet)) .route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet)) .route("/{name}", web::get().to(greet))
}) })
.bind("127.0.0.1:8000") .bind("127.0.0.1:8000")?
.expect("Can not bind to port 8000")
.run() .run()
.unwrap(); .await
}` "rust" "" }} }` "rust" "" }}
</div> </div>
</div> </div>
@ -66,7 +68,7 @@ fn main() {
<h2>Flexible Responders</h2> <h2>Flexible Responders</h2>
<p> <p>
Handler functions in actix can return a wide range of objects that Handler functions in actix can return a wide range of objects that
implement the <code>Responder</code> trait. This makes it a breeze implement the <code>Responder</code> trait. This makes it a breeze
to return consistent responses from your APIs. to return consistent responses from your APIs.
</p> </p>
{{ highlight `#[derive(Serialize)] {{ highlight `#[derive(Serialize)]
@ -74,11 +76,11 @@ struct Measurement {
temperature: f32, temperature: f32,
} }
fn hello_world() -> impl Responder { async fn hello_world() -> impl Responder {
"Hello World!" "Hello World!"
} }
fn current_temperature() -> impl Responder { async fn current_temperature() -> impl Responder {
web::Json(Measurement { temperature: 42.3 }) web::Json(Measurement { temperature: 42.3 })
}` "rust" "" }} }` "rust" "" }}
</div> </div>
@ -99,7 +101,7 @@ struct Event {
tags: Vec<String>, tags: Vec<String>,
} }
fn capture_event(evt: web::Json<Event>) -> impl Responder { async fn capture_event(evt: web::Json<Event>) -> impl Responder {
let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags); let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);
format!("got event {}", new_event.id.unwrap()) format!("got event {}", new_event.id.unwrap())
}` "rust" "" }} }` "rust" "" }}
@ -107,7 +109,7 @@ fn capture_event(evt: web::Json<Event>) -> impl Responder {
<div class="actix-feature" id="forms"> <div class="actix-feature" id="forms">
<h2>Easy Form Handling</h2> <h2>Easy Form Handling</h2>
<p> <p>
Handling multipart/urlencoded form data is easy. Just define Handling multipart/urlencoded form data is easy. Just define
a structure that can be deserialized and actix will handle a structure that can be deserialized and actix will handle
the rest. the rest.
</p> </p>
@ -117,7 +119,7 @@ struct Register {
country: String, country: String,
} }
fn register(form: web::Form<Register>) -> impl Responder { async fn register(form: web::Form<Register>) -> impl Responder {
format!("Hello {} from {}!", form.username, form.country) format!("Hello {} from {}!", form.username, form.country)
}` "rust" "" }} }` "rust" "" }}
</div> </div>
@ -125,22 +127,21 @@ fn register(form: web::Form<Register>) -> impl Responder {
<h2>Request Routing</h2> <h2>Request Routing</h2>
<p> <p>
An actix app comes with a URL routing system that lets you match on An actix app comes with a URL routing system that lets you match on
URLs and invoke individual handlers. For extra flexibility, scopes URLs and invoke individual handlers. For extra flexibility, scopes
can be used. can be used.
</p> </p>
{{ highlight `fn index(_req: HttpRequest) -> impl Responder { {{ highlight `async fn index(_req: HttpRequest) -> impl Responder {
"Hello from the index page!" "Hello from the index page!"
} }
fn hello(path: web::Path<String>) -> impl Responder { async fn hello(path: web::Path<String>) -> impl Responder {
format!("Hello {}!", &path) format!("Hello {}!", &path)
} }
fn main() { let app = App::new()
App::new()
.route("/", web::get().to(index)) .route("/", web::get().to(index))
.route("/{name}", web::get().to(hello)); .route("/{name}", web::get().to(hello));
}` "rust" "" }} ` "rust" "" }}
</div> </div>
</div> </div>
<div class="col-md-3 actix-feature-selectors"> <div class="col-md-3 actix-feature-selectors">
@ -155,4 +156,4 @@ fn main() {
</div> </div>
</div> </div>
{{ partial "footer" . }} {{ partial "footer" . }}

View File

@ -2,4 +2,6 @@
{{- .Page.Site.Params.actixVersion -}} {{- .Page.Site.Params.actixVersion -}}
{{- else if eq (.Get 0) "actix-web" -}} {{- else if eq (.Get 0) "actix-web" -}}
{{- .Page.Site.Params.actixWebVersion -}} {{- .Page.Site.Params.actixWebVersion -}}
{{- end -}} {{- else if eq (.Get 0) "actix-rt" -}}
{{- .Page.Site.Params.actixRtVersion -}}
{{- end -}}