1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

update examples

This commit is contained in:
Nikolay Kim 2018-03-30 23:37:15 -07:00
parent 44e3df82f6
commit 7a743fa6b5
11 changed files with 28 additions and 33 deletions

View File

@ -51,7 +51,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse> {
// response
Ok(HttpResponse::build(StatusCode::OK)
.content_type("text/html; charset=utf-8")
.body(&html).unwrap())
.body(&html))
}
@ -69,7 +69,7 @@ fn p404(req: HttpRequest) -> Result<HttpResponse> {
// response
Ok(HttpResponse::build(StatusCode::NOT_FOUND)
.content_type("text/html; charset=utf-8")
.body(html).unwrap())
.body(html))
}
@ -78,20 +78,19 @@ fn index_async(req: HttpRequest) -> FutureResult<HttpResponse, Error>
{
println!("{:?}", req);
result(HttpResponse::Ok()
result(Ok(HttpResponse::Ok()
.content_type("text/html")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
.map_err(|e| e.into()))
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))))
}
/// handler with path parameters like `/user/{name}/`
fn with_param(req: HttpRequest) -> Result<HttpResponse>
fn with_param(req: HttpRequest) -> HttpResponse
{
println!("{:?}", req);
Ok(HttpResponse::Ok()
HttpResponse::Ok()
.content_type("test/plain")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))?)
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
}
fn main() {

View File

@ -44,7 +44,7 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
.from_err()
.and_then(|res| {
match res {
Ok(user) => Ok(HttpResponse::Ok().json(user)?),
Ok(user) => Ok(HttpResponse::Ok().json(user)),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
})

View File

@ -18,9 +18,7 @@ fn index(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|resp| resp.body() // <- this is MessageBody type, resolves to complete body
.from_err() // <- convert PayloadError to a Error
.and_then(|body| { // <- we got complete body, now send as server response
HttpResponse::Ok()
.body(body)
.map_err(Error::from)
Ok(HttpResponse::Ok().body(body))
}))
.responder()
}
@ -33,11 +31,10 @@ fn streaming(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
.send() // <- connect to host and send request
.map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|resp| { // <- we received client response
HttpResponse::Ok()
Ok(HttpResponse::Ok()
// read one chunk from client response and send this chunk to a server response
// .from_err() converts PayloadError to a Error
.body(Body::Streaming(Box::new(resp.from_err())))
.map_err(|e| e.into()) // HttpOk::build() maybe return HttpError, we need to convert it to a Error
.body(Body::Streaming(Box::new(resp.from_err()))))
})
.responder()
}

View File

@ -27,13 +27,13 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
.from_err() // convert all errors into `Error`
.and_then(|val: MyObj| {
println!("model: {:?}", val);
Ok(HttpResponse::Ok().json(val)?) // <- send response
Ok(HttpResponse::Ok().json(val)) // <- send response
})
.responder()
}
/// This handler uses `With` helper for loading serde json object.
fn extract_item(item: Json<MyObj>) -> Result<HttpResponse, Error> {
fn extract_item(item: Json<MyObj>) -> HttpResponse {
println!("model: {:?}", &item);
HttpResponse::Ok().json(item.0) // <- send response
}
@ -64,7 +64,7 @@ fn index_manual(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
.and_then(|body| {
// body is loaded, now we can deserialize serde-json
let obj = serde_json::from_slice::<MyObj>(&body)?;
Ok(HttpResponse::Ok().json(obj)?) // <- send response
Ok(HttpResponse::Ok().json(obj)) // <- send response
})
.responder()
}
@ -79,7 +79,7 @@ fn index_mjsonrust(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Erro
let injson: JsonValue = match result { Ok(v) => v, Err(e) => object!{"err" => e.to_string() } };
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(injson.dump()).unwrap())
.body(injson.dump()))
})
.responder()
}

View File

@ -67,7 +67,7 @@ fn graphiql(_req: HttpRequest<State>) -> Result<HttpResponse, Error> {
let html = graphiql_source("http://127.0.0.1:8080/graphql");
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html).unwrap())
.body(html))
}
fn graphql(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
@ -79,7 +79,7 @@ fn graphql(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
.from_err()
.and_then(|res| {
match res {
Ok(user) => Ok(HttpResponse::Ok().body(user)?),
Ok(user) => Ok(HttpResponse::Ok().body(user)),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
})

View File

@ -163,6 +163,6 @@ impl ProtoBufResponseBuilder for HttpResponseBuilder {
let mut body = Vec::new();
value.encode(&mut body).map_err(|e| ProtoBufPayloadError::Serialize(e))?;
Ok(self.body(body)?)
Ok(self.body(body))
}
}

View File

@ -34,7 +34,7 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
.from_err()
.and_then(|res| {
match res {
Ok(user) => Ok(HttpResponse::Ok().json(user)?),
Ok(user) => Ok(HttpResponse::Ok().json(user)),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
})

View File

@ -11,8 +11,7 @@ use std::cell::Cell;
use actix::prelude::*;
use actix_web::{
http, server, ws, middleware,
Application, HttpRequest, HttpResponse, Error};
http, server, ws, middleware, Application, HttpRequest, HttpResponse};
/// Application state
struct AppState {
@ -20,7 +19,7 @@ struct AppState {
}
/// simple handle
fn index(req: HttpRequest<AppState>) -> Result<HttpResponse, Error> {
fn index(req: HttpRequest<AppState>) -> HttpResponse {
println!("{:?}", req);
req.state().counter.set(req.state().counter.get() + 1);

View File

@ -27,7 +27,7 @@ fn index(req: HttpRequest<State>) -> Result<HttpResponse, Error> {
};
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(s)?)
.body(s))
}
fn main() {

View File

@ -15,7 +15,7 @@ fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
println!("{:?}", req);
Ok(HttpResponse::Ok()
.content_type("text/plain")
.body("Welcome!")?)
.body("Welcome!"))
}
fn main() {

View File

@ -1,4 +1,4 @@
use actix_web::{Error, HttpMessage, HttpResponse, HttpRequest};
use actix_web::{AsyncResponder, Error, HttpMessage, HttpResponse, HttpRequest};
use futures::Future;