1
0
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:
Cameron Dershem
2019-06-15 16:37:08 -04:00
parent ebc6a44650
commit f7b22dfdc0
13 changed files with 238 additions and 184 deletions

View File

@ -0,0 +1,8 @@
[package]
name = "either"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "1.0"
futures = "0.1"

View 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
}