mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-07 00:00:12 +02:00
Add Html response type
This commit is contained in:
@@ -2,7 +2,6 @@ use crate::{body::Body, Error};
|
||||
use bytes::Bytes;
|
||||
use futures_util::{future, ready};
|
||||
use http::Request;
|
||||
use http_body::Body as _;
|
||||
use pin_project::pin_project;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::{
|
||||
|
||||
+8
-14
@@ -1,5 +1,3 @@
|
||||
#![allow(unused_imports, dead_code)]
|
||||
|
||||
/*
|
||||
|
||||
Improvements to make:
|
||||
@@ -13,27 +11,19 @@ Tests
|
||||
*/
|
||||
|
||||
use self::{
|
||||
body::{Body, BoxBody},
|
||||
extract::FromRequest,
|
||||
handler::{Handler, HandlerSvc},
|
||||
response::IntoResponse,
|
||||
body::Body,
|
||||
routing::{EmptyRouter, RouteAt},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use futures_util::{future, ready};
|
||||
use http::{header, HeaderValue, Method, Request, Response, StatusCode};
|
||||
use http_body::Body as _;
|
||||
use futures_util::ready;
|
||||
use http::Response;
|
||||
use pin_project::pin_project;
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use std::{
|
||||
convert::Infallible,
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tower::{BoxError, Layer, Service, ServiceExt};
|
||||
use tower::Service;
|
||||
|
||||
pub mod body;
|
||||
pub mod extract;
|
||||
@@ -154,11 +144,15 @@ where
|
||||
mod tests {
|
||||
#![allow(warnings)]
|
||||
use super::*;
|
||||
use crate::handler::Handler;
|
||||
use http::{Method, Request, StatusCode};
|
||||
use hyper::Server;
|
||||
use serde::Deserialize;
|
||||
use std::time::Duration;
|
||||
use std::{fmt, net::SocketAddr, sync::Arc};
|
||||
use tower::{
|
||||
layer::util::Identity, make::Shared, service_fn, timeout::TimeoutLayer, ServiceBuilder,
|
||||
ServiceExt,
|
||||
};
|
||||
use tower_http::{
|
||||
add_extension::AddExtensionLayer,
|
||||
|
||||
@@ -92,6 +92,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Html<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse<Body> for Html<T>
|
||||
where
|
||||
T: Into<Bytes>,
|
||||
{
|
||||
fn into_response(self) -> Result<Response<Body>, Error> {
|
||||
let bytes = self.0.into();
|
||||
let mut res = Response::new(Body::from(bytes));
|
||||
res.headers_mut()
|
||||
.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html"));
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Empty;
|
||||
|
||||
|
||||
+1
-9
@@ -14,7 +14,7 @@ use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tower::{BoxError, Layer, Service};
|
||||
use tower::{BoxError, Service};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct EmptyRouter(pub(crate) ());
|
||||
@@ -398,12 +398,4 @@ mod tests {
|
||||
std::str::from_utf8(&route.spec).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
fn route(method: Method, uri: &'static str) -> RouteSpec {
|
||||
RouteSpec::new(method, uri)
|
||||
}
|
||||
|
||||
fn req(method: Method, uri: &str) -> Request<()> {
|
||||
Request::builder().uri(uri).method(method).body(()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user