From 5d8cbccfe961229c468ea8b8551c02098614510f Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 15:12:06 -0400 Subject: [PATCH 01/14] Remove article. --- guide/src/qs_1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guide/src/qs_1.md b/guide/src/qs_1.md index aac24a7de..5e31aec63 100644 --- a/guide/src/qs_1.md +++ b/guide/src/qs_1.md @@ -2,7 +2,7 @@ ## Install Rust -Before we begin, we need to install Rust using the [rustup](https://www.rustup.rs/): +Before we begin, we need to install Rust using [rustup](https://www.rustup.rs/): ```bash curl https://sh.rustup.rs -sSf | sh From 0fbd05009df877a450ac6fe4563b3c7f03e318b3 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 17:31:18 -0400 Subject: [PATCH 02/14] Guide: tweaks to the request and response chapter. --- guide/src/qs_7.md | 61 +++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/guide/src/qs_7.md b/guide/src/qs_7.md index fab21a34b..9b6649390 100644 --- a/guide/src/qs_7.md +++ b/guide/src/qs_7.md @@ -5,8 +5,11 @@ 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 that helps building responses. -Check [documentation](../actix_web/dev/struct.HttpResponseBuilder.html) -for type descriptions. The methods `.body`, `.finish`, `.json` finalize response creation and + +> Check the [documentation](../actix_web/dev/struct.HttpResponseBuilder.html) +> for type descriptions. + +The methods `.body`, `.finish`, `.json` finalize response creation and return a constructed *HttpResponse* instance. If this methods is called for the same builder instance multiple times, the builder will panic. @@ -28,19 +31,19 @@ fn index(req: HttpRequest) -> HttpResponse { Actix automatically *compresses*/*decompresses* payloads. Following codecs are supported: - * Brotli - * Gzip - * Deflate - * Identity +* Brotli +* Gzip +* Deflate +* Identity - If request headers contain a `Content-Encoding` header, the request payload is decompressed - according to the header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`. +If request headers contain a `Content-Encoding` header, the request payload is decompressed +according to the header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`. Response payload is compressed based on the *content_encoding* parameter. -By default `ContentEncoding::Auto` is used. If `ContentEncoding::Auto` is selected +By default `ContentEncoding::Auto` is used. If `ContentEncoding::Auto` is selected, then compression depends on the request's `Accept-Encoding` header. `ContentEncoding::Identity` can be used to disable compression. -If another content encoding is selected the compression is enforced for this codec. For example, +If another content encoding is selected, the compression is enforced for this codec. For example, to enable `brotli` use `ContentEncoding::Br`: ```rust @@ -55,13 +58,12 @@ fn index(req: HttpRequest) -> HttpResponse { # fn main() {} ``` - ## JSON Request There are several options for json body deserialization. -The first option is to use *Json* extractor. You define handler function -that accepts `Json` as a parameter and use `.with()` method for registering +The first option is to use *Json* extractor. You define a handler function +that accepts `Json` as a parameter and use the `.with()` method for registering this handler. It is also possible to accept arbitrary valid json object by using `serde_json::Value` as a type `T` @@ -116,8 +118,9 @@ fn index(mut req: HttpRequest) -> Box> { # fn main() {} ``` -Or you can manually load the payload into memory and then deserialize it. -Here is a simple example. We will deserialize a *MyObj* struct. We need to load the request +Alternatively, you can manually load the payload into memory and then deserialize it. + +In the following example, we will deserialize a *MyObj* struct. We need to load the request body first and then deserialize the json into an object. ```rust @@ -149,9 +152,8 @@ fn index(req: HttpRequest) -> Box> { # fn main() {} ``` -A complete example for both options is available in -[examples directory](https://github.com/actix/actix-web/tree/master/examples/json/). - +> A complete example for both options is available in +> [examples directory](https://github.com/actix/actix-web/tree/master/examples/json/). ## JSON Response @@ -186,12 +188,12 @@ Actix automatically decodes *chunked* encoding. `HttpRequest::payload()` already the decoded byte stream. If the request payload is compressed with one of the supported compression codecs (br, gzip, deflate) the byte stream is decompressed. -Chunked encoding on response can be enabled with `HttpResponseBuilder::chunked()`. -But this takes effect only for `Body::Streaming(BodyStream)` or `Body::StreamingContext` bodies. -Also if response payload compression is enabled and streaming body is used, chunked encoding +Chunked encoding on response can be enabled with `HttpResponseBuilder::chunked()`, +but this takes effect only for `Body::Streaming(BodyStream)` or `Body::StreamingContext` bodies. +Also, if the response payload compression is enabled and a streaming body is used, chunked encoding is enabled automatically. -Enabling chunked encoding for *HTTP/2.0* responses is forbidden. +> Enabling chunked encoding for *HTTP/2.0* responses is forbidden. ```rust # extern crate bytes; @@ -218,7 +220,7 @@ a stream of multipart items, each item can be a [*Field*](../actix_web/multipart/struct.Field.html) or a nested *Multipart* stream. `HttpResponse::multipart()` returns the *Multipart* stream for the current request. -In simple form multipart stream handling can be implemented similar to this example +Simple form multipart stream handling could be implemented like the following: ```rust,ignore # extern crate actix_web; @@ -248,17 +250,18 @@ fn index(req: HttpRequest) -> Box> { } ``` -A full example is available in the -[examples directory](https://github.com/actix/actix-web/tree/master/examples/multipart/). +> A full example is available in the +> [examples directory](https://github.com/actix/actix-web/tree/master/examples/multipart/). ## Urlencoded body Actix provides support for *application/x-www-form-urlencoded* encoded bodies. `HttpResponse::urlencoded()` returns a [*UrlEncoded*](../actix_web/dev/struct.UrlEncoded.html) future, which resolves -to the deserialized instance, the type of the instance must implement the -`Deserialize` trait from *serde*. The *UrlEncoded* future can resolve into -a error in several cases: +to the deserialized instance. The type of the instance must implement the +`Deserialize` trait from *serde*. + +The *UrlEncoded* future can resolve into an error in several cases: * content type is not `application/x-www-form-urlencoded` * transfer encoding is `chunked`. @@ -294,7 +297,7 @@ fn index(mut req: HttpRequest) -> Box> { *HttpRequest* is a stream of `Bytes` objects. It can be used to read the request body payload. -In this example handle reads the request payload chunk by chunk and prints every chunk. +In the following example, we read and print the request payload chunk by chunk: ```rust # extern crate actix_web; From ab60ec6e1df60a8832f3f141701b30f0099a7bd7 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 18:03:30 -0400 Subject: [PATCH 03/14] Guide: updates to the Testing chapter. --- guide/src/qs_8.md | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/guide/src/qs_8.md b/guide/src/qs_8.md index 380f9e0e7..9d6327cfa 100644 --- a/guide/src/qs_8.md +++ b/guide/src/qs_8.md @@ -5,9 +5,9 @@ integration tests. ## Unit tests -For unit testing actix provides a request builder type and simple handler runner. +For unit testing, actix provides a request builder type and simple handler runner. [*TestRequest*](../actix_web/test/struct.TestRequest.html) implements a builder-like pattern. -You can generate a `HttpRequest` instance with `finish()` or you can +You can generate a `HttpRequest` instance with `finish()`, or you can run your handler with `run()` or `run_async()`. ```rust @@ -36,19 +36,20 @@ fn main() { } ``` - ## Integration tests -There are several methods how you can test your application. Actix provides -[*TestServer*](../actix_web/test/struct.TestServer.html) -server that can be used to run the whole application of just specific handlers -in real http server. *TestServer::get()*, *TestServer::post()* or *TestServer::client()* +There are several methods for testing your application. Actix provides +[*TestServer*](../actix_web/test/struct.TestServer.html), which can be used +to run the application with specific handlers in a real http server. + +`TestServer::get()`, `TestServer::post()`, or `TestServer::client()` methods can be used to send requests to the test server. -In simple form *TestServer* can be configured to use handler. *TestServer::new* method -accepts configuration function, only argument for this function is *test application* -instance. You can check the [api documentation](../actix_web/test/struct.TestApp.html) -for more information. +A simple form `TestServer` can be configured to use a handler. +`TestServer::new` method accepts a configuration function, and the only argument +for this function is a *test application* instance. + +> Check the [api documentation](../actix_web/test/struct.TestApp.html) for more information. ```rust # extern crate actix_web; @@ -70,8 +71,8 @@ fn main() { } ``` -The other option is to use an application factory. In this case you need to pass the factory -function same way as you would for real http server configuration. +The other option is to use an application factory. In this case, you need to pass the factory +function the same way as you would for real http server configuration. ```rust # extern crate actix_web; @@ -98,11 +99,10 @@ fn main() { } ``` -If you need more complex application configuration, for example you may need to -initialize application state or start `SyncActor`'s for diesel interation, you -can use `TestServer::build_with_state()` method. This method accepts closure -that has to construct application state. This closure runs when actix system is -configured already, so you can initialize any additional actors. +If you need more complex application configuration, use the `TestServer::build_with_state()` +method. For example, you may need to initialize application state or start `SyncActor`'s for diesel +interation. This method accepts a closure that constructs the application state, +and it runs when the actix system is configured. Thus, you can initialize any additional actors. ```rust,ignore #[test] @@ -127,10 +127,10 @@ fn test() { ## WebSocket server tests -It is possible to register a *handler* with `TestApp::handler()` that -initiates a web socket connection. *TestServer* provides `ws()` which connects to +It is possible to register a *handler* with `TestApp::handler()`, which +initiates a web socket connection. *TestServer* provides the method `ws()`, which connects to the websocket server and returns ws reader and writer objects. *TestServer* also -provides an `execute()` method which runs future objects to completion and returns +provides an `execute()` method, which runs future objects to completion and returns result of the future computation. Here is a simple example that shows how to test server websocket handler. From 1f08100f6f7bbeac92f6c689c174394c69d4ec70 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 18:04:42 -0400 Subject: [PATCH 04/14] Guide: updates to the WebSockets chapter. --- guide/src/qs_9.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/guide/src/qs_9.md b/guide/src/qs_9.md index 158ba2513..b7fdc840a 100644 --- a/guide/src/qs_9.md +++ b/guide/src/qs_9.md @@ -3,10 +3,10 @@ Actix supports WebSockets out-of-the-box. It is possible to convert a request's `Payload` to a stream of [*ws::Message*](../actix_web/ws/enum.Message.html) with a [*ws::WsStream*](../actix_web/ws/struct.WsStream.html) and then use stream -combinators to handle actual messages. But it is simpler to handle websocket communications +combinators to handle actual messages, but it is simpler to handle websocket communications with an http actor. -This is example of a simple websocket echo server: +The following is example of a simple websocket echo server: ```rust # extern crate actix; @@ -41,8 +41,8 @@ fn main() { } ``` -A simple websocket echo server example is available in the -[examples directory](https://github.com/actix/actix-web/blob/master/examples/websocket). +> A simple websocket echo server example is available in the +> [examples directory](https://github.com/actix/actix-web/blob/master/examples/websocket). -An example chat server with the ability to chat over a websocket or tcp connection -is available in [websocket-chat directory](https://github.com/actix/actix-web/tree/master/examples/websocket-chat/) +> An example chat server with the ability to chat over a websocket or tcp connection +> is available in [websocket-chat directory](https://github.com/actix/actix-web/tree/master/examples/websocket-chat/) From a88e97edba030bde272e8739bf0b6d34d0aabc9b Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 18:29:18 -0400 Subject: [PATCH 05/14] Guide: updates to Middleware chapter. --- guide/src/qs_10.md | 78 ++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/guide/src/qs_10.md b/guide/src/qs_10.md index ce1ed4a7e..0594c04bf 100644 --- a/guide/src/qs_10.md +++ b/guide/src/qs_10.md @@ -1,23 +1,25 @@ # Middleware -Actix' middleware system allows to add additional behavior to request/response processing. -Middleware can hook into incoming request process and modify request or halt request -processing and return response early. Also it can hook into response processing. +Actix's middleware system allows us to add additional behavior to request/response processing. +Middleware can hook into an incoming request process, enabling the ability to modify requests +as well as the ability to halt request processing and return response early. -Typically middlewares are involved in the following actions: +Middleware can also hook into response processing. + +Typically, middleware is involved in the following actions: * Pre-process the Request * Post-process a Response * Modify application state * Access external services (redis, logging, sessions) -Middlewares are registered for each application and are executed in same order as -registration order. In general, a *middleware* is a type that implements the +Middleware is registered for each application and executed in same order as +registration. In general, a *middleware* is a type that implements the [*Middleware trait*](../actix_web/middlewares/trait.Middleware.html). Each method in this trait has a default implementation. Each method can return a result immediately or a *future* object. -Here is an example of a simple middleware that adds request and response headers: +The following is an example of a simple middleware that adds request and response headers: ```rust # extern crate http; @@ -57,16 +59,17 @@ fn main() { } ``` -Actix provides several useful middlewares, like *logging*, *user sessions*, etc. - +> Actix provides several useful middlewares, such as *logging*, *user sessions*, etc. ## Logging Logging is implemented as a middleware. It is common to register a logging middleware as the first middleware for the application. -Logging middleware has to be registered for each application. *Logger* middleware -uses the standard log crate to log information. You should enable logger for *actix_web* -package to see access log ([env_logger](https://docs.rs/env_logger/*/env_logger/) or similar). +Logging middleware must be registered for each application. + +The `Logger` middleware uses the standard log crate to log information. You should enable logger +for *actix_web* package to see access log +([env_logger](https://docs.rs/env_logger/*/env_logger/) or similar). ### Usage @@ -76,6 +79,7 @@ Default `Logger` can be created with `default` method, it uses the default forma ```ignore %a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T ``` + ```rust # extern crate actix_web; extern crate env_logger; @@ -93,7 +97,7 @@ fn main() { } ``` -Here is an example of the default logging format: +The following is an example of the default logging format: ``` INFO:actix_web::middleware::logger: 127.0.0.1:59934 [02/Dec/2017:00:21:43 -0800] "GET / HTTP/1.1" 302 0 "-" "curl/7.54.0" 0.000397 @@ -126,12 +130,11 @@ INFO:actix_web::middleware::logger: 127.0.0.1:59947 [02/Dec/2017:00:22:40 -0800] `%{FOO}e` os.environ['FOO'] - ## Default headers -To set default response headers the `DefaultHeaders` middleware can be used. The +To set default response headers, the `DefaultHeaders` middleware can be used. The *DefaultHeaders* middleware does not set the header if response headers already contain -the specified header. +a specified header. ```rust # extern crate actix_web; @@ -153,27 +156,28 @@ fn main() { ## User sessions Actix provides a general solution for session management. The -[*Session storage*](../actix_web/middleware/struct.SessionStorage.html) middleware can be +[**SessionStorage**](../actix_web/middleware/struct.SessionStorage.html) middleware can be used with different backend types to store session data in different backends. -By default only cookie session backend is implemented. Other backend implementations -could be added later. -[*Cookie session backend*](../actix_web/middleware/struct.CookieSessionBackend.html) -uses signed cookies as session storage. *Cookie session backend* creates sessions which -are limited to storing fewer than 4000 bytes of data (as the payload must fit into a -single cookie). Internal server error is generated if session contains more than 4000 bytes. +> By default, only cookie session backend is implemented. Other backend implementations +> can be added. -You need to pass a random value to the constructor of *CookieSessionBackend*. -This is private key for cookie session. When this value is changed, all session data is lost. -Note that whatever you write into your session is visible by the user (but not modifiable). +[**CookieSessionBackend**](../actix_web/middleware/struct.CookieSessionBackend.html) +uses signed cookies as session storage. `CookieSessionBackend` creates sessions which +are limited to storing fewer than 4000 bytes of data, as the payload must fit into a +single cookie. An internal server error is generated if a session contains more than 4000 bytes. -In general case, you create -[*Session storage*](../actix_web/middleware/struct.SessionStorage.html) middleware -and initializes it with specific backend implementation, like *CookieSessionBackend*. -To access session data +You need to pass a random value to the constructor of `CookieSessionBackend`. +This is a private key for cookie session. When this value is changed, all session data is lost. + +> **Note**: anything you write into the session is visible by the user, but it is not modifiable. + +In general, you create a +`SessionStorage` middleware and initialize it with specific backend implementation, +such as a `CookieSessionBackend`. To access session data, [*HttpRequest::session()*](../actix_web/middleware/trait.RequestSession.html#tymethod.session) - has to be used. This method returns a -[*Session*](../actix_web/middleware/struct.Session.html) object, which allows to get or set + must be used. This method returns a +[*Session*](../actix_web/middleware/struct.Session.html) object, which allows us to get or set session data. ```rust @@ -212,12 +216,12 @@ fn main() { ## Error handlers -`ErrorHandlers` middleware allows to provide custom handlers for responses. +`ErrorHandlers` middleware allows us to provide custom handlers for responses. -You can use `ErrorHandlers::handler()` method to register a custom error handler -for specific status code. You can modify existing response or create completly new -one. Error handler can return response immediately or return future that resolves -to a response. +You can use the `ErrorHandlers::handler()` method to register a custom error handler +for specific status code. You can modify an existing response or create completly new +one. The error handler can return a response immediately or return a future that resolves +into a response. ```rust # extern crate actix_web; From c3fbba26786029d067a2e4f39b1300c13815d609 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 18:40:57 -0400 Subject: [PATCH 06/14] Guide: updates to static file handling chapter. --- guide/src/qs_12.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/guide/src/qs_12.md b/guide/src/qs_12.md index 1da5f1ef9..2feb5a1be 100644 --- a/guide/src/qs_12.md +++ b/guide/src/qs_12.md @@ -2,8 +2,8 @@ ## Individual file -It is possible to serve static files with custom path pattern and `NamedFile`. To -match path tail we can use `[.*]` regex. +It is possible to serve static files with a custom path pattern and `NamedFile`. To +match a path tail, we can use a `[.*]` regex. ```rust # extern crate actix_web; @@ -24,9 +24,9 @@ fn main() { ## Directory -To serve files from specific directory and sub-directories `StaticFiles` could be used. -`StaticFiles` must be registered with `App::handler()` method otherwise -it won't be able to serve sub-paths. +To serve files from specific directories and sub-directories, `StaticFiles` can be used. +`StaticFiles` must be registered with an `App::handler()` method, otherwise +it will be unable to serve sub-paths. ```rust # extern crate actix_web; @@ -39,11 +39,11 @@ fn main() { } ``` -First parameter is a base directory. Second parameter is *show_index*, if it is set to *true* -directory listing would be returned for directories, if it is set to *false* -then *404 Not Found* would be returned instead of directory listing. +The first parameter is the base directory. If the second parameter, *show_index*, is set to **true**, +the directory listing will be returned, and if it is set to **false**, +*404 Not Found* will be returned. -Instead of showing files listing for directory, it is possible to redirect to specific -index file. Use +Instead of showing files listing for directory, it is possible to redirect to a specific +index file. Use the [*StaticFiles::index_file()*](../actix_web/s/struct.StaticFiles.html#method.index_file) method to configure this redirect. From e7f9f5b46d6e4e754d3f0a7eced79b330e1b5f14 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 18:46:56 -0400 Subject: [PATCH 07/14] Guide: updates to HTTP/2 chapter. --- guide/src/qs_13.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/guide/src/qs_13.md b/guide/src/qs_13.md index 753a9c16f..bd25b7b60 100644 --- a/guide/src/qs_13.md +++ b/guide/src/qs_13.md @@ -1,13 +1,15 @@ # HTTP/2.0 -Actix web automatically upgrades connection to *HTTP/2.0* if possible. +Actix web automatically upgrades connections to *HTTP/2.0* if possible. ## Negotiation *HTTP/2.0* protocol over tls without prior knowledge requires -[tls alpn](https://tools.ietf.org/html/rfc7301). At the moment only -`rust-openssl` has support. Turn on the `alpn` feature to enable `alpn` negotiation. -With enabled `alpn` feature `HttpServer` provides the +[tls alpn](https://tools.ietf.org/html/rfc7301). + +> Currently, only `rust-openssl` has support. + +`alpn` negotiation requires enabling the feature. When enabled, `HttpServer` provides the [serve_tls](../actix_web/struct.HttpServer.html#method.serve_tls) method. ```toml @@ -35,10 +37,10 @@ fn main() { } ``` -Upgrade to *HTTP/2.0* schema described in +Upgrades to *HTTP/2.0* schema described in [rfc section 3.2](https://http2.github.io/http2-spec/#rfc.section.3.2) is not supported. Starting *HTTP/2* with prior knowledge is supported for both clear text connection and tls connection. [rfc section 3.4](https://http2.github.io/http2-spec/#rfc.section.3.4) -Please check [example](https://github.com/actix/actix-web/tree/master/examples/tls) -for a concrete example. +> Check the [examples/tls](https://github.com/actix/actix-web/tree/master/examples/tls) +> for a concrete example. From 0f0fe5f14893302033729189eed8e4e6e71f17fd Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:02:11 -0400 Subject: [PATCH 08/14] Guide: updates to the Database integration chapter. --- guide/src/qs_14.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/guide/src/qs_14.md b/guide/src/qs_14.md index a805e7a58..0fe16f174 100644 --- a/guide/src/qs_14.md +++ b/guide/src/qs_14.md @@ -2,13 +2,14 @@ ## Diesel -At the moment of 1.0 release Diesel does not support asynchronous operations. -But it possible to use the `actix` synchronous actor system as a db interface api. -Technically sync actors are worker style actors, multiple of them -can be run in parallel and process messages from same queue (sync actors work in mpsc mode). +At the moment, Diesel 1.0 does not support asynchronous operations, +but it possible to use the `actix` synchronous actor system as a database interface api. -Let's create a simple db api that can insert a new user row into an SQLite table. -We have to define sync actor and connection that this actor will use. The same approach +Technically, sync actors are worker style actors. Multiple sync actors +can be run in parallel and process messages from same queue. Sync actors work in mpsc mode. + +Let's create a simple database api that can insert a new user row into a SQLite table. +We must define a sync actor and a connection that this actor will use. The same approach can be used for other databases. ```rust,ignore @@ -21,7 +22,7 @@ impl Actor for DbExecutor { } ``` -This is the definition of our actor. Now we need to define the *create user* message and response. +This is the definition of our actor. Now, we must define the *create user* message and response. ```rust,ignore struct CreateUser { @@ -33,8 +34,8 @@ impl Message for CreateUser { } ``` -We can send a `CreateUser` message to the `DbExecutor` actor, and as a result we get a -`User` model instance. Now we need to define the actual handler implementation for this message. +We can send a `CreateUser` message to the `DbExecutor` actor, and as a result, we receive a +`User` model instance. Next, we must define the handler implementation for this message. ```rust,ignore impl Handler for DbExecutor { @@ -67,7 +68,7 @@ impl Handler for DbExecutor { } ``` -That's it. Now we can use the *DbExecutor* actor from any http handler or middleware. +That's it! Now, we can use the *DbExecutor* actor from any http handler or middleware. All we need is to start *DbExecutor* actors and store the address in a state where http handler can access it. @@ -97,9 +98,9 @@ fn main() { } ``` -And finally we can use the address in a request handler. We get a message response -asynchronously, so the handler needs to return a future object, also `Route::a()` needs to be -used for async handler registration. +Finally, we use the address in a request handler. We receive the message response +asynchronously, thus the handler returns a future object. +`Route::a()` must be used for async handler registration. ```rust,ignore @@ -120,8 +121,8 @@ fn index(req: HttpRequest) -> Box> } ``` -Full example is available in the -[examples directory](https://github.com/actix/actix-web/tree/master/examples/diesel/). +> A full example is available in the +> [examples directory](https://github.com/actix/actix-web/tree/master/examples/diesel/). -More information on sync actors can be found in the -[actix documentation](https://docs.rs/actix/0.5.0/actix/sync/index.html). +> More information on sync actors can be found in the +> [actix documentation](https://docs.rs/actix/0.5.0/actix/sync/index.html). From 7cff5d9adecc6a5830dfe38586fb272b6bc37d7e Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:17:03 -0400 Subject: [PATCH 09/14] Guide: additional tweaks to request and response chapter. --- guide/src/qs_7.md | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/guide/src/qs_7.md b/guide/src/qs_7.md index 9b6649390..3e8694514 100644 --- a/guide/src/qs_7.md +++ b/guide/src/qs_7.md @@ -4,13 +4,13 @@ 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 that helps building responses. +which implements various convenience methods for building responses. > Check the [documentation](../actix_web/dev/struct.HttpResponseBuilder.html) > for type descriptions. -The methods `.body`, `.finish`, `.json` finalize response creation and -return a constructed *HttpResponse* instance. If this methods is called for the same +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. ```rust @@ -29,7 +29,7 @@ fn index(req: HttpRequest) -> HttpResponse { ## Content encoding -Actix automatically *compresses*/*decompresses* payloads. Following codecs are supported: +Actix automatically *compresses*/*decompresses* payloads. The following codecs are supported: * Brotli * Gzip @@ -40,11 +40,13 @@ If request headers contain a `Content-Encoding` header, the request payload is d according to the header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`. Response payload is compressed based on the *content_encoding* parameter. -By default `ContentEncoding::Auto` is used. If `ContentEncoding::Auto` is selected, -then compression depends on the request's `Accept-Encoding` header. -`ContentEncoding::Identity` can be used to disable compression. -If another content encoding is selected, the compression is enforced for this codec. For example, -to enable `brotli` use `ContentEncoding::Br`: +By default, `ContentEncoding::Auto` is used. If `ContentEncoding::Auto` is selected, +then the compression depends on the request's `Accept-Encoding` header. + +> `ContentEncoding::Identity` can be used to disable compression. +> If another content encoding is selected, the compression is enforced for that codec. + +For example, to enable `brotli` use `ContentEncoding::Br`: ```rust # extern crate actix_web; @@ -62,10 +64,10 @@ fn index(req: HttpRequest) -> HttpResponse { There are several options for json body deserialization. -The first option is to use *Json* extractor. You define a handler function -that accepts `Json` as a parameter and use the `.with()` method for registering +The first option is to use *Json* extractor. First, you define a handler function +that accepts `Json` as a parameter, then, you use the `.with()` method for registering this handler. It is also possible to accept arbitrary valid json object by -using `serde_json::Value` as a type `T` +using `serde_json::Value` as a type `T`. ```rust # extern crate actix_web; @@ -89,7 +91,7 @@ fn main() { } ``` -The second option is to use *HttpResponse::json()*. This method returns a +Another option is to use *HttpResponse::json()*. This method returns a [*JsonBody*](../actix_web/dev/struct.JsonBody.html) object which resolves into the deserialized value. @@ -118,7 +120,7 @@ fn index(mut req: HttpRequest) -> Box> { # fn main() {} ``` -Alternatively, you can manually load the payload into memory and then deserialize it. +You may also manually load the payload into memory and then deserialize it. In the following example, we will deserialize a *MyObj* struct. We need to load the request body first and then deserialize the json into an object. @@ -158,8 +160,8 @@ fn index(req: HttpRequest) -> Box> { ## JSON Response The `Json` type allows to respond with well-formed JSON data: simply return a value of -type Json where T is the type of a structure to serialize into *JSON*. The -type `T` must implement the `Serialize` trait from *serde*. +type Json where `T` is the type of a structure to serialize into *JSON*. +The type `T` must implement the `Serialize` trait from *serde*. ```rust # extern crate actix_web; @@ -186,11 +188,11 @@ fn main() { Actix automatically decodes *chunked* encoding. `HttpRequest::payload()` already contains the decoded byte stream. If the request payload is compressed with one of the supported -compression codecs (br, gzip, deflate) the byte stream is decompressed. +compression codecs (br, gzip, deflate), then the byte stream is decompressed. -Chunked encoding on response can be enabled with `HttpResponseBuilder::chunked()`, -but this takes effect only for `Body::Streaming(BodyStream)` or `Body::StreamingContext` bodies. -Also, if the response payload compression is enabled and a streaming body is used, chunked encoding +Chunked encoding on a response can be enabled with `HttpResponseBuilder::chunked()`. +This takes effect only for `Body::Streaming(BodyStream)` or `Body::StreamingContext` bodies. +If the response payload compression is enabled and a streaming body is used, chunked encoding is enabled automatically. > Enabling chunked encoding for *HTTP/2.0* responses is forbidden. @@ -216,11 +218,11 @@ fn index(req: HttpRequest) -> HttpResponse { Actix provides multipart stream support. [*Multipart*](../actix_web/multipart/struct.Multipart.html) is implemented as -a stream of multipart items, each item can be a +a stream of multipart items. Each item can be a [*Field*](../actix_web/multipart/struct.Field.html) or a nested *Multipart* stream. `HttpResponse::multipart()` returns the *Multipart* stream for the current request. -Simple form multipart stream handling could be implemented like the following: +The following demonstrates multipart stream handling for a simple form: ```rust,ignore # extern crate actix_web; From 1a45dbd768fc1b36404b9e54131811c9d708241f Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:26:07 -0400 Subject: [PATCH 10/14] Guide: additional tweak to testing chapter. --- guide/src/qs_8.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/guide/src/qs_8.md b/guide/src/qs_8.md index 9d6327cfa..358d72ad0 100644 --- a/guide/src/qs_8.md +++ b/guide/src/qs_8.md @@ -5,7 +5,7 @@ integration tests. ## Unit tests -For unit testing, actix provides a request builder type and simple handler runner. +For unit testing, actix provides a request builder type and a simple handler runner. [*TestRequest*](../actix_web/test/struct.TestRequest.html) implements a builder-like pattern. You can generate a `HttpRequest` instance with `finish()`, or you can run your handler with `run()` or `run_async()`. @@ -42,7 +42,7 @@ There are several methods for testing your application. Actix provides [*TestServer*](../actix_web/test/struct.TestServer.html), which can be used to run the application with specific handlers in a real http server. -`TestServer::get()`, `TestServer::post()`, or `TestServer::client()` +`TestServer::get()`, `TestServer::post()`, and `TestServer::client()` methods can be used to send requests to the test server. A simple form `TestServer` can be configured to use a handler. @@ -133,7 +133,7 @@ the websocket server and returns ws reader and writer objects. *TestServer* also provides an `execute()` method, which runs future objects to completion and returns result of the future computation. -Here is a simple example that shows how to test server websocket handler. +The following example shows how to test a server websocket handler: ```rust # extern crate actix; From e4a85a53f46e471c2dee27d056b699111c08b5ef Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:35:11 -0400 Subject: [PATCH 11/14] Guide: additional tweaks to Middleware. --- guide/src/qs_10.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/guide/src/qs_10.md b/guide/src/qs_10.md index 0594c04bf..d4b4addf9 100644 --- a/guide/src/qs_10.md +++ b/guide/src/qs_10.md @@ -1,8 +1,8 @@ # Middleware Actix's middleware system allows us to add additional behavior to request/response processing. -Middleware can hook into an incoming request process, enabling the ability to modify requests -as well as the ability to halt request processing and return response early. +Middleware can hook into an incoming request process, enabling us to modify requests +as well as halt request processing to return a response early. Middleware can also hook into response processing. @@ -19,7 +19,7 @@ registration. In general, a *middleware* is a type that implements the in this trait has a default implementation. Each method can return a result immediately or a *future* object. -The following is an example of a simple middleware that adds request and response headers: +The following demonstrates using middleware to add request and response headers: ```rust # extern crate http; @@ -68,8 +68,8 @@ It is common to register a logging middleware as the first middleware for the ap Logging middleware must be registered for each application. The `Logger` middleware uses the standard log crate to log information. You should enable logger -for *actix_web* package to see access log -([env_logger](https://docs.rs/env_logger/*/env_logger/) or similar). +for *actix_web* package to see access log ([env_logger](https://docs.rs/env_logger/*/env_logger/) +or similar). ### Usage @@ -219,7 +219,7 @@ fn main() { `ErrorHandlers` middleware allows us to provide custom handlers for responses. You can use the `ErrorHandlers::handler()` method to register a custom error handler -for specific status code. You can modify an existing response or create completly new +for a specific status code. You can modify an existing response or create a completly new one. The error handler can return a response immediately or return a future that resolves into a response. From 3a80cb7bf3423d2ba50af016c872ae3c54422254 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:37:14 -0400 Subject: [PATCH 12/14] Guide: tweak to http/2. --- guide/src/qs_13.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guide/src/qs_13.md b/guide/src/qs_13.md index bd25b7b60..963d5598a 100644 --- a/guide/src/qs_13.md +++ b/guide/src/qs_13.md @@ -42,5 +42,5 @@ Upgrades to *HTTP/2.0* schema described in Starting *HTTP/2* with prior knowledge is supported for both clear text connection and tls connection. [rfc section 3.4](https://http2.github.io/http2-spec/#rfc.section.3.4) -> Check the [examples/tls](https://github.com/actix/actix-web/tree/master/examples/tls) +> Check out [examples/tls](https://github.com/actix/actix-web/tree/master/examples/tls) > for a concrete example. From 94b41fd484b46605da30792d7f10031bf5e6f1c7 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:42:18 -0400 Subject: [PATCH 13/14] Guide: tweak to database integration. --- guide/src/qs_14.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/guide/src/qs_14.md b/guide/src/qs_14.md index 0fe16f174..0d1998e4d 100644 --- a/guide/src/qs_14.md +++ b/guide/src/qs_14.md @@ -34,7 +34,7 @@ impl Message for CreateUser { } ``` -We can send a `CreateUser` message to the `DbExecutor` actor, and as a result, we receive a +We can send a `CreateUser` message to the `DbExecutor` actor, and as a result, we will receive a `User` model instance. Next, we must define the handler implementation for this message. ```rust,ignore @@ -98,8 +98,8 @@ fn main() { } ``` -Finally, we use the address in a request handler. We receive the message response -asynchronously, thus the handler returns a future object. +We will use the address in a request handler. The handle returns a future object; +thus, we receive the message response asynchronously. `Route::a()` must be used for async handler registration. From 18b706d4fb83490168cb9d6df422071c0739e189 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 6 Apr 2018 19:44:52 -0400 Subject: [PATCH 14/14] Guide: tweak to websocket and testing. --- guide/src/qs_8.md | 2 +- guide/src/qs_9.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/guide/src/qs_8.md b/guide/src/qs_8.md index 358d72ad0..f80fb8eb3 100644 --- a/guide/src/qs_8.md +++ b/guide/src/qs_8.md @@ -133,7 +133,7 @@ the websocket server and returns ws reader and writer objects. *TestServer* also provides an `execute()` method, which runs future objects to completion and returns result of the future computation. -The following example shows how to test a server websocket handler: +The following example demonstrates how to test a websocket handler: ```rust # extern crate actix; diff --git a/guide/src/qs_9.md b/guide/src/qs_9.md index b7fdc840a..e0d71f12b 100644 --- a/guide/src/qs_9.md +++ b/guide/src/qs_9.md @@ -6,7 +6,7 @@ a [*ws::WsStream*](../actix_web/ws/struct.WsStream.html) and then use stream combinators to handle actual messages, but it is simpler to handle websocket communications with an http actor. -The following is example of a simple websocket echo server: +The following is an example of a simple websocket echo server: ```rust # extern crate actix;