mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-03 07:45:14 +00:00
Use Template::MIME_TYPE instead of extension
This commit is contained in:
parent
a9aebf82fb
commit
332d741f21
@ -5,9 +5,9 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use actix_web::body::BoxBody;
|
use actix_web::body::BoxBody;
|
||||||
|
use actix_web::http::header::HeaderValue;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::{HttpResponse, HttpResponseBuilder, ResponseError};
|
use actix_web::{HttpResponse, HttpResponseBuilder, ResponseError};
|
||||||
use askama::mime::extension_to_mime_type;
|
|
||||||
pub use askama::*;
|
pub use askama::*;
|
||||||
|
|
||||||
/// Newtype to let askama::Error implement actix_web::ResponseError.
|
/// Newtype to let askama::Error implement actix_web::ResponseError.
|
||||||
@ -36,12 +36,9 @@ pub trait TemplateToResponse {
|
|||||||
impl<T: askama::Template> TemplateToResponse for T {
|
impl<T: askama::Template> TemplateToResponse for T {
|
||||||
fn to_response(&self) -> HttpResponse<BoxBody> {
|
fn to_response(&self) -> HttpResponse<BoxBody> {
|
||||||
match self.render() {
|
match self.render() {
|
||||||
Ok(buffer) => {
|
Ok(buffer) => HttpResponseBuilder::new(StatusCode::OK)
|
||||||
let ctype = extension_to_mime_type(T::EXTENSION.unwrap_or("txt"));
|
.content_type(HeaderValue::from_static(T::MIME_TYPE))
|
||||||
HttpResponseBuilder::new(StatusCode::OK)
|
.body(buffer),
|
||||||
.content_type(ctype)
|
|
||||||
.body(buffer)
|
|
||||||
}
|
|
||||||
Err(err) => HttpResponse::from_error(ActixError(err)),
|
Err(err) => HttpResponse::from_error(ActixError(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,13 @@ pub use http::Response;
|
|||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use http_body::{Empty, Full};
|
use http_body::{Empty, Full};
|
||||||
|
|
||||||
pub fn into_response<T: Template>(t: &T, ext: &str) -> Response<BoxBody> {
|
pub fn into_response<T: Template>(t: &T, _ext: &str) -> Response<BoxBody> {
|
||||||
match t.render() {
|
match t.render() {
|
||||||
Ok(body) => Response::builder()
|
Ok(body) => Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
.header(
|
.header(
|
||||||
"content-type",
|
http::header::CONTENT_TYPE,
|
||||||
askama::mime::extension_to_mime_type(ext).to_string(),
|
http::HeaderValue::from_static(T::MIME_TYPE),
|
||||||
)
|
)
|
||||||
.body(body::boxed(Full::from(body)))
|
.body(body::boxed(Full::from(body)))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
@ -8,13 +8,13 @@ pub use gotham::handler::IntoResponse;
|
|||||||
pub use gotham::state::State;
|
pub use gotham::state::State;
|
||||||
pub use hyper::{Body, Response, StatusCode};
|
pub use hyper::{Body, Response, StatusCode};
|
||||||
|
|
||||||
pub fn respond<T: Template>(t: &T, ext: &str) -> Response<Body> {
|
pub fn respond<T: Template>(t: &T, _ext: &str) -> Response<Body> {
|
||||||
match t.render() {
|
match t.render() {
|
||||||
Ok(body) => Response::builder()
|
Ok(body) => Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
.header(
|
.header(
|
||||||
"content-type",
|
hyper::header::CONTENT_TYPE,
|
||||||
mime::extension_to_mime_type(ext).to_string(),
|
hyper::header::HeaderValue::from_static(T::MIME_TYPE),
|
||||||
)
|
)
|
||||||
.body(body.into())
|
.body(body.into())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
@ -2,13 +2,10 @@
|
|||||||
#![deny(elided_lifetimes_in_paths)]
|
#![deny(elided_lifetimes_in_paths)]
|
||||||
#![deny(unreachable_pub)]
|
#![deny(unreachable_pub)]
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
|
|
||||||
use mendes::application::{Application, Responder};
|
use mendes::application::{Application, Responder};
|
||||||
use mendes::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE};
|
use mendes::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE};
|
||||||
use mendes::http::request::Parts;
|
use mendes::http::request::Parts;
|
||||||
use mendes::http::Response;
|
use mendes::http::Response;
|
||||||
use mime_guess::MimeGuess;
|
|
||||||
|
|
||||||
pub use askama::*;
|
pub use askama::*;
|
||||||
|
|
||||||
@ -16,7 +13,7 @@ pub fn into_response<A, T>(
|
|||||||
app: &A,
|
app: &A,
|
||||||
req: &Parts,
|
req: &Parts,
|
||||||
t: &T,
|
t: &T,
|
||||||
ext: Option<&str>,
|
_ext: Option<&str>,
|
||||||
) -> Response<A::ResponseBody>
|
) -> Response<A::ResponseBody>
|
||||||
where
|
where
|
||||||
A: Application,
|
A: Application,
|
||||||
@ -29,13 +26,9 @@ where
|
|||||||
Err(e) => return <A::Error as From<_>>::from(e).into_response(app, req),
|
Err(e) => return <A::Error as From<_>>::from(e).into_response(app, req),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut builder = Response::builder();
|
Response::builder()
|
||||||
builder = builder.header(CONTENT_LENGTH, content.len());
|
.header(CONTENT_LENGTH, content.len())
|
||||||
if let Some(ext) = ext {
|
.header(CONTENT_TYPE, HeaderValue::from_static(T::MIME_TYPE))
|
||||||
if let Some(ty) = MimeGuess::from_ext(ext).first() {
|
.body(content.into())
|
||||||
builder = builder.header(CONTENT_TYPE, HeaderValue::try_from(ty.as_ref()).unwrap());
|
.unwrap()
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.body(content.into()).unwrap()
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ async fn test() {
|
|||||||
rsp.headers
|
rsp.headers
|
||||||
.get("content-type")
|
.get("content-type")
|
||||||
.and_then(|hv| hv.to_str().ok()),
|
.and_then(|hv| hv.to_str().ok()),
|
||||||
Some("text/plain")
|
Some("text/plain; charset=utf-8")
|
||||||
);
|
);
|
||||||
assert_eq!(to_bytes(body).await.unwrap(), &b"Hello, world!"[..]);
|
assert_eq!(to_bytes(body).await.unwrap(), &b"Hello, world!"[..]);
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,15 @@
|
|||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
pub use askama::*;
|
pub use askama::*;
|
||||||
use rocket::http::{ContentType, Status};
|
use rocket::http::{Header, Status};
|
||||||
pub use rocket::request::Request;
|
pub use rocket::request::Request;
|
||||||
use rocket::response::Response;
|
use rocket::response::Response;
|
||||||
pub use rocket::response::{Responder, Result};
|
pub use rocket::response::{Responder, Result};
|
||||||
|
|
||||||
pub fn respond<T: Template>(t: &T, ext: &str) -> Result<'static> {
|
pub fn respond<T: Template>(t: &T, _ext: &str) -> Result<'static> {
|
||||||
let rsp = t.render().map_err(|_| Status::InternalServerError)?;
|
let rsp = t.render().map_err(|_| Status::InternalServerError)?;
|
||||||
let ctype = ContentType::from_extension(ext).ok_or(Status::InternalServerError)?;
|
|
||||||
Response::build()
|
Response::build()
|
||||||
.header(ctype)
|
.header(Header::new("content-type", T::MIME_TYPE))
|
||||||
.sized_body(Cursor::new(rsp))
|
.sized_body(Cursor::new(rsp))
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
@ -6,16 +6,12 @@ pub use askama;
|
|||||||
pub use tide;
|
pub use tide;
|
||||||
|
|
||||||
use askama::*;
|
use askama::*;
|
||||||
use tide::{http::Mime, Body, Response};
|
use tide::{Body, Response};
|
||||||
|
|
||||||
pub fn try_into_body<T: Template>(t: &T, ext: &str) -> Result<Body> {
|
pub fn try_into_body<T: Template>(t: &T, _ext: &str) -> Result<Body> {
|
||||||
let string = t.render()?;
|
let string = t.render()?;
|
||||||
let mut body = Body::from_string(string);
|
let mut body = Body::from_string(string);
|
||||||
|
body.set_mime(T::MIME_TYPE);
|
||||||
if let Some(mime) = Mime::from_extension(ext) {
|
|
||||||
body.set_mime(mime);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(body)
|
Ok(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,14 +9,11 @@ use warp::http::{self, header, StatusCode};
|
|||||||
use warp::hyper::Body;
|
use warp::hyper::Body;
|
||||||
use warp::reply::Response;
|
use warp::reply::Response;
|
||||||
|
|
||||||
pub fn reply<T: askama::Template>(t: &T, ext: &str) -> Response {
|
pub fn reply<T: askama::Template>(t: &T, _ext: &str) -> Response {
|
||||||
match t.render() {
|
match t.render() {
|
||||||
Ok(body) => http::Response::builder()
|
Ok(body) => http::Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
.header(
|
.header(header::CONTENT_TYPE, T::MIME_TYPE)
|
||||||
header::CONTENT_TYPE,
|
|
||||||
mime::extension_to_mime_type(ext).to_string(),
|
|
||||||
)
|
|
||||||
.body(body.into()),
|
.body(body.into()),
|
||||||
Err(_) => http::Response::builder()
|
Err(_) => http::Response::builder()
|
||||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user