mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
First pass at Handlers chapter.
This commit is contained in:
8
examples/either/Cargo.toml
Normal file
8
examples/either/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "either"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
futures = "0.1"
|
29
examples/either/src/main.rs
Normal file
29
examples/either/src/main.rs
Normal file
@ -0,0 +1,29 @@
|
||||
// <main>
|
||||
use actix_web::{web, App, Either, Error, HttpRequest, HttpResponse};
|
||||
use futures::future::{ok, Future};
|
||||
|
||||
type RegisterResult =
|
||||
Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>;
|
||||
|
||||
fn index(_req: HttpRequest) -> RegisterResult {
|
||||
if is_a_variant() {
|
||||
// <- choose variant A
|
||||
Either::A(HttpResponse::BadRequest().body("Bad data"))
|
||||
} else {
|
||||
Either::B(
|
||||
// <- variant B
|
||||
Box::new(ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(format!("Hello!")))),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new().route("/", web::get().to(index));
|
||||
}
|
||||
// </main>
|
||||
|
||||
fn is_a_variant() -> bool {
|
||||
true
|
||||
}
|
Reference in New Issue
Block a user