mirror of
https://github.com/actix/actix-web.git
synced 2024-11-20 08:31:09 +00:00
update examples
This commit is contained in:
parent
44e3df82f6
commit
7a743fa6b5
11 changed files with 28 additions and 33 deletions
|
@ -51,7 +51,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||||
// response
|
// response
|
||||||
Ok(HttpResponse::build(StatusCode::OK)
|
Ok(HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("text/html; charset=utf-8")
|
.content_type("text/html; charset=utf-8")
|
||||||
.body(&html).unwrap())
|
.body(&html))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ fn p404(req: HttpRequest) -> Result<HttpResponse> {
|
||||||
// response
|
// response
|
||||||
Ok(HttpResponse::build(StatusCode::NOT_FOUND)
|
Ok(HttpResponse::build(StatusCode::NOT_FOUND)
|
||||||
.content_type("text/html; charset=utf-8")
|
.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);
|
println!("{:?}", req);
|
||||||
|
|
||||||
result(HttpResponse::Ok()
|
result(Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
|
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))))
|
||||||
.map_err(|e| e.into()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// handler with path parameters like `/user/{name}/`
|
/// handler with path parameters like `/user/{name}/`
|
||||||
fn with_param(req: HttpRequest) -> Result<HttpResponse>
|
fn with_param(req: HttpRequest) -> HttpResponse
|
||||||
{
|
{
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("test/plain")
|
.content_type("test/plain")
|
||||||
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))?)
|
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -44,7 +44,7 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(|res| {
|
.and_then(|res| {
|
||||||
match res {
|
match res {
|
||||||
Ok(user) => Ok(HttpResponse::Ok().json(user)?),
|
Ok(user) => Ok(HttpResponse::Ok().json(user)),
|
||||||
Err(_) => Ok(HttpResponse::InternalServerError().into())
|
Err(_) => Ok(HttpResponse::InternalServerError().into())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -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
|
|resp| resp.body() // <- this is MessageBody type, resolves to complete body
|
||||||
.from_err() // <- convert PayloadError to a Error
|
.from_err() // <- convert PayloadError to a Error
|
||||||
.and_then(|body| { // <- we got complete body, now send as server response
|
.and_then(|body| { // <- we got complete body, now send as server response
|
||||||
HttpResponse::Ok()
|
Ok(HttpResponse::Ok().body(body))
|
||||||
.body(body)
|
|
||||||
.map_err(Error::from)
|
|
||||||
}))
|
}))
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
@ -33,11 +31,10 @@ fn streaming(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
.send() // <- connect to host and send request
|
.send() // <- connect to host and send request
|
||||||
.map_err(Error::from) // <- convert SendRequestError to an Error
|
.map_err(Error::from) // <- convert SendRequestError to an Error
|
||||||
.and_then(|resp| { // <- we received client response
|
.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
|
// read one chunk from client response and send this chunk to a server response
|
||||||
// .from_err() converts PayloadError to a Error
|
// .from_err() converts PayloadError to a Error
|
||||||
.body(Body::Streaming(Box::new(resp.from_err())))
|
.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
|
|
||||||
})
|
})
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,13 +27,13 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
.from_err() // convert all errors into `Error`
|
.from_err() // convert all errors into `Error`
|
||||||
.and_then(|val: MyObj| {
|
.and_then(|val: MyObj| {
|
||||||
println!("model: {:?}", val);
|
println!("model: {:?}", val);
|
||||||
Ok(HttpResponse::Ok().json(val)?) // <- send response
|
Ok(HttpResponse::Ok().json(val)) // <- send response
|
||||||
})
|
})
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This handler uses `With` helper for loading serde json object.
|
/// 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);
|
println!("model: {:?}", &item);
|
||||||
HttpResponse::Ok().json(item.0) // <- send response
|
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| {
|
.and_then(|body| {
|
||||||
// body is loaded, now we can deserialize serde-json
|
// body is loaded, now we can deserialize serde-json
|
||||||
let obj = serde_json::from_slice::<MyObj>(&body)?;
|
let obj = serde_json::from_slice::<MyObj>(&body)?;
|
||||||
Ok(HttpResponse::Ok().json(obj)?) // <- send response
|
Ok(HttpResponse::Ok().json(obj)) // <- send response
|
||||||
})
|
})
|
||||||
.responder()
|
.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() } };
|
let injson: JsonValue = match result { Ok(v) => v, Err(e) => object!{"err" => e.to_string() } };
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("application/json")
|
.content_type("application/json")
|
||||||
.body(injson.dump()).unwrap())
|
.body(injson.dump()))
|
||||||
})
|
})
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ fn graphiql(_req: HttpRequest<State>) -> Result<HttpResponse, Error> {
|
||||||
let html = graphiql_source("http://127.0.0.1:8080/graphql");
|
let html = graphiql_source("http://127.0.0.1:8080/graphql");
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html; charset=utf-8")
|
.content_type("text/html; charset=utf-8")
|
||||||
.body(html).unwrap())
|
.body(html))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphql(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
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()
|
.from_err()
|
||||||
.and_then(|res| {
|
.and_then(|res| {
|
||||||
match res {
|
match res {
|
||||||
Ok(user) => Ok(HttpResponse::Ok().body(user)?),
|
Ok(user) => Ok(HttpResponse::Ok().body(user)),
|
||||||
Err(_) => Ok(HttpResponse::InternalServerError().into())
|
Err(_) => Ok(HttpResponse::InternalServerError().into())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -163,6 +163,6 @@ impl ProtoBufResponseBuilder for HttpResponseBuilder {
|
||||||
|
|
||||||
let mut body = Vec::new();
|
let mut body = Vec::new();
|
||||||
value.encode(&mut body).map_err(|e| ProtoBufPayloadError::Serialize(e))?;
|
value.encode(&mut body).map_err(|e| ProtoBufPayloadError::Serialize(e))?;
|
||||||
Ok(self.body(body)?)
|
Ok(self.body(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(|res| {
|
.and_then(|res| {
|
||||||
match res {
|
match res {
|
||||||
Ok(user) => Ok(HttpResponse::Ok().json(user)?),
|
Ok(user) => Ok(HttpResponse::Ok().json(user)),
|
||||||
Err(_) => Ok(HttpResponse::InternalServerError().into())
|
Err(_) => Ok(HttpResponse::InternalServerError().into())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -11,8 +11,7 @@ use std::cell::Cell;
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http, server, ws, middleware,
|
http, server, ws, middleware, Application, HttpRequest, HttpResponse};
|
||||||
Application, HttpRequest, HttpResponse, Error};
|
|
||||||
|
|
||||||
/// Application state
|
/// Application state
|
||||||
struct AppState {
|
struct AppState {
|
||||||
|
@ -20,7 +19,7 @@ struct AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// simple handle
|
/// simple handle
|
||||||
fn index(req: HttpRequest<AppState>) -> Result<HttpResponse, Error> {
|
fn index(req: HttpRequest<AppState>) -> HttpResponse {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
req.state().counter.set(req.state().counter.get() + 1);
|
req.state().counter.set(req.state().counter.get() + 1);
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ fn index(req: HttpRequest<State>) -> Result<HttpResponse, Error> {
|
||||||
};
|
};
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(s)?)
|
.body(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -15,7 +15,7 @@ fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body("Welcome!")?)
|
.body("Welcome!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use actix_web::{Error, HttpMessage, HttpResponse, HttpRequest};
|
use actix_web::{AsyncResponder, Error, HttpMessage, HttpResponse, HttpRequest};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue