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

v3 examples (#364)

This commit is contained in:
Rob Ede
2020-09-12 16:49:45 +01:00
committed by GitHub
parent 3f2b2708c3
commit 49e29a5751
112 changed files with 251 additions and 333 deletions

View File

@ -2,18 +2,16 @@
name = "json-example"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
workspace = ".."
edition = "2018"
[dependencies]
actix-web = "2.0.0"
actix-rt = "1.0.0"
actix-web = "3"
actix-service = "1.0.0"
bytes = "0.5.2"
futures = "0.3.1"
env_logger = "0.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
json = "0.12"
[dev-dependencies]
actix-rt = "1"

View File

@ -1,7 +1,6 @@
use actix_web::{
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use bytes::{Bytes, BytesMut};
use futures::StreamExt;
use json::JsonValue;
use serde::{Deserialize, Serialize};
@ -31,7 +30,7 @@ const MAX_SIZE: usize = 262_144; // max payload size is 256k
/// This handler manually load request payload and parse json object
async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error> {
// payload is a stream of Bytes objects
let mut body = BytesMut::new();
let mut body = web::BytesMut::new();
while let Some(chunk) = payload.next().await {
let chunk = chunk?;
// limit max size of in-memory payload
@ -47,7 +46,7 @@ async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error>
}
/// This handler manually load request payload and parse json-rust
async fn index_mjsonrust(body: Bytes) -> Result<HttpResponse, Error> {
async fn index_mjsonrust(body: web::Bytes) -> Result<HttpResponse, Error> {
// body is loaded, now we can deserialize json-rust
let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result
let injson: JsonValue = match result {
@ -59,7 +58,7 @@ async fn index_mjsonrust(body: Bytes) -> Result<HttpResponse, Error> {
.body(injson.dump()))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();