mirror of
https://github.com/actix/actix-website
synced 2025-01-22 16:15:56 +01:00
Minor wording recommendations (#184)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
32b299e14f
commit
d659d04649
@ -12,7 +12,7 @@ responses, etc.
|
||||
|
||||
All `actix-web` servers are built around the [`App`][app] instance. It is used for
|
||||
registering routes for resources and middlewares. It also stores application
|
||||
state shared across all handlers within same scope.
|
||||
state shared across all handlers within the same scope.
|
||||
|
||||
An application's [`scope`][scope] acts as a namespace for all routes, i.e. all routes for a
|
||||
specific application scope have the same url path prefix. The application prefix always
|
||||
@ -33,7 +33,7 @@ are created. This resource is available through the `/app/index.html` url.
|
||||
## State
|
||||
|
||||
Application state is shared with all routes and resources within the same scope. State
|
||||
can be accessed with the [`web::Data<T>`][data] extractor where `T` is type of state. State is
|
||||
can be accessed with the [`web::Data<T>`][data] extractor where `T` is the type of the state. State is
|
||||
also available for middlewares.
|
||||
|
||||
Let's write a simple application and store the application name in the state:
|
||||
@ -44,59 +44,59 @@ and pass in the state when initializing the App, and start the application:
|
||||
|
||||
{{< include-example example="application" file="state.rs" section="start_app" >}}
|
||||
|
||||
Any number of state types could be registered within application.
|
||||
Any number of state types could be registered within the application.
|
||||
|
||||
## Shared Mutable State
|
||||
|
||||
`HttpServer` accepts an application factory rather than an application instance.
|
||||
Http server constructs an application instance for each thread, thus application data must be
|
||||
An `HttpServer` constructs an application instance for each thread. Therefore, application data must be
|
||||
constructed multiple times. If you want to share data between different threads, a shareable
|
||||
object should be used, e.g. Send + Sync.
|
||||
object should be used, e.g. `Send` + `Sync`.
|
||||
|
||||
Internally, [`web::Data`][data] uses Arc. Thus, in order to avoid double Arc, we should create our Data before registering it using [`App::app_data()`][appdata].
|
||||
Internally, [`web::Data`][data] uses `Arc`. Thus, in order to avoid creating two `Arc`s, we should create our Data before registering it using [`App::app_data()`][appdata].
|
||||
|
||||
In the following example, we will write an application with mutable, shared state. First, we define our state and create our handler:
|
||||
|
||||
{{< include-example example="application" file="state.rs" section="setup_mutable" >}}
|
||||
|
||||
and register the data in an App:
|
||||
and register the data in an `App`:
|
||||
|
||||
{{< include-example example="application" file="state.rs" section="make_app_mutable" >}}
|
||||
|
||||
## Using an Application Scope to Compose Applications
|
||||
|
||||
The [`web::scope()`][webscope] method allows to set a specific application prefix. This scope represents
|
||||
The [`web::scope()`][webscope] method allows setting a resource group prefix. This scope represents
|
||||
a resource prefix that will be prepended to all resource patterns added by the resource
|
||||
configuration. This can be used to help mount a set of routes at a different location
|
||||
than the included callable's author intended while still maintaining the same resource names.
|
||||
than the original author intended while still maintaining the same resource names.
|
||||
|
||||
For example:
|
||||
|
||||
{{< include-example example="application" file="scope.rs" section="scope" >}}
|
||||
|
||||
In the above example, the *show_users* route will have an effective route pattern of
|
||||
*/users/show* instead of */show* because the application's scope argument will be prepended
|
||||
to the pattern. The route will then only match if the URL path is */users/show*,
|
||||
and when the [`HttpRequest.url_for()`][urlfor] function is called with the route name show_users,
|
||||
In the above example, the `show_users` route will have an effective route pattern of
|
||||
`/users/show` instead of `/show` because the application's scope argument will be prepended
|
||||
to the pattern. The route will then only match if the URL path is `/users/show`,
|
||||
and when the [`HttpRequest.url_for()`][urlfor] function is called with the route name `show_users`,
|
||||
it will generate a URL with that same path.
|
||||
|
||||
## Application guards and virtual hosting
|
||||
|
||||
You can think of a guard as a simple function that accepts a *request* object reference
|
||||
and returns *true* or *false*. Formally, a guard is any object that implements the
|
||||
[`Guard`][guardtrait] trait. Actix-web provides several guards, you can check
|
||||
[functions section][guardfuncs] of api docs.
|
||||
[`Guard`][guardtrait] trait. Actix-web provides several guards. You can check the
|
||||
[functions section][guardfuncs] of the API docs.
|
||||
|
||||
One of the provided guards is [`Header`][guardheader], it can be used as application's
|
||||
filter based on request's header information.
|
||||
One of the provided guards is [`Header`][guardheader]. It can be used as a
|
||||
filter based on request header information.
|
||||
|
||||
{{< include-example example="application" file="vh.rs" section="vh" >}}
|
||||
|
||||
# Configure
|
||||
|
||||
For simplicity and reusability both [`App`][appconfig] and [`web::Scope`][webscopeconfig] provide the `configure` method.
|
||||
This function is useful for moving parts of configuration to a different module or even
|
||||
library. For example, some of the resource's configuration could be moved to different
|
||||
This function is useful for moving parts of the configuration to a different module or even
|
||||
library. For example, some of the resource's configuration could be moved to a different
|
||||
module.
|
||||
|
||||
{{< include-example example="application" file="config.rs" section="config" >}}
|
||||
@ -108,7 +108,7 @@ The result of the above example would be:
|
||||
/app -> "app"
|
||||
/api/test -> "test"
|
||||
```
|
||||
Each [`ServiceConfig`][serviceconfig] can have it's own `data`, `routes`, and `services`.
|
||||
Each [`ServiceConfig`][serviceconfig] can have its own `data`, `routes`, and `services`.
|
||||
|
||||
[usingappprefix]: /docs/url-dispatch/index.html#using-an-application-prefix-to-compose-applications
|
||||
[stateexample]: https://github.com/actix/examples/blob/master/state/src/main.rs
|
||||
|
@ -52,9 +52,9 @@ accepts a function that should return an application factory.
|
||||
That's it! Now, compile and run the program with `cargo run`.
|
||||
Head over to `http://localhost:8088/` to see the results.
|
||||
|
||||
**Note**: You could notice `#[actix_rt::main]` attribute macro. This
|
||||
macro executes marked async function in actix runtime. Any async function
|
||||
could be marked and executed by this macro.
|
||||
**Note**: You may have noticed the `#[actix_rt::main]` attribute macro. This
|
||||
macro executes the associated async function within the actix runtime.
|
||||
Any async function could be marked and executed by this macro.
|
||||
|
||||
### Using Attribute Macros to Define Routes
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <config>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
// this function could be located in different module
|
||||
// this function could be located in a different module
|
||||
fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/test")
|
||||
@ -10,7 +10,7 @@ fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
);
|
||||
}
|
||||
|
||||
// this function could be located in different module
|
||||
// this function could be located in a different module
|
||||
fn config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/app")
|
||||
|
@ -37,6 +37,7 @@ async fn _main() -> std::io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
// move counter into the closure
|
||||
App::new()
|
||||
// Note: using app_data instead of data
|
||||
.app_data(counter.clone()) // <- register the created data
|
||||
.route("/", web::get().to(_index))
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user