mirror of
https://github.com/actix/actix-website
synced 2025-02-02 04:09:06 +01:00
Updated app examples
This commit is contained in:
parent
e204566062
commit
93995e185f
@ -23,7 +23,7 @@ The prefix should consist of value path segments.
|
|||||||
> any request with the paths `/app`, `/app/`, or `/app/test` would match;
|
> any request with the paths `/app`, `/app/`, or `/app/test` would match;
|
||||||
> however, the path `/application` would not match.
|
> however, the path `/application` would not match.
|
||||||
|
|
||||||
{{< include-example example="application" section="setup" >}}
|
{{< include-example example="application" section="make_app" >}}
|
||||||
|
|
||||||
In this example, an application with the `/app` prefix and a `index.html` resource
|
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.
|
are created. This resource is available through the `/app/index.html` url.
|
||||||
@ -33,23 +33,7 @@ are created. This resource is available through the `/app/index.html` url.
|
|||||||
|
|
||||||
Multiple applications can be served with one server:
|
Multiple applications can be served with one server:
|
||||||
|
|
||||||
```rust
|
{{< include-example example="application" section="run_server" >}}
|
||||||
# extern crate actix_web;
|
|
||||||
use actix_web::{server, App, HttpResponse};
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
server::new(|| vec![
|
|
||||||
App::new()
|
|
||||||
.prefix("/app1")
|
|
||||||
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
|
||||||
App::new()
|
|
||||||
.prefix("/app2")
|
|
||||||
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
|
||||||
App::new()
|
|
||||||
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
All `/app1` requests route to the first application, `/app2` to the second, and all other to the third.
|
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
|
**Applications get matched based on registration order**. If an application with a more generic
|
||||||
@ -67,29 +51,11 @@ State is also available for route matching predicates and middlewares.
|
|||||||
Let's write a simple application that uses shared state. We are going to store request count
|
Let's write a simple application that uses shared state. We are going to store request count
|
||||||
in the state:
|
in the state:
|
||||||
|
|
||||||
```rust
|
{{< include-example example="application" file="state.rs" section="setup" >}}
|
||||||
# extern crate actix_web;
|
|
||||||
use std::cell::Cell;
|
|
||||||
use actix_web::{App, HttpRequest, http};
|
|
||||||
|
|
||||||
// This struct represents state
|
When the app is initialized it needs to be passed the initial state:
|
||||||
struct AppState {
|
|
||||||
counter: Cell<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(req: HttpRequest<AppState>) -> String {
|
{{< include-example example="application" file="state.rs" section="make_app" >}}
|
||||||
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() {
|
|
||||||
App::with_state(AppState{counter: Cell::new(0)})
|
|
||||||
.resource("/", |r| r.method(http::Method::GET).f(index))
|
|
||||||
.finish();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Note**: http server accepts an application factory rather than an application
|
> **Note**: http server accepts an application factory rather than an application
|
||||||
> instance. Http server constructs an application instance for each thread, thus application state
|
> instance. Http server constructs an application instance for each thread, thus application state
|
||||||
@ -105,30 +71,4 @@ Combining multiple applications with different state is possible as well.
|
|||||||
|
|
||||||
This limitation can easily be overcome with the [App::boxed](https://docs.rs/actix-web/*/actix_web/struct.App.html#method.boxed) method, which converts an App into a boxed trait object.
|
This limitation can easily be overcome with the [App::boxed](https://docs.rs/actix-web/*/actix_web/struct.App.html#method.boxed) method, which converts an App into a boxed trait object.
|
||||||
|
|
||||||
```rust
|
{{< include-example example="application" file="state.rs" section="combine" >}}
|
||||||
# use std::thread;
|
|
||||||
# extern crate actix_web;
|
|
||||||
use actix_web::{server, App, HttpResponse};
|
|
||||||
|
|
||||||
struct State1;
|
|
||||||
struct State2;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
# thread::spawn(|| {
|
|
||||||
server::new(|| {
|
|
||||||
vec![
|
|
||||||
App::with_state(State1)
|
|
||||||
.prefix("/app1")
|
|
||||||
.resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
|
||||||
.boxed(),
|
|
||||||
App::with_state(State2)
|
|
||||||
.prefix("/app2")
|
|
||||||
.resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
|
||||||
.boxed()
|
|
||||||
]
|
|
||||||
})
|
|
||||||
.bind("127.0.0.1:8080").unwrap()
|
|
||||||
.run()
|
|
||||||
# });
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
@ -1,15 +1,40 @@
|
|||||||
|
#![allow(unused)]
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
use actix_web::{App, Responder, HttpRequest, http::Method};
|
use actix_web::{App, Responder, HttpRequest, HttpResponse, server, http::Method};
|
||||||
|
|
||||||
// <setup>
|
mod state;
|
||||||
|
|
||||||
|
fn make_app() {
|
||||||
|
// <make_app>
|
||||||
fn index(req: HttpRequest) -> impl Responder {
|
fn index(req: HttpRequest) -> impl Responder {
|
||||||
"Hello world!"
|
"Hello world!"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
let app = App::new()
|
||||||
let app = App::new()
|
.prefix("/app")
|
||||||
.prefix("/app")
|
.resource("/index.html", |r| r.method(Method::GET).f(index))
|
||||||
.resource("/index.html", |r| r.method(Method::GET).f(index))
|
.finish()
|
||||||
.finish();
|
// </make_app>
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_server() {
|
||||||
|
// <run_server>
|
||||||
|
let server = server::new(|| vec![
|
||||||
|
App::new()
|
||||||
|
.prefix("/app1")
|
||||||
|
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
||||||
|
App::new()
|
||||||
|
.prefix("/app2")
|
||||||
|
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
||||||
|
App::new()
|
||||||
|
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
||||||
|
]);
|
||||||
|
// </run_server>
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
make_app();
|
||||||
|
run_server();
|
||||||
|
state::test();
|
||||||
}
|
}
|
||||||
// </setup>
|
|
||||||
|
62
examples/application/src/state.rs
Normal file
62
examples/application/src/state.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// <setup>
|
||||||
|
use std::cell::Cell;
|
||||||
|
use actix_web::{App, HttpRequest, http};
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
// </setup>
|
||||||
|
|
||||||
|
fn make_app() {
|
||||||
|
|
||||||
|
// <make_app>
|
||||||
|
App::with_state(AppState { counter: Cell::new(0) })
|
||||||
|
.resource("/", |r| r.method(http::Method::GET).f(index))
|
||||||
|
.finish()
|
||||||
|
// </make_app>
|
||||||
|
;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
use std::thread;
|
||||||
|
use actix_web::{server, HttpResponse};
|
||||||
|
|
||||||
|
fn combine() {
|
||||||
|
thread::spawn(|| {
|
||||||
|
// <combine>
|
||||||
|
struct State1;
|
||||||
|
struct State2;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
server::new(|| {
|
||||||
|
vec![
|
||||||
|
App::with_state(State1)
|
||||||
|
.prefix("/app1")
|
||||||
|
.resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||||
|
.boxed(),
|
||||||
|
App::with_state(State2)
|
||||||
|
.prefix("/app2")
|
||||||
|
.resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||||
|
.boxed()
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8080").unwrap()
|
||||||
|
.run()
|
||||||
|
}
|
||||||
|
// </combine>
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test() {
|
||||||
|
make_app();
|
||||||
|
combine();
|
||||||
|
}
|
@ -2,6 +2,6 @@
|
|||||||
{{$file := .Get "file" | default "main.rs"}}
|
{{$file := .Get "file" | default "main.rs"}}
|
||||||
{{$section := .Get "section"}}
|
{{$section := .Get "section"}}
|
||||||
{{$source := readFile (printf "examples/%s/src/%s" $example $file)}}
|
{{$source := readFile (printf "examples/%s/src/%s" $example $file)}}
|
||||||
{{$source := index (findRE (printf "(?ms)// <%s>$(.*?)// </%s>" $section $section) $source) 0 }}
|
{{$source := index (findRE (printf "(?ms)^// <%s>$(.*?)^// </%s>$" $section $section) $source) 0 }}
|
||||||
{{$source := trim (replaceRE "// <[^>]+>" "" $source) "\n" }}
|
{{$source := trim (replaceRE "(?m)^// <[^>]+>" "" $source) "\n" }}
|
||||||
{{- highlight $source "rust" "" -}}
|
{{- highlight $source "rust" "" -}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user