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:
10
examples/responses/Cargo.toml
Normal file
10
examples/responses/Cargo.toml
Normal 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"
|
16
examples/responses/src/auto.rs
Normal file
16
examples/responses/src/auto.rs
Normal 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>
|
13
examples/responses/src/brotli.rs
Normal file
13
examples/responses/src/brotli.rs
Normal 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() {}
|
14
examples/responses/src/chunked.rs
Normal file
14
examples/responses/src/chunked.rs
Normal 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() {}
|
12
examples/responses/src/identity.rs
Normal file
12
examples/responses/src/identity.rs
Normal 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>
|
18
examples/responses/src/identity_two.rs
Normal file
18
examples/responses/src/identity_two.rs
Normal 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>
|
19
examples/responses/src/json_resp.rs
Normal file
19
examples/responses/src/json_resp.rs
Normal 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>
|
21
examples/responses/src/main.rs
Normal file
21
examples/responses/src/main.rs
Normal 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() {}
|
Reference in New Issue
Block a user