summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs82
1 files changed, 47 insertions, 35 deletions
diff --git a/src/main.rs b/src/main.rs
index 43b6fa6..ca0a3d9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+use tower_http::services::ServeDir;
use axum::response::Html;
use axum::extract::State;
use std::sync::Arc;
@@ -11,29 +12,12 @@ use std::io::*;
use std::path::Path;
use std::path::PathBuf;
-trait Post<'a>: fmt::Debug {
+trait Post: fmt::Debug {
fn dump(&self);
- fn display_name(&'a self) -> &'a str;
+ fn display_name(&self) -> Cow<str>;
fn get_content(&self) -> Cow<str>;
}
-#[derive(Debug)]
-struct MockPost;
-
-impl<'a> Post<'a> for MockPost {
- fn dump(&self) {
- println!("Post content goes here.");
- }
-
- fn display_name(&'a self) -> &'a str {
- ""
- }
-
- fn get_content(&self) -> Cow<str> {
- Cow::Borrowed("")
- }
-}
-
struct MdFilePost {
path: PathBuf,
name: OsString,
@@ -48,13 +32,13 @@ impl MdFilePost {
}
}
-impl<'a> Post<'a> for MdFilePost {
+impl Post for MdFilePost {
fn dump(&self) {
println!("{}: {}", &self.display_name(), &self.get_content());
}
- fn display_name(&'a self) -> &'a str {
- self.name.to_str().unwrap()
+ fn display_name(&self) -> Cow<str> {
+ self.name.to_string_lossy()
}
fn get_content(&self) -> Cow<str> {
@@ -94,8 +78,8 @@ impl Posts for FsDirPosts {
}
}
-trait Page<'a>: Template {
- fn from_post(post: &impl Post<'a>) -> Self;
+trait Page: Template {
+ fn from_post(post: &impl Post) -> Self;
}
#[derive(Template)]
@@ -104,32 +88,60 @@ struct MdPage {
article: String,
}
-impl<'a> Page<'a> for MdPage {
- fn from_post(post: &impl Post<'a>) -> Self {
+impl Page for MdPage {
+ fn from_post(post: &impl Post) -> Self {
Self {
article: post.get_content().into_owned(),
}
}
}
-fn test_get_posts(posts: &impl Posts) {
- for post in posts.get_posts() {
- let page = MdPage::from_post(&post);
- println!("{}", page.render().unwrap());
+#[derive(Template)]
+#[template(path = "posts.html")]
+struct PostsView {
+ post_titles: Vec<String>
+}
+
+impl PostsView {
+ fn with_posts(posts: &impl Posts) -> Self {
+ Self {
+ post_titles: posts.get_posts().map(|p| p.display_name().into_owned()).map(String::from).collect()
+ }
}
}
-async fn handler(State(posts): State<Arc<impl Posts>>) -> Html<String> {
- let post = posts.get_posts().next().unwrap();
- let page = MdPage::from_post(&post);
- Html(page.render().unwrap())
+async fn posts_handler(State(posts): State<Arc<impl Posts>>) -> Html<String> {
+ let view = PostsView::with_posts(&*posts);
+ Html(view.render().unwrap())
+}
+
+#[derive(Template)]
+#[template(path = "index.html")]
+struct IndexTemplate;
+
+async fn index_handler() -> Html<String> {
+ Html(IndexTemplate {}.render().unwrap())
+}
+
+#[derive(Template)]
+#[template(path = "policies.html")]
+struct PoliciesTemplate;
+
+async fn policies_handler() -> Html<String> {
+ Html(PoliciesTemplate{}.render().unwrap())
}
#[tokio::main]
async fn main() {
let repo = Arc::new(FsDirPosts::new(&format!("/data/ct/{}", "blog")));
- let app = Router::new().route("/posts", get(handler)).with_state(repo);
+ let app = Router::new()
+ .nest_service("/static", ServeDir::new("static"))
+ .nest_service("/assets", ServeDir::new("/data/ct/assets"))
+ .route("/posts", get(posts_handler))
+ .route("/policies", get(policies_handler))
+ .route("/", get(index_handler))
+ .with_state(repo);
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
axum::serve(listener, app).await.unwrap();