impl Responder for Html<T: Template>
This commit is contained in:
parent
591fd66786
commit
65dd3252c9
27
src/main.rs
27
src/main.rs
|
@ -6,11 +6,11 @@
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use reqwest::{Client, StatusCode, Url};
|
use reqwest::{Client, StatusCode, Url};
|
||||||
use rocket::form::{Form, FromForm};
|
use rocket::form::{Form, FromForm};
|
||||||
use rocket::http::{ContentType, Header};
|
use rocket::http::{ContentType, Header, Status};
|
||||||
use rocket::response::status::NoContent;
|
use rocket::response::{self, status::NoContent, Debug, Redirect, Responder};
|
||||||
use rocket::response::{Debug, Redirect, Responder};
|
use rocket::{get, post, routes, uri, Request, Response, State};
|
||||||
use rocket::{get, post, routes, uri, State};
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::io::Cursor;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[rocket::launch]
|
#[rocket::launch]
|
||||||
|
@ -38,13 +38,26 @@ fn rocket() -> _ {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[derive(Debug)]
|
||||||
fn index() -> Result<(ContentType, String), Debug<askama::Error>> {
|
struct Html<T: Template>(T);
|
||||||
|
|
||||||
|
impl<T: Template> Responder<'_, 'static> for Html<T> {
|
||||||
|
fn respond_to(self, _request: &Request<'_>) -> response::Result<'static> {
|
||||||
|
let data = self.0.render().map_err(|_| Status::InternalServerError)?;
|
||||||
|
Response::build()
|
||||||
|
.header(Header::new("content-type", T::MIME_TYPE))
|
||||||
|
.sized_body(data.len(), Cursor::new(data))
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct Index;
|
struct Index;
|
||||||
|
|
||||||
Ok((ContentType::HTML, Index.render()?))
|
#[get("/")]
|
||||||
|
fn index() -> Html<Index> {
|
||||||
|
Html(Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
|
|
Loading…
Reference in New Issue