mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
Updated app examples
This commit is contained in:
@ -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};
|
||||
|
||||
// <setup>
|
||||
mod state;
|
||||
|
||||
fn make_app() {
|
||||
// <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()
|
||||
// </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();
|
||||
}
|
Reference in New Issue
Block a user