From 1c3697197dcf5489be9bfb07ab5ebd33d2c20699 Mon Sep 17 00:00:00 2001 From: Cameron Dershem Date: Wed, 26 Jun 2019 01:58:47 -0400 Subject: [PATCH] Responses section is done-ish. --- content/docs/response.md | 16 ++++++++-------- examples/responses/src/auto.rs | 22 ++++++++++++++-------- examples/responses/src/brotli.rs | 15 +++++++++------ examples/responses/src/chunked.rs | 11 ++++++++++- examples/responses/src/identity.rs | 15 +++++++++------ examples/responses/src/identity_two.rs | 9 +++++++-- examples/responses/src/json_resp.rs | 20 +++++++++++++------- examples/responses/src/main.rs | 18 +++++++++++------- 8 files changed, 81 insertions(+), 45 deletions(-) diff --git a/content/docs/response.md b/content/docs/response.md index 62f370d..54a62b7 100644 --- a/content/docs/response.md +++ b/content/docs/response.md @@ -6,15 +6,15 @@ weight: 210 # Response -A builder-like pattern is used to construct an instance of `HttpResponse`. -`HttpResponse` provides several methods that return a `HttpResponseBuilder` instance, -which implements various convenience methods for building responses. +A builder-like pattern is used to construct an instance of `HttpResponse`. `HttpResponse` +provides several methods that return a `HttpResponseBuilder` instance, which implements +various convenience methods for building responses. > Check the [documentation][responsebuilder] for type descriptions. -The methods `.body`, `.finish`, and `.json` finalize response creation and -return a constructed *HttpResponse* instance. If this methods is called on the same -builder instance multiple times, the builder will panic. +The methods `.body`, `.finish`, and `.json` finalize response creation and return a +constructed *HttpResponse* instance. If this methods is called on the same builder +instance multiple times, the builder will panic. {{< include-example example="responses" file="main.rs" section="builder" >}} @@ -39,8 +39,8 @@ For example, to enable `brotli` use `ContentEncoding::Br`: {{< include-example example="responses" file="brotli.rs" section="brotli" >}} -In this case we explicitly disable content compression -by setting content encoding to a `Identity` value: +In this case we explicitly disable content compression by setting content encoding to +an `Identity` value: {{< include-example example="responses" file="identity.rs" section="identity" >}} diff --git a/examples/responses/src/auto.rs b/examples/responses/src/auto.rs index d4b2cb7..bd0f14e 100644 --- a/examples/responses/src/auto.rs +++ b/examples/responses/src/auto.rs @@ -1,16 +1,22 @@ // -use actix_web::{ - http::ContentEncoding, middleware, web, App, HttpRequest, HttpResponse, -}; +use actix_web::{http::ContentEncoding, middleware, HttpResponse}; -fn index(_req: HttpRequest) -> HttpResponse { +fn index() -> HttpResponse { HttpResponse::Ok().body("data") } pub fn main() { - App::new() - // v- disable compression for all routes - .wrap(middleware::Compress::new(ContentEncoding::Identity)) - .route("/", web::get().to(index)); + use actix_web::{web, App, HttpServer}; + + HttpServer::new(|| { + App::new() + // v- disable compression for all routes + .wrap(middleware::Compress::new(ContentEncoding::Identity)) + .route("/", web::get().to(index)) + }) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } // diff --git a/examples/responses/src/brotli.rs b/examples/responses/src/brotli.rs index 79717ff..5694b28 100644 --- a/examples/responses/src/brotli.rs +++ b/examples/responses/src/brotli.rs @@ -1,16 +1,19 @@ // -use actix_web::{ - http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse, -}; +use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse}; -fn index_br(_req: HttpRequest) -> HttpResponse { +fn index_br() -> HttpResponse { HttpResponse::Ok() .encoding(ContentEncoding::Br) .body("data") } // -use actix_web::{web, App}; pub fn main() { - App::new().route("/", web::get().to(index_br)); + use actix_web::{web, App, HttpServer}; + + HttpServer::new(|| App::new().route("/", web::get().to(index_br))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } diff --git a/examples/responses/src/chunked.rs b/examples/responses/src/chunked.rs index a2c4a62..50899cb 100644 --- a/examples/responses/src/chunked.rs +++ b/examples/responses/src/chunked.rs @@ -11,4 +11,13 @@ // )))))) // } // -pub fn main() {} + +// pub fn main() { +// use actix_web::{web, App, HttpServer}; + +// HttpServer::new(|| App::new().route("/", web::get().to(index))) +// .bind("127.0.0.1:8088") +// .unwrap() +// .run() +// .unwrap(); +// } diff --git a/examples/responses/src/identity.rs b/examples/responses/src/identity.rs index 039d713..63fb968 100644 --- a/examples/responses/src/identity.rs +++ b/examples/responses/src/identity.rs @@ -1,9 +1,7 @@ // -use actix_web::{ - http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse, -}; +use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse}; -fn index(_req: HttpRequest) -> HttpResponse { +fn index() -> HttpResponse { HttpResponse::Ok() // v- disable compression .encoding(ContentEncoding::Identity) @@ -11,7 +9,12 @@ fn index(_req: HttpRequest) -> HttpResponse { } // -use actix_web::{web, App}; pub fn main() { - App::new().route("/", web::get().to(index)); + use actix_web::{web, App, HttpServer}; + + HttpServer::new(|| App::new().route("/", web::get().to(index))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } diff --git a/examples/responses/src/identity_two.rs b/examples/responses/src/identity_two.rs index 0ac8f65..8e6020f 100644 --- a/examples/responses/src/identity_two.rs +++ b/examples/responses/src/identity_two.rs @@ -17,7 +17,12 @@ pub fn index(_req: HttpRequest) -> HttpResponse { } // -use actix_web::{web, App}; pub fn main() { - App::new().route("/", web::get().to(index)); + use actix_web::{web, App, HttpServer}; + + HttpServer::new(|| App::new().route("/", web::get().to(index))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } diff --git a/examples/responses/src/json_resp.rs b/examples/responses/src/json_resp.rs index 96d9591..7fc329d 100644 --- a/examples/responses/src/json_resp.rs +++ b/examples/responses/src/json_resp.rs @@ -1,19 +1,25 @@ // -use actix_web::{web, App, HttpRequest, Result}; -use serde::Serialize; +use actix_web::{web, HttpResponse, Result}; +use serde::{Deserialize, Serialize}; -#[derive(Serialize)] +#[derive(Serialize, Deserialize)] struct MyObj { name: String, } -fn index(req: HttpRequest) -> Result> { - Ok(web::Json(MyObj { - name: req.match_info().query("name").to_string(), +fn index(obj: web::Path) -> Result { + Ok(HttpResponse::Ok().json(MyObj { + name: obj.name.to_string(), })) } pub fn main() { - App::new().route(r"/a/{name}", web::get().to(index)); + use actix_web::{App, HttpServer}; + + HttpServer::new(|| App::new().route(r"/a/{name}", web::get().to(index))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } // diff --git a/examples/responses/src/main.rs b/examples/responses/src/main.rs index 943612e..1d63343 100644 --- a/examples/responses/src/main.rs +++ b/examples/responses/src/main.rs @@ -4,12 +4,11 @@ pub mod chunked; pub mod identity; pub mod identity_two; pub mod json_resp; -// -use actix_web::{ - http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse, -}; -fn index(_req: HttpRequest) -> HttpResponse { +// +use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse}; + +fn index() -> HttpResponse { HttpResponse::Ok() .encoding(ContentEncoding::Br) .content_type("plain/text") @@ -18,7 +17,12 @@ fn index(_req: HttpRequest) -> HttpResponse { } // -use actix_web::{web, App}; pub fn main() { - App::new().route("/", web::get().to(index)); + use actix_web::{web, App, HttpServer}; + + HttpServer::new(|| App::new().route("/", web::get().to(index))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); }