From 14e47859a09400552b91c14789915d37571daa56 Mon Sep 17 00:00:00 2001 From: Yann Simon Date: Fri, 8 Jun 2018 10:17:29 +0200 Subject: [PATCH] document how to start the app with state --- content/docs/application.md | 4 ++++ examples/application/src/state.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/content/docs/application.md b/content/docs/application.md index 9f01dfc..b8224e9 100644 --- a/content/docs/application.md +++ b/content/docs/application.md @@ -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 diff --git a/examples/application/src/state.rs b/examples/application/src/state.rs index e8c81a8..4462953 100644 --- a/examples/application/src/state.rs +++ b/examples/application/src/state.rs @@ -24,6 +24,18 @@ App::with_state(AppState { counter: Cell::new(0) }) ; } +fn 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() +// +; +} + use actix_web::{server, HttpResponse}; use std::thread;