1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00
This commit is contained in:
Rob Ede
2020-09-12 16:21:54 +01:00
committed by GitHub
parent a0ce9f28e2
commit 4d8d53cea5
145 changed files with 1011 additions and 1461 deletions

View File

@ -6,8 +6,6 @@ edition = "2018"
[dependencies]
serde = "1.0"
serde_json = "1.0"
actix-web = "2.0"
actix-rt = "1.0"
actix-web = "3"
futures = "0.3.1"
bytes = "0.4"
actix-multipart = "0.2"
actix-multipart = "0.3"

View File

@ -1,21 +0,0 @@
// // <json-two>
// use actix_web::{error::Error, HttpRequest, HttpResponse};
// use futures::Future;
// use serde::{Deserialize, Serialize};
// #[derive(Debug, Serialize, Deserialize)]
// struct MyObj {
// name: String,
// number: i32,
// }
// pub fn index(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
// req.json()
// .from_err()
// .and_then(|val: MyObj| {
// println!("model: {:?}", val);
// Ok(HttpResponse::Ok().json(val)) // <- send response
// })
// .responder()
// }
// // </json-two>

View File

@ -1,4 +1,3 @@
pub mod json_two;
pub mod manual;
pub mod multipart;
pub mod streaming;
@ -18,10 +17,10 @@ async fn index(info: web::Json<Info>) -> Result<String> {
Ok(format!("Welcome {}!", info.username))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,6 +1,5 @@
// <json-manual>
use actix_web::{error, web, App, Error, HttpResponse};
use bytes::BytesMut;
use actix_web::{error, post, web, App, Error, HttpResponse};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use serde_json;
@ -13,9 +12,10 @@ struct MyObj {
const MAX_SIZE: usize = 262_144; // max payload size is 256k
#[post("/")]
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
@ -31,12 +31,12 @@ async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error>
}
// </json-manual>
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::HttpServer;
HttpServer::new(|| App::new().route("/", web::post().to(index_manual)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index_manual))
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,7 +1,8 @@
// <streaming>
use actix_web::{web, Error, HttpResponse};
use actix_web::{get, web, Error, HttpResponse};
use futures::StreamExt;
#[get("/")]
async fn index(mut body: web::Payload) -> Result<HttpResponse, Error> {
let mut bytes = web::BytesMut::new();
while let Some(item) = body.next().await {
@ -14,12 +15,12 @@ async fn index(mut body: web::Payload) -> Result<HttpResponse, Error> {
}
// </streaming>
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,5 +1,5 @@
// <urlencoded>
use actix_web::{web, HttpResponse};
use actix_web::{post, web, HttpResponse};
use serde::Deserialize;
#[derive(Deserialize)]
@ -7,17 +7,18 @@ struct FormData {
username: String,
}
#[post("/")]
async fn index(form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().body(format!("username: {}", form.username))
}
// </urlencoded>
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}