1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 19:14:36 +01:00

Front page examples return 'impl Responder'

This commit is contained in:
Cameron Dershem 2019-06-16 23:37:14 -04:00
parent f922e8fb96
commit 3f95205696
5 changed files with 24 additions and 30 deletions

View File

@ -1,5 +1,5 @@
// <easy-form-handling> // <easy-form-handling>
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -14,11 +14,8 @@ fn index() -> HttpResponse {
.body(include_str!("../static/form.html")) .body(include_str!("../static/form.html"))
} }
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> { fn register(params: web::Form<Register>) -> impl Responder {
Ok(HttpResponse::Ok().body(format!( format!("Hello {} from {}!", params.username, params.country)
"Hello {} from {}!",
params.username, params.country
)))
} }
fn main() { fn main() {

View File

@ -1,9 +1,9 @@
// <main-example> // <main-example>
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; use actix_web::{web, App, HttpRequest, HttpServer, Responder};
fn greet(req: HttpRequest) -> HttpResponse { 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");
HttpResponse::Ok().body(format!("Hello {}!", &name)) format!("Hello {}!", &name)
} }
fn main() { fn main() {

View File

@ -1,4 +1,4 @@
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
@ -20,9 +20,9 @@ fn store_event_in_db(timestamp: f64, kind: String, tags: Vec<String>) -> Event {
} }
} }
fn capture_event(evt: web::Json<Event>) -> actix_web::Result<HttpResponse> { fn capture_event(evt: web::Json<Event>) -> impl Responder {
let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone()); let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone());
Ok(HttpResponse::Ok().json(new_event)) format!("got event {}", new_event.id.unwrap())
} }
fn index() -> HttpResponse { fn index() -> HttpResponse {

View File

@ -1,12 +1,12 @@
// <request-routing> // <request-routing>
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; use actix_web::{web, App, HttpRequest, HttpServer, Responder};
fn index(_req: HttpRequest) -> HttpResponse { fn index(_req: HttpRequest) -> impl Responder {
HttpResponse::Ok().body("Hello from the index page.") "Hello from the index page."
} }
fn hello(path: web::Path<String>) -> HttpResponse { fn hello(path: web::Path<String>) -> impl Responder {
HttpResponse::Ok().body(format!("Hello {}!", &path)) format!("Hello {}!", &path)
} }
fn main() { fn main() {

View File

@ -41,9 +41,9 @@
<div class="actix-content"> <div class="actix-content">
{{ highlight `use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; {{ highlight `use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
fn greet(req: HttpRequest) -> HttpResponse { 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");
HttpResponse::Ok().body(format!("Hello {}!", &name)) format!("Hello {}!", &name)
} }
fn main() { fn main() {
@ -98,11 +98,11 @@ struct Event {
kind: String, kind: String,
tags: Vec<String>, tags: Vec<String>,
} }
fn capture_event(evt: web::Json<Event>) -> actix_web::Result<HttpResponse> { fn capture_event(evt: web::Json<Event>) -> impl Responder {
let new_event = store_event_in_db(evt.timestamp, let new_event = store_event_in_db(evt.timestamp,
evt.kind.clone(), evt.kind.clone(),
evt.tags.clone()); evt.tags.clone());
Ok(HttpResponse::Ok().json(new_event)) format!("got event {}", new_event.id.unwrap())
}` "rust" "" }} }` "rust" "" }}
</div> </div>
<div class="actix-feature" id="forms"> <div class="actix-feature" id="forms">
@ -118,11 +118,8 @@ struct Register {
country: String, country: String,
} }
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> { fn register(params: web::Form<Register>) -> impl Responder {
Ok(HttpResponse::Ok().body(format!( format!("Hello {} from {}!", params.username, params.country)
"Hello {} from {}!",
params.username, params.country
)))
}` "rust" "" }} }` "rust" "" }}
</div> </div>
<div class="actix-feature" id="routing"> <div class="actix-feature" id="routing">
@ -132,12 +129,12 @@ fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {
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) -> HttpResponse { {{ highlight `fn index(_req: HttpRequest) -> impl Responder {
HttpResponse::Ok().body("Hello from the index page!") "Hello from the index page!"
} }
fn hello(path: web::Path<String>) -> HttpResponse { fn hello(path: web::Path<String>) -> impl Responder {
HttpResponse::Ok().body(format!("Hello {}!", &path)) format!("Hello {}!", &path)
} }
fn main() { fn main() {