1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00

First pass at Responses section.

This commit is contained in:
Cameron Dershem
2019-06-17 15:15:33 -04:00
parent 507842bf1c
commit ea1600c417
10 changed files with 135 additions and 96 deletions

View File

@ -0,0 +1,10 @@
[package]
name = "responses"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "1.0"
serde = "1.0"
futures = "0.1"
bytes = "0.4"

View File

@ -0,0 +1,16 @@
// <auto>
use actix_web::{
http::ContentEncoding, middleware, web, App, HttpRequest, HttpResponse,
};
fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok().body("data")
}
fn main() {
let app = App::new()
// v- disable compression for all routes
.wrap(middleware::Compress::new(ContentEncoding::Identity))
.route("/", web::get().to(index));
}
// </auto>

View File

@ -0,0 +1,13 @@
// <brotli>
use actix_web::{
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
};
fn index_br(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.encoding(ContentEncoding::Br)
.body("data")
}
// </brotli>
fn main() {}

View File

@ -0,0 +1,14 @@
// <chunked>
// use actix_web::{web, HttpRequest, HttpResponse};
// use bytes::Bytes;
// use futures::stream::once;
// fn index(req: HttpRequest) -> HttpResponse {
// HttpResponse::Ok()
// .chunked()
// .body(Body::Streaming(Box::new(once(Ok(Bytes::from_static(
// b"data",
// ))))))
// }
// </chunked>
fn main() {}

View File

@ -0,0 +1,12 @@
// <identity>
use actix_web::{
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
};
fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
// v- disable compression
.encoding(ContentEncoding::Identity)
.body("data")
}
// </identity>

View File

@ -0,0 +1,18 @@
// <identity-two>
use actix_web::{
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
};
static HELLO_WORLD: &[u8] = &[
0x1f, 0x8b, 0x08, 0x00, 0xa2, 0x30, 0x10, 0x5c, 0x00, 0x03, 0xcb, 0x48, 0xcd, 0xc9,
0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf,
0x0c, 0x00, 0x00, 0x00,
];
pub fn index(_req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.encoding(ContentEncoding::Identity)
.header("content-encoding", "gzip")
.body(HELLO_WORLD)
}
// </identity-two>

View File

@ -0,0 +1,19 @@
// <json-resp>
use actix_web::{web, App, HttpRequest, Result};
use serde::Serialize;
#[derive(Serialize)]
struct MyObj {
name: String,
}
fn index(req: HttpRequest) -> Result<web::Json<MyObj>> {
Ok(web::Json(MyObj {
name: req.match_info().query("name").to_string(),
}))
}
fn main() {
App::new().route(r"/a/{name}", web::get().to(index));
}
// </json-resp>

View File

@ -0,0 +1,21 @@
mod auto;
mod brotli;
mod chunked;
mod identity;
mod identity_two;
mod json_resp;
// <builder>
use actix_web::{
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
};
fn index(_req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.encoding(ContentEncoding::Br)
.content_type("plain/text")
.header("X-Hdr", "sample")
.body("data")
}
// </builder>
fn main() {}