mirror of
https://github.com/actix/actix-website
synced 2025-01-22 16:15:56 +01:00
parent
c39325a649
commit
cdbb5dd2b2
@ -80,7 +80,7 @@ Actix-web also provides several other extractors:
|
||||
need access to the request.
|
||||
* *String* - You can convert a request's payload to a *String*. [*Example*][stringexample]
|
||||
is available in doc strings.
|
||||
* *bytes::Bytes* - You can convert a request's payload into *Bytes*.
|
||||
* *actix_web::web::Bytes* - You can convert a request's payload into *Bytes*.
|
||||
[*Example*][bytesexample]
|
||||
is available in doc strings.
|
||||
* *Payload* - You can access a request's payload.
|
||||
|
@ -38,7 +38,7 @@ complex types are involved.
|
||||
|
||||
```rust
|
||||
async fn index(_req: HttpRequest) -> impl Responder {
|
||||
Bytes::from_static(b"Hello world!")
|
||||
web::Bytes::from_static(b"Hello world!")
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1,12 +1,10 @@
|
||||
// <stream>
|
||||
use actix_web::{get, App, Error, HttpResponse, HttpServer};
|
||||
use bytes::Bytes;
|
||||
use futures::future::ok;
|
||||
use futures::stream::once;
|
||||
use actix_web::{get, web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::{future::ok, stream::once};
|
||||
|
||||
#[get("/stream")]
|
||||
async fn stream() -> HttpResponse {
|
||||
let body = once(ok::<_, Error>(Bytes::from_static(b"test")));
|
||||
let body = once(ok::<_, Error>(web::Bytes::from_static(b"test")));
|
||||
|
||||
HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <logging>
|
||||
use actix_web::{error, get, middleware::Logger, App, HttpServer, Result};
|
||||
use log::debug;
|
||||
use derive_more::{Display, Error};
|
||||
use log::debug;
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "my error: {}", name)]
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <json-two>
|
||||
use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web::{error, web, App, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -2,7 +2,6 @@
|
||||
use actix_web::{error, post, web, App, Error, HttpResponse};
|
||||
use futures::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct MyObj {
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <responder-trait>
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, Responder};
|
||||
use serde::Serialize;
|
||||
use futures::future::{ready, Ready};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MyObj {
|
||||
|
@ -1,12 +1,11 @@
|
||||
// <chunked>
|
||||
use actix_web::{get, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use bytes::Bytes;
|
||||
use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use futures::future::ok;
|
||||
use futures::stream::once;
|
||||
|
||||
#[get("/")]
|
||||
async fn index(_req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok().streaming(once(ok::<_, Error>(Bytes::from_static(b"data"))))
|
||||
HttpResponse::Ok().streaming(once(ok::<_, Error>(web::Bytes::from_static(b"data"))))
|
||||
}
|
||||
// </chunked>
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <signals>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, rt::System};
|
||||
use actix_web::{rt::System, web, App, HttpResponse, HttpServer};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
|
||||
|
@ -14,7 +14,8 @@ async fn main() -> std::io::Result<()> {
|
||||
// load ssl keys
|
||||
// to create a self-signed temporary cert for testing:
|
||||
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
|
||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
let mut builder =
|
||||
SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
builder
|
||||
.set_private_key_file("key.pem", SslFiletype::PEM)
|
||||
.unwrap();
|
||||
|
@ -23,7 +23,8 @@ mod tests {
|
||||
App::new()
|
||||
.data(AppState { count: 4 })
|
||||
.route("/", web::get().to(index)),
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
let req = test::TestRequest::get().uri("/").to_request();
|
||||
let resp: AppState = test::read_response_json(&mut app, req).await;
|
||||
|
||||
|
@ -1,22 +1,21 @@
|
||||
// <stream-response>
|
||||
use std::task::Poll;
|
||||
use bytes::Bytes;
|
||||
use futures::stream::poll_fn;
|
||||
use std::task::Poll;
|
||||
|
||||
use actix_web::http::{ContentEncoding, StatusCode};
|
||||
use actix_web::{web, http, App, Error, HttpRequest, HttpResponse};
|
||||
use actix_web::{http, web, App, Error, HttpRequest, HttpResponse};
|
||||
|
||||
async fn sse(_req: HttpRequest) -> HttpResponse {
|
||||
let mut counter: usize = 5;
|
||||
|
||||
// yields `data: N` where N in [5; 1]
|
||||
let server_events = poll_fn(move |_cx| -> Poll<Option<Result<Bytes, Error>>> {
|
||||
let server_events = poll_fn(move |_cx| -> Poll<Option<Result<web::Bytes, Error>>> {
|
||||
if counter == 0 {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
let payload = format!("data: {}\n\n", counter);
|
||||
counter -= 1;
|
||||
Poll::Ready(Some(Ok(Bytes::from(payload))))
|
||||
Poll::Ready(Some(Ok(web::Bytes::from(payload))))
|
||||
});
|
||||
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
@ -51,15 +50,24 @@ mod tests {
|
||||
|
||||
// first chunk
|
||||
let (bytes, mut resp) = resp.take_body().into_future().await;
|
||||
assert_eq!(bytes.unwrap().unwrap(), Bytes::from_static(b"data: 5\n\n"));
|
||||
assert_eq!(
|
||||
bytes.unwrap().unwrap(),
|
||||
web::Bytes::from_static(b"data: 5\n\n")
|
||||
);
|
||||
|
||||
// second chunk
|
||||
let (bytes, mut resp) = resp.take_body().into_future().await;
|
||||
assert_eq!(bytes.unwrap().unwrap(), Bytes::from_static(b"data: 4\n\n"));
|
||||
assert_eq!(
|
||||
bytes.unwrap().unwrap(),
|
||||
web::Bytes::from_static(b"data: 4\n\n")
|
||||
);
|
||||
|
||||
// remaining part
|
||||
let bytes = test::load_stream(resp.take_body().into_stream()).await;
|
||||
assert_eq!(bytes.unwrap(), Bytes::from_static(b"data: 3\n\ndata: 2\n\ndata: 1\n\n"));
|
||||
assert_eq!(
|
||||
bytes.unwrap(),
|
||||
web::Bytes::from_static(b"data: 3\n\ndata: 2\n\ndata: 1\n\n")
|
||||
);
|
||||
}
|
||||
}
|
||||
// </stream-response>
|
||||
|
Loading…
x
Reference in New Issue
Block a user