mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
v3 (#188)
This commit is contained in:
@ -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>
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user