2017-12-02 08:06:15 +01:00
|
|
|
# Application
|
2017-11-28 21:44:59 +01:00
|
|
|
|
2018-04-06 01:43:17 +02:00
|
|
|
Actix web provides various primitives to build web servers and applications with Rust.
|
|
|
|
It provides routing, middlewares, pre-processing of requests, post-processing of responses,
|
2018-01-13 20:17:48 +01:00
|
|
|
websocket protocol handling, multipart streams, etc.
|
2017-11-28 21:44:59 +01:00
|
|
|
|
2018-03-31 09:16:55 +02:00
|
|
|
All actix web servers are built around the `App` instance.
|
2018-04-06 01:43:17 +02:00
|
|
|
It is used for registering routes for resources and middlewares.
|
|
|
|
It also stores application state shared across all handlers within same application.
|
2017-11-28 21:44:59 +01:00
|
|
|
|
2018-04-06 01:43:17 +02:00
|
|
|
Applications act as a namespace for all routes, i.e all routes for a specific application
|
2018-03-28 22:16:01 +02:00
|
|
|
have the same url path prefix. The application prefix always contains a leading "/" slash.
|
2018-04-06 01:43:17 +02:00
|
|
|
If a supplied prefix does not contain leading slash, it is automatically inserted.
|
|
|
|
The prefix should consist of value path segments.
|
|
|
|
|
|
|
|
> For an application with prefix `/app`,
|
|
|
|
> any request with the paths `/app`, `/app/`, or `/app/test` would match;
|
|
|
|
> however, the path `/application` would not match.
|
2017-11-28 21:44:59 +01:00
|
|
|
|
|
|
|
```rust,ignore
|
2017-12-04 22:32:05 +01:00
|
|
|
# extern crate actix_web;
|
|
|
|
# extern crate tokio_core;
|
2018-03-31 08:07:33 +02:00
|
|
|
# use actix_web::{*, http::Method};
|
2017-12-04 22:32:05 +01:00
|
|
|
# fn index(req: HttpRequest) -> &'static str {
|
|
|
|
# "Hello world!"
|
|
|
|
# }
|
|
|
|
# fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
let app = App::new()
|
2017-12-29 23:04:13 +01:00
|
|
|
.prefix("/app")
|
2017-12-04 23:07:53 +01:00
|
|
|
.resource("/index.html", |r| r.method(Method::GET).f(index))
|
2017-11-28 21:44:59 +01:00
|
|
|
.finish()
|
2017-12-04 22:32:05 +01:00
|
|
|
# }
|
2017-11-28 21:44:59 +01:00
|
|
|
```
|
|
|
|
|
2018-04-06 01:43:17 +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.
|
|
|
|
|
|
|
|
> For more information, check the
|
|
|
|
> [URL Dispatch](./qs_5.html#using-a-application-prefix-to-compose-applications) section.
|
2017-12-02 08:32:15 +01:00
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
Multiple applications can be served with one server:
|
2017-12-02 08:32:15 +01:00
|
|
|
|
|
|
|
```rust
|
2017-12-04 22:32:05 +01:00
|
|
|
# extern crate actix_web;
|
|
|
|
# extern crate tokio_core;
|
2017-12-11 23:16:29 +01:00
|
|
|
# use tokio_core::net::TcpStream;
|
|
|
|
# use std::net::SocketAddr;
|
2018-04-06 17:40:11 +02:00
|
|
|
use actix_web::{server, App, HttpResponse};
|
2017-12-02 08:32:15 +01:00
|
|
|
|
|
|
|
fn main() {
|
2018-04-06 17:40:11 +02:00
|
|
|
server::new(|| vec![
|
2018-03-31 09:16:55 +02:00
|
|
|
App::new()
|
2017-12-11 23:16:29 +01:00
|
|
|
.prefix("/app1")
|
2018-03-31 08:07:33 +02:00
|
|
|
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
2018-03-31 09:16:55 +02:00
|
|
|
App::new()
|
2017-12-11 23:16:29 +01:00
|
|
|
.prefix("/app2")
|
2018-03-31 08:07:33 +02:00
|
|
|
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
2018-03-31 09:16:55 +02:00
|
|
|
App::new()
|
2018-03-31 08:07:33 +02:00
|
|
|
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
2017-12-02 08:32:15 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 01:43:17 +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.
|
2017-12-14 06:38:47 +01:00
|
|
|
|
|
|
|
## State
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
Application state is shared with all routes and resources within the same application.
|
2018-04-06 01:43:17 +02:00
|
|
|
When using an http actor,state can be accessed with the `HttpRequest::state()` as read-only,
|
|
|
|
but interior mutability with `RefCell` can be used to achieve state mutability.
|
2018-03-28 22:16:01 +02:00
|
|
|
State is also available for route matching predicates and middlewares.
|
2017-12-14 06:38:47 +01:00
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
Let's write a simple application that uses shared state. We are going to store request count
|
2017-12-27 04:59:41 +01:00
|
|
|
in the state:
|
|
|
|
|
2017-12-14 06:38:47 +01:00
|
|
|
```rust
|
|
|
|
# extern crate actix;
|
|
|
|
# extern crate actix_web;
|
2017-12-27 04:59:41 +01:00
|
|
|
#
|
2017-12-14 06:38:47 +01:00
|
|
|
use std::cell::Cell;
|
2018-03-31 09:16:55 +02:00
|
|
|
use actix_web::{App, HttpRequest, http};
|
2017-12-14 06:38:47 +01:00
|
|
|
|
|
|
|
// This struct represents state
|
|
|
|
struct AppState {
|
|
|
|
counter: Cell<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(req: HttpRequest<AppState>) -> String {
|
|
|
|
let count = req.state().counter.get() + 1; // <- get count
|
|
|
|
req.state().counter.set(count); // <- store new count in state
|
|
|
|
|
|
|
|
format!("Request number: {}", count) // <- response with count
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
App::with_state(AppState{counter: Cell::new(0)})
|
2018-03-31 02:31:18 +02:00
|
|
|
.resource("/", |r| r.method(http::Method::GET).f(index))
|
2017-12-14 06:38:47 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 01:43:17 +02:00
|
|
|
> **Note**: http server accepts an application factory rather than an application
|
|
|
|
> instance. Http server constructs an application instance for each thread, thus application state
|
|
|
|
> must be constructed multiple times. If you want to share state between different threads, a
|
|
|
|
> shared object should be used, e.g. `Arc`. Application state does not need to be `Send` and `Sync`,
|
|
|
|
> but the application factory must be `Send` + `Sync`.
|