mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
Handlers done-ish.
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
[package]
|
||||
name = "async-handlers"
|
||||
version = "0.7.0"
|
||||
version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "0.7"
|
||||
actix-web = "1.0"
|
||||
futures = "0.1"
|
||||
bytes = "0.4"
|
||||
|
@ -1,24 +1,28 @@
|
||||
fn is_error() -> bool {
|
||||
true
|
||||
false
|
||||
}
|
||||
|
||||
// <main>
|
||||
use actix_web::{error, App, Error, HttpRequest, HttpResponse};
|
||||
// <async-stream>
|
||||
use actix_web::{error, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use futures::future::{result, Future};
|
||||
|
||||
fn index(
|
||||
_req: &HttpRequest,
|
||||
_req: HttpRequest,
|
||||
) -> Result<Box<Future<Item = HttpResponse, Error = Error>>, Error> {
|
||||
if is_error() {
|
||||
Err(error::ErrorBadRequest("bad request"))
|
||||
} else {
|
||||
Ok(Box::new(result(Ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(format!("Hello!"))))))
|
||||
.body("Hello!")))))
|
||||
}
|
||||
}
|
||||
// </main>
|
||||
// </async-stream>
|
||||
|
||||
pub fn main() {
|
||||
App::new().resource("/", |r| r.route().f(index)).finish();
|
||||
HttpServer::new(|| App::new().route("/", web::to_async(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -1,25 +1,22 @@
|
||||
mod async_stream;
|
||||
mod stream;
|
||||
// <main>
|
||||
use actix_web::{App, AsyncResponder, Error, HttpRequest, HttpResponse};
|
||||
use futures::future::{result, Future};
|
||||
pub mod async_stream;
|
||||
pub mod stream;
|
||||
// <async-responder>
|
||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse};
|
||||
use futures::future::{ok, Future};
|
||||
|
||||
fn index(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||
result(Ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(format!("Hello!"))))
|
||||
.responder()
|
||||
fn index(_req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
||||
Box::new(ok::<_, Error>(
|
||||
HttpResponse::Ok().content_type("text/html").body("Hello!"),
|
||||
))
|
||||
}
|
||||
|
||||
fn index2(_req: &HttpRequest) -> Box<Future<Item = &'static str, Error = Error>> {
|
||||
result(Ok("Welcome!")).responder()
|
||||
fn index2(_req: HttpRequest) -> Box<Future<Item = &'static str, Error = Error>> {
|
||||
Box::new(ok::<_, Error>("Welcome!"))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.resource("/async", |r| r.route().a(index))
|
||||
.resource("/", |r| r.route().a(index2))
|
||||
// .resource("/", |r| r.route().f(async_stream::index))
|
||||
.finish();
|
||||
.route("/async", web::to_async(index))
|
||||
.route("/", web::to_async(index2));
|
||||
}
|
||||
// </main>
|
||||
// </async-responder>
|
||||
|
@ -1,17 +1,21 @@
|
||||
// <main>
|
||||
use actix_web::{App, Body, HttpRequest, HttpResponse};
|
||||
// <stream>
|
||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use bytes::Bytes;
|
||||
use futures::stream::once;
|
||||
|
||||
fn index(_req: &HttpRequest) -> HttpResponse {
|
||||
let body = once(Ok(Bytes::from_static(b"test")));
|
||||
fn index(_req: HttpRequest) -> HttpResponse {
|
||||
let body = once::<Bytes, Error>(Ok(Bytes::from_static(b"test")));
|
||||
|
||||
HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(Body::Streaming(Box::new(body)))
|
||||
.streaming(Box::new(body))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().resource("/async", |r| r.f(index)).finish();
|
||||
HttpServer::new(|| App::new().route("/async", web::to_async(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </main>
|
||||
// </stream>
|
||||
|
Reference in New Issue
Block a user