Echo Writes Code

handlers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::errors::{ Result };
use crate::content::{ AtomFeed };
use crate::forms::{ self };
use crate::state::{ ApplicationState };
use crate::templates;

use axum::{ Form };
use axum::extract::{ Path, State };
use axum::http::{ StatusCode };
use axum::response::{ Html, IntoResponse, Redirect };

use std::sync::{ Arc };

pub(crate) async fn index_handler(
	State(state): State<Arc<ApplicationState>>,
) -> Result<Html<String>> {
	let context = templates::index_context(&state)?;
	let html = state.tera.render("page.tera.html", &context)?;
	Ok(Html(html))
}

pub(crate) async fn index_atom_feed_handler(
	State(state): State<Arc<ApplicationState>>,
) -> Result<AtomFeed<String>> {
	let context = templates::index_atom_feed_context(&state)?;
	let feed = state.tera.render("atom_feed.tera.xml", &context)?;
	Ok(AtomFeed(feed))
}

pub(crate) async fn list_handler(
	State(state): State<Arc<ApplicationState>>,
	Path(list_name): Path<String>,
) -> Result<Html<String>> {
	let context = templates::list_context(&state, &list_name)?;
	let html = state.tera.render("list.tera.html", &context)?;
	Ok(Html(html))
}

pub(crate) async fn list_atom_feed_handler(
	State(state): State<Arc<ApplicationState>>,
	Path(list_name): Path<String>,
) -> Result<AtomFeed<String>> {
	let context = templates::list_atom_feed_context(&state, &list_name)?;
	let feed = state.tera.render("atom_feed.tera.xml", &context)?;
	Ok(AtomFeed(feed))
}

pub(crate) async fn page_handler(
	State(state): State<Arc<ApplicationState>>,
	Path(page_name): Path<String>,
) -> Result<Html<String>> {
	let context = templates::page_context(&state, &page_name)?;
	let html = state.tera.render("page.tera.html", &context)?;
	Ok(Html(html))
}

pub(crate) async fn post_handler(
	State(state): State<Arc<ApplicationState>>,
	Path(post_name): Path<String>,
) -> Result<Html<String>> {
	let context = templates::post_context(&state, &post_name)?;
	let html = state.tera.render("post.tera.html", &context)?;
	Ok(Html(html))
}

pub(crate) async fn not_found_handler() -> impl IntoResponse {
	(StatusCode::NOT_FOUND, "Not found")
}

pub(crate) async fn contact_form_handler(
	State(state): State<Arc<ApplicationState>>,
	Form(contact): Form<forms::Contact>,
) -> Result<Redirect> {
	contact.send_email(&state)
		.await?;

	Ok(Redirect::to("/page/message-sent"))
}