From 93995e185f89294fe39ce59ffbb46fe95480aeca Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 23 May 2018 22:01:33 +0200 Subject: [PATCH] Updated app examples --- content/docs/application.md | 72 +++---------------------- examples/application/src/main.rs | 41 +++++++++++--- examples/application/src/state.rs | 62 +++++++++++++++++++++ layouts/shortcodes/include-example.html | 4 +- 4 files changed, 103 insertions(+), 76 deletions(-) create mode 100644 examples/application/src/state.rs diff --git a/content/docs/application.md b/content/docs/application.md index 8da3203..74be754 100644 --- a/content/docs/application.md +++ b/content/docs/application.md @@ -23,7 +23,7 @@ The prefix should consist of value path segments. > any request with the paths `/app`, `/app/`, or `/app/test` would 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 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: -```rust -# 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())), - ]); -} -``` +{{< include-example example="application" section="run_server" >}} 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 @@ -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 in the state: -```rust -# extern crate actix_web; -use std::cell::Cell; -use actix_web::{App, HttpRequest, http}; +{{< include-example example="application" file="state.rs" section="setup" >}} -// This struct represents state -struct AppState { - counter: Cell, -} +When the app is initialized it needs to be passed the initial state: -fn index(req: HttpRequest) -> 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() { - App::with_state(AppState{counter: Cell::new(0)}) - .resource("/", |r| r.method(http::Method::GET).f(index)) - .finish(); -} -``` +{{< include-example example="application" file="state.rs" section="make_app" >}} > **Note**: http server accepts an application factory rather than an application > 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. -```rust -# 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() -# }); -} -``` +{{< include-example example="application" file="state.rs" section="combine" >}} diff --git a/examples/application/src/main.rs b/examples/application/src/main.rs index 65442a9..96808a2 100644 --- a/examples/application/src/main.rs +++ b/examples/application/src/main.rs @@ -1,15 +1,40 @@ +#![allow(unused)] extern crate actix_web; -use actix_web::{App, Responder, HttpRequest, http::Method}; +use actix_web::{App, Responder, HttpRequest, HttpResponse, server, http::Method}; -// +mod state; + +fn make_app() { +// fn index(req: HttpRequest) -> impl Responder { "Hello world!" } -fn main() { - let app = App::new() - .prefix("/app") - .resource("/index.html", |r| r.method(Method::GET).f(index)) - .finish(); +let app = App::new() + .prefix("/app") + .resource("/index.html", |r| r.method(Method::GET).f(index)) + .finish() +// +; +} + +fn 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())), +]); +// +} + +fn main() { + make_app(); + run_server(); + state::test(); } -// diff --git a/examples/application/src/state.rs b/examples/application/src/state.rs new file mode 100644 index 0000000..84a5f98 --- /dev/null +++ b/examples/application/src/state.rs @@ -0,0 +1,62 @@ +// +use std::cell::Cell; +use actix_web::{App, HttpRequest, http}; + +// This struct represents state +struct AppState { + counter: Cell, +} + +fn index(req: HttpRequest) -> 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 make_app() { + +// +App::with_state(AppState { counter: Cell::new(0) }) + .resource("/", |r| r.method(http::Method::GET).f(index)) + .finish() +// +; + +} + + +use std::thread; +use actix_web::{server, HttpResponse}; + +fn combine() { +thread::spawn(|| { +// +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() +} +// +}); +} + +pub fn test() { + make_app(); + combine(); +} diff --git a/layouts/shortcodes/include-example.html b/layouts/shortcodes/include-example.html index ca8b29d..a7482db 100644 --- a/layouts/shortcodes/include-example.html +++ b/layouts/shortcodes/include-example.html @@ -2,6 +2,6 @@ {{$file := .Get "file" | default "main.rs"}} {{$section := .Get "section"}} {{$source := readFile (printf "examples/%s/src/%s" $example $file)}} -{{$source := index (findRE (printf "(?ms)// <%s>$(.*?)// " $section $section) $source) 0 }} -{{$source := trim (replaceRE "// <[^>]+>" "" $source) "\n" }} +{{$source := index (findRE (printf "(?ms)^// <%s>$(.*?)^// $" $section $section) $source) 0 }} +{{$source := trim (replaceRE "(?m)^// <[^>]+>" "" $source) "\n" }} {{- highlight $source "rust" "" -}}