mirror of
https://github.com/tokio-rs/axum.git
synced 2025-10-02 15:24:54 +00:00
Add Html
response type
This commit is contained in:
parent
0b2f791bf4
commit
6822766165
33
examples/hello_world.rs
Normal file
33
examples/hello_world.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use http::Request;
|
||||||
|
use hyper::Server;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use tower::{make::Shared, ServiceBuilder};
|
||||||
|
use tower_http::trace::TraceLayer;
|
||||||
|
use tower_web::{body::Body, response::Html, Error};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
// build our application with some routes
|
||||||
|
let app = tower_web::app()
|
||||||
|
.at("/")
|
||||||
|
.get(handler)
|
||||||
|
// convert it into a `Service`
|
||||||
|
.into_service();
|
||||||
|
|
||||||
|
// add some middleware
|
||||||
|
let app = ServiceBuilder::new()
|
||||||
|
.layer(TraceLayer::new_for_http())
|
||||||
|
.service(app);
|
||||||
|
|
||||||
|
// run it with hyper
|
||||||
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
|
tracing::debug!("listening on {}", addr);
|
||||||
|
let server = Server::bind(&addr).serve(Shared::new(app));
|
||||||
|
server.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handler(_req: Request<Body>) -> Result<Html<&'static str>, Error> {
|
||||||
|
Ok(Html("<h1>Hello, World!</h1>"))
|
||||||
|
}
|
@ -2,7 +2,6 @@ use crate::{body::Body, Error};
|
|||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::{future, ready};
|
use futures_util::{future, ready};
|
||||||
use http::Request;
|
use http::Request;
|
||||||
use http_body::Body as _;
|
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::{
|
use std::{
|
||||||
|
22
src/lib.rs
22
src/lib.rs
@ -1,5 +1,3 @@
|
|||||||
#![allow(unused_imports, dead_code)]
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
Improvements to make:
|
Improvements to make:
|
||||||
@ -13,27 +11,19 @@ Tests
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
body::{Body, BoxBody},
|
body::Body,
|
||||||
extract::FromRequest,
|
|
||||||
handler::{Handler, HandlerSvc},
|
|
||||||
response::IntoResponse,
|
|
||||||
routing::{EmptyRouter, RouteAt},
|
routing::{EmptyRouter, RouteAt},
|
||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::{future, ready};
|
use futures_util::ready;
|
||||||
use http::{header, HeaderValue, Method, Request, Response, StatusCode};
|
use http::Response;
|
||||||
use http_body::Body as _;
|
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
||||||
use std::{
|
use std::{
|
||||||
convert::Infallible,
|
|
||||||
future::Future,
|
future::Future,
|
||||||
marker::PhantomData,
|
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
use tower::{BoxError, Layer, Service, ServiceExt};
|
use tower::Service;
|
||||||
|
|
||||||
pub mod body;
|
pub mod body;
|
||||||
pub mod extract;
|
pub mod extract;
|
||||||
@ -154,11 +144,15 @@ where
|
|||||||
mod tests {
|
mod tests {
|
||||||
#![allow(warnings)]
|
#![allow(warnings)]
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::handler::Handler;
|
||||||
|
use http::{Method, Request, StatusCode};
|
||||||
use hyper::Server;
|
use hyper::Server;
|
||||||
|
use serde::Deserialize;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{fmt, net::SocketAddr, sync::Arc};
|
use std::{fmt, net::SocketAddr, sync::Arc};
|
||||||
use tower::{
|
use tower::{
|
||||||
layer::util::Identity, make::Shared, service_fn, timeout::TimeoutLayer, ServiceBuilder,
|
layer::util::Identity, make::Shared, service_fn, timeout::TimeoutLayer, ServiceBuilder,
|
||||||
|
ServiceExt,
|
||||||
};
|
};
|
||||||
use tower_http::{
|
use tower_http::{
|
||||||
add_extension::AddExtensionLayer,
|
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)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct Empty;
|
pub struct Empty;
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ use std::{
|
|||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
use tower::{BoxError, Layer, Service};
|
use tower::{BoxError, Service};
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct EmptyRouter(pub(crate) ());
|
pub struct EmptyRouter(pub(crate) ());
|
||||||
@ -398,12 +398,4 @@ mod tests {
|
|||||||
std::str::from_utf8(&route.spec).unwrap(),
|
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user