2018-05-23 22:01:33 +02:00
|
|
|
// <setup>
|
2018-05-24 05:39:15 +02:00
|
|
|
use actix_web::{http, App, HttpRequest};
|
2018-05-23 22:01:33 +02:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
// This struct represents state
|
|
|
|
struct AppState {
|
|
|
|
counter: Cell<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(req: HttpRequest<AppState>) -> String {
|
|
|
|
let count = req.state().counter.get() + 1; // <- get count
|
2018-05-24 05:39:15 +02:00
|
|
|
req.state().counter.set(count); // <- store new count in state
|
2018-05-23 22:01:33 +02:00
|
|
|
|
2018-05-24 05:39:15 +02:00
|
|
|
format!("Request number: {}", count) // <- response with count
|
2018-05-23 22:01:33 +02:00
|
|
|
}
|
|
|
|
// </setup>
|
|
|
|
|
|
|
|
fn make_app() {
|
2018-05-24 18:31:40 +02:00
|
|
|
// <make_app>
|
|
|
|
App::with_state(AppState { counter: Cell::new(0) })
|
2018-05-23 22:01:33 +02:00
|
|
|
.resource("/", |r| r.method(http::Method::GET).f(index))
|
|
|
|
.finish()
|
|
|
|
// </make_app>
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
use actix_web::{server, HttpResponse};
|
2018-05-24 05:39:15 +02:00
|
|
|
use std::thread;
|
2018-05-23 22:01:33 +02:00
|
|
|
|
|
|
|
fn combine() {
|
2018-05-24 05:39:15 +02:00
|
|
|
thread::spawn(|| {
|
2018-05-24 18:31:40 +02:00
|
|
|
// <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(),
|
2018-05-24 05:39:15 +02:00
|
|
|
]
|
2018-05-24 18:31:40 +02:00
|
|
|
}).bind("127.0.0.1:8080")
|
|
|
|
.unwrap()
|
|
|
|
.run()
|
|
|
|
}
|
|
|
|
// </combine>
|
2018-05-24 05:39:15 +02:00
|
|
|
});
|
2018-05-23 22:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test() {
|
|
|
|
make_app();
|
|
|
|
combine();
|
|
|
|
}
|