1
0
mirror of https://github.com/actix/actix-website synced 2024-11-24 00:41:07 +01:00

Merge pull request #23 from yanns/start_app_with_state

document how to start the app with state
This commit is contained in:
Nikolay Kim 2018-06-08 08:26:23 -07:00 committed by GitHub
commit 11d41fab8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -62,6 +62,10 @@ When the app is initialized it needs to be passed the initial 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`.
>
> To start the previous app, create it into a closure:
{{< include-example example="application" file="state.rs" section="start_app" >}}
## Combining applications with different state

View File

@ -24,6 +24,18 @@ App::with_state(AppState { counter: Cell::new(0) })
;
}
fn start_app() {
// <start_app>
server::new(|| {
App::with_state(AppState { counter: Cell::new(0) })
.resource("/", |r| r.method(http::Method::GET).f(index))
}).bind("127.0.0.1:8080")
.unwrap()
.run()
// </start_app>
;
}
use actix_web::{server, HttpResponse};
use std::thread;