2018-05-22 23:15:08 +02:00
|
|
|
---
|
|
|
|
title: Application
|
|
|
|
menu: docs_basics
|
|
|
|
weight: 140
|
|
|
|
---
|
|
|
|
|
|
|
|
# Writing an Application
|
|
|
|
|
|
|
|
`actix-web` provides various primitives to build web servers and applications with Rust.
|
2019-06-18 22:52:51 +02:00
|
|
|
It provides routing, middlewares, pre-processing of requests, post-processing of
|
|
|
|
responses, etc.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
All `actix-web` servers are built around the `App` instance. It is used for
|
2018-05-22 23:15:08 +02:00
|
|
|
registering routes for resources and middlewares. It also stores application
|
2019-06-20 00:00:31 +02:00
|
|
|
state shared across all handlers within same scope.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
An application's `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
|
|
|
|
contains a leading "/" slash. If a supplied prefix does not contain leading slash,
|
|
|
|
it is automatically inserted. The prefix should consist of value path segments.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
> For an application with scope `/app`,
|
2018-05-22 23:15:08 +02:00
|
|
|
> any request with the paths `/app`, `/app/`, or `/app/test` would match;
|
|
|
|
> however, the path `/application` would not match.
|
|
|
|
|
2019-06-19 06:20:50 +02:00
|
|
|
{{< include-example example="application" file="app.rs" section="setup" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
In this example, an application with the `/app` prefix and a `index.html` resource
|
|
|
|
are created. This resource is available through the `/app/index.html` url.
|
|
|
|
|
2019-06-25 05:36:32 +02:00
|
|
|
> For more information, check the [URL Dispatch][usingappprefix] section.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
Multiple application scopes can be served with one server:
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
{{< include-example example="application" file="main.rs" section="multi" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
All `/app1` requests route to the first application, `/app2` to the second, and all other to the third.
|
|
|
|
**Applications get matched based on registration order**. If an application with a more generic
|
|
|
|
prefix is registered before a less generic one, it would effectively block the less generic
|
|
|
|
application matching. For example, if an `App` with the prefix `"/"` was registered
|
|
|
|
as the first application, it would match all incoming requests.
|
|
|
|
|
|
|
|
## State
|
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
Application state is shared with all routes and resources within the same scope. State
|
2019-07-15 11:35:50 +02:00
|
|
|
can be accessed with the `web::Data<State>` extractor. State is also available for route matching guards and middlewares.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-07-15 11:35:50 +02:00
|
|
|
Let's write a simple application and store the application name in the state:
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2018-05-23 22:01:33 +02:00
|
|
|
{{< include-example example="application" file="state.rs" section="setup" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-07-15 11:35:50 +02:00
|
|
|
and pass in the state when initializing the App, and start the application:
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-07-15 11:35:50 +02:00
|
|
|
{{< include-example example="application" file="state.rs" section="start_app" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-07-15 11:35:50 +02:00
|
|
|
## Shared Mutable State
|
2019-02-26 04:45:46 +01:00
|
|
|
|
2019-07-15 11:35:50 +02:00
|
|
|
`HttpServer` accepts an application factory rather than an application instance.
|
|
|
|
Http server constructs an application instance for each thread, thus 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.
|
2018-06-08 10:17:29 +02:00
|
|
|
|
2019-07-15 11:35:50 +02:00
|
|
|
Internally, `web::Data` uses Arc. Thus, in order to avoid double Arc, we should create our Data before registering it using `register_data()`.
|
|
|
|
|
|
|
|
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" >}}
|
|
|
|
|
2019-07-15 12:12:37 +02:00
|
|
|
and register the data in an App:
|
2019-07-15 11:35:50 +02:00
|
|
|
|
|
|
|
{{< include-example example="application" file="state.rs" section="make_app_mutable" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
## Combining applications with different state
|
|
|
|
|
|
|
|
Combining multiple applications with different state is possible as well.
|
|
|
|
|
2019-06-19 06:20:50 +02:00
|
|
|
{{< include-example example="application" file="combine.rs" section="combine" >}}
|
2018-06-08 06:08:11 +02:00
|
|
|
|
2019-06-13 09:24:25 +02:00
|
|
|
## Using an Application Scope to Compose Applications
|
2018-06-08 06:08:11 +02:00
|
|
|
|
2019-06-25 05:36:32 +02:00
|
|
|
The `web::scope()` method allows to set a specific application 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.
|
2018-06-08 06:08:11 +02:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-06-20 00:00:31 +02:00
|
|
|
{{< include-example example="application" file="scope.rs" section="scope" >}}
|
2018-06-08 06:08:11 +02:00
|
|
|
|
|
|
|
In the above example, the *show_users* route will have an effective route pattern of
|
2019-06-13 09:24:25 +02:00
|
|
|
*/users/show* instead of */show* because the application's scope argument will be prepended
|
2018-06-08 06:08:11 +02:00
|
|
|
to the pattern. The route will then only match if the URL path is */users/show*,
|
|
|
|
and when the `HttpRequest.url_for()` function is called with the route name show_users,
|
|
|
|
it will generate a URL with that same path.
|
|
|
|
|
2019-06-13 09:24:25 +02:00
|
|
|
## Application guards and virtual hosting
|
2018-06-08 06:08:11 +02:00
|
|
|
|
2019-06-13 09:24:25 +02:00
|
|
|
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
|
2019-06-25 05:36:32 +02:00
|
|
|
[`Guard`][guardtrait] trait. Actix-web provides several guards, you can check
|
|
|
|
[functions section][guardfuncs] of api docs.
|
2018-06-08 06:08:11 +02:00
|
|
|
|
2019-06-25 05:36:32 +02:00
|
|
|
One of the provided guards is [`Header`][guardheader], it can be used as application's
|
|
|
|
filter based on request's header information.
|
2018-06-08 06:08:11 +02:00
|
|
|
|
|
|
|
{{< include-example example="application" file="vh.rs" section="vh" >}}
|
2019-06-20 00:00:31 +02:00
|
|
|
|
|
|
|
# Configure
|
|
|
|
|
|
|
|
For simplicity and reusability both `App` and `web::scope` 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
|
|
|
|
module.
|
|
|
|
|
|
|
|
{{< include-example example="application" file="config.rs" section="config" >}}
|
|
|
|
|
|
|
|
The result of the above example would be:
|
|
|
|
|
|
|
|
```
|
|
|
|
/ -> "/"
|
|
|
|
/app -> "app"
|
|
|
|
/api/test -> "test"
|
|
|
|
```
|
|
|
|
|
|
|
|
Each `ServiceConfig` can have it's own `data`, `routes`, and `services`
|
2019-06-25 05:36:32 +02:00
|
|
|
|
|
|
|
[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
|
|
|
|
[guardtrait]: https://docs.rs/actix-web/1.0.2/actix_web/guard/trait.Guard.html
|
|
|
|
[guardfuncs]: https://docs.rs/actix-web/1.0.2/actix_web/guard/index.html#functions
|
|
|
|
[guardheader]: ((https://docs.rs/actix-web/1.0.2/actix_web/guard/fn.Header.html
|