1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- use tide::{
- http::mime,
- sessions::{CookieStore, SessionMiddleware},
- Response, Result, StatusCode,
- };
-
- use std::{env, io::ErrorKind};
- use tera::Context;
-
- use crate::routes;
-
- pub fn session() -> SessionMiddleware<CookieStore> {
- SessionMiddleware::new(
- CookieStore::new(),
- env::var("TIDE_SECRET")
- .expect(
- "Please provide a TIDE_SECRET value of at \
- least 32 bytes in order to run this example",
- )
- .as_bytes(),
- )
- }
-
- pub async fn errors(mut res: Response) -> Result<Response> {
- let mut context = Context::new();
-
- match res.downcast_error::<async_std::io::Error>() {
- Some(e) => {
- context.insert("error", &e.to_string());
- let status = if let ErrorKind::NotFound = e.kind() {
- StatusCode::NotFound
- } else {
- StatusCode::InternalServerError
- };
- let body: String = routes::render_template("error.html", &context)?;
- res.set_body(body);
- res.set_status(status);
- }
- None => match res.status() {
- StatusCode::NotFound => {
- context.insert("error", "Page not found");
- res.set_body(routes::render_template("error.html", &context)?);
- }
- _ => {}
- },
- };
-
- res.set_content_type(mime::HTML);
- Ok(res)
- }
|