1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

Update to master (#90)

This commit is contained in:
Juan Aguilar
2019-03-26 04:29:00 +01:00
committed by Douman
parent 1779f963d9
commit 53fc2221ef
44 changed files with 139 additions and 143 deletions

View File

@ -6,7 +6,7 @@ workspace = ".."
edition = "2018"
[dependencies]
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
actix-web = { git="https://github.com/actix/actix-web.git" }
bytes = "0.4"
futures = "0.1"

View File

@ -4,7 +4,7 @@ extern crate json;
use actix_web::{
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use bytes::{Bytes, BytesMut};
use bytes::BytesMut;
use futures::{Future, Stream};
use json::JsonValue;
use serde_derive::{Deserialize, Serialize};
@ -32,12 +32,9 @@ fn extract_item(item: web::Json<MyObj>, req: HttpRequest) -> HttpResponse {
const MAX_SIZE: usize = 262_144; // max payload size is 256k
/// This handler manually load request payload and parse json object
fn index_manual<P>(
payload: web::Payload<P>,
) -> impl Future<Item = HttpResponse, Error = Error>
where
P: Stream<Item = Bytes, Error = error::PayloadError>,
{
fn index_manual(
payload: web::Payload,
) -> impl Future<Item = HttpResponse, Error = Error> {
// payload is a stream of Bytes objects
payload
// `Future::from_err` acts like `?` in that it coerces the error type from
@ -64,12 +61,7 @@ where
}
/// This handler manually load request payload and parse json-rust
fn index_mjsonrust<P>(
pl: web::Payload<P>,
) -> impl Future<Item = HttpResponse, Error = Error>
where
P: Stream<Item = Bytes, Error = error::PayloadError>,
{
fn index_mjsonrust(pl: web::Payload) -> impl Future<Item = HttpResponse, Error = Error> {
pl.concat2().from_err().and_then(|body| {
// body is loaded, now we can deserialize json-rust
let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result
@ -90,18 +82,18 @@ fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
// enable logger
.middleware(middleware::Logger::default())
.wrap(middleware::Logger::default())
.service(
web::resource("/extractor").route(
web::post()
.config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
.data(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
.to(index),
),
)
.service(
web::resource("/extractor2").route(
web::post()
.config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
.data(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
.to_async(extract_item),
),
)