diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers.rs | 45 | ||||
-rw-r--r-- | src/main.rs | 47 | ||||
-rw-r--r-- | src/middleware/cache_control.rs | 2 | ||||
-rw-r--r-- | src/posts/fs_post.rs | 8 | ||||
-rw-r--r-- | src/tutors/fs_tutor.rs | 4 | ||||
-rw-r--r-- | src/views.rs | 3 | ||||
-rw-r--r-- | src/views/about.rs | 2 | ||||
-rw-r--r-- | src/views/brochure.rs | 2 | ||||
-rw-r--r-- | src/views/highered.rs | 6 | ||||
-rw-r--r-- | src/views/index.rs | 2 | ||||
-rw-r--r-- | src/views/k12.rs | 6 | ||||
-rw-r--r-- | src/views/policies.rs | 2 | ||||
-rw-r--r-- | src/views/post.rs | 2 | ||||
-rw-r--r-- | src/views/posts.rs | 2 | ||||
-rw-r--r-- | src/views/pro.rs | 6 |
15 files changed, 98 insertions, 41 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 800d8f8..aa36e15 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,15 +1,18 @@ -use askama::Template; -use crate::views::post::PostView; -use crate::views::posts::PostsView; use crate::posts::abstractions::repo::PostRepo; -use crate::views::policies::PoliciesTemplate; -use crate::views::index::IndexTemplate; -use crate::views::brochure::BrochureTemplate; -use crate::views::about::AboutView; use crate::tutors::abstractions::tutor_repo::TutorRepo; +use crate::views::about::AboutView; +use crate::views::brochure::BrochureTemplate; +use crate::views::highered::HigherEdTemplate; +use crate::views::index::IndexTemplate; +use crate::views::k12::K12Template; +use crate::views::policies::PoliciesTemplate; +use crate::views::post::PostView; +use crate::views::posts::PostsView; +use crate::views::pro::ProTemplate; +use askama::Template; +use axum::extract::{Path, State}; +use axum::response::{Html, Redirect}; use std::sync::Arc; -use axum::response::Html; -use axum::extract::{State, Path}; pub async fn about_handler(State(repo): State<Arc<impl TutorRepo>>) -> Html<String> { let view = AboutView::with_tutors(repo.load()); @@ -17,7 +20,7 @@ pub async fn about_handler(State(repo): State<Arc<impl TutorRepo>>) -> Html<Stri } pub async fn brochure_handler() -> Html<String> { - Html(BrochureTemplate{}.render().unwrap()) + Html(BrochureTemplate {}.render().unwrap()) } pub async fn index_handler() -> Html<String> { @@ -25,7 +28,7 @@ pub async fn index_handler() -> Html<String> { } pub async fn policies_handler() -> Html<String> { - Html(PoliciesTemplate{}.render().unwrap()) + Html(PoliciesTemplate {}.render().unwrap()) } pub async fn posts_handler(State(repo): State<Arc<impl PostRepo>>) -> Html<String> { @@ -33,8 +36,26 @@ pub async fn posts_handler(State(repo): State<Arc<impl PostRepo>>) -> Html<Strin Html(view.render().unwrap()) } -pub async fn post_handler(Path(post_id): Path<String>, State(repo): State<Arc<impl PostRepo>>) -> Html<String> { +pub async fn post_handler( + Path(post_id): Path<String>, + State(repo): State<Arc<impl PostRepo>>, +) -> Html<String> { let view = PostView::with_post(repo.by_id(&post_id)); Html(view.render().unwrap()) } +pub async fn post_redirect(Path(post_id): Path<String>) -> Redirect { + Redirect::permanent(&format!("/blog/{post_id}")) +} + +pub async fn k12_handler() -> Html<String> { + Html(K12Template {}.render().unwrap()) +} + +pub async fn highered_handler() -> Html<String> { + Html(HigherEdTemplate {}.render().unwrap()) +} + +pub async fn pro_handler() -> Html<String> { + Html(ProTemplate {}.render().unwrap()) +} diff --git a/src/main.rs b/src/main.rs index 372915b..a9cd722 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,24 @@ +use axum::response::Redirect; +use axum::{extract::Request, routing::get, Router, ServiceExt}; +use middleware::cache_control::cache_static; +use posts::fs_post_repo::FsPostRepo; +use std::{env, sync::Arc}; use tower::Layer; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; -use tower_http::{trace::{self, TraceLayer}, normalize_path::NormalizePathLayer}; +use tower_http::services::ServeDir; +use tower_http::{ + normalize_path::NormalizePathLayer, + trace::{self, TraceLayer}, +}; use tracing::{info, Level}; -use axum::{routing::get, Router, ServiceExt, extract::Request}; +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; use tutors::fs_tutor_repo::FsTutorRepo; -use std::{sync::Arc, env}; -use tower_http::services::ServeDir; -use posts::fs_post_repo::FsPostRepo; -use middleware::cache_control::cache_static; +mod handlers; mod helpers; +mod middleware; mod posts; mod tutors; mod views; -mod handlers; -mod middleware; #[tokio::main] async fn main() { @@ -28,9 +32,9 @@ async fn main() { .init(); info!("loading state..."); - let blog_dir = env::var("CT_POSTS").unwrap(); - let tutor_dir = env::var("CT_TEAM").unwrap(); - let assets_dir = env::var("CT_ASSETS").unwrap(); + let blog_dir = env::var("CT_POSTS").unwrap_or(String::from("/var/ct/posts")); + let tutor_dir = env::var("CT_TEAM").unwrap_or(String::from("/var/ct/team")); + let assets_dir = env::var("CT_ASSETS").unwrap_or(String::from("/var/ct/assets")); let posts = Arc::new(FsPostRepo::with_dir(blog_dir)); let tutors = Arc::new(FsTutorRepo::with_dir(tutor_dir.clone())); @@ -38,12 +42,17 @@ async fn main() { info!("initializing router..."); let app = Router::new() .route("/", get(handlers::index_handler)) - .route("/posts", get(handlers::posts_handler)) - .route("/posts/:post_id", get(handlers::post_handler)) + .route("/posts", get(|| async { Redirect::permanent("/blog") })) + .route("/posts/{post_id}", get(handlers::post_redirect)) + .route("/blog", get(handlers::posts_handler)) + .route("/blog/{post_id}", get(handlers::post_handler)) .with_state(posts) .route("/policies", get(handlers::policies_handler)) .route("/brochure", get(handlers::brochure_handler)) .route("/about", get(handlers::about_handler)) + .route("/k12", get(handlers::k12_handler)) + .route("/highered", get(handlers::highered_handler)) + .route("/professional", get(handlers::pro_handler)) .with_state(tutors) .nest_service("/assets", ServeDir::new(assets_dir)) .nest_service("/team", ServeDir::new(tutor_dir)) @@ -51,14 +60,14 @@ async fn main() { .layer(axum::middleware::from_fn(cache_static)) .layer( TraceLayer::new_for_http() - .make_span_with(trace::DefaultMakeSpan::new() - .level(Level::INFO)) - .on_response(trace::DefaultOnResponse::new() - .level(Level::INFO)) + .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) + .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), ); let app = NormalizePathLayer::trim_trailing_slash().layer(app); let addr = env::var("CT_BIND").unwrap_or("0.0.0.0:8000".into()); let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); - axum::serve(listener, ServiceExt::<Request>::into_make_service(app)).await.unwrap(); + axum::serve(listener, ServiceExt::<Request>::into_make_service(app)) + .await + .unwrap(); } diff --git a/src/middleware/cache_control.rs b/src/middleware/cache_control.rs index 30f31ca..0f11924 100644 --- a/src/middleware/cache_control.rs +++ b/src/middleware/cache_control.rs @@ -3,7 +3,7 @@ use axum::http::header::{HeaderValue, CACHE_CONTROL}; use axum::middleware::Next; use axum::response::Response; -pub async fn cache_static(mut request: Request, next: Next) -> Response { +pub async fn cache_static(request: Request, next: Next) -> Response { let was_static = request.uri().path().starts_with("/assets"); let mut response = next.run(request).await; diff --git a/src/posts/fs_post.rs b/src/posts/fs_post.rs index 6023c40..12ce538 100644 --- a/src/posts/fs_post.rs +++ b/src/posts/fs_post.rs @@ -1,3 +1,5 @@ +use comrak::Options; + use crate::posts::abstractions::post::Post; use std::{borrow::Cow, fs, path::PathBuf}; @@ -19,18 +21,20 @@ impl Post for FsPost { fn get_article(&self) -> Cow<str> { let article = fs::read_to_string(&self.file).unwrap(); - Cow::Owned(article) + Cow::Owned(comrak::markdown_to_html(&article, &Options::default())) } fn get_description(&self) -> Cow<str> { let article = self.get_article(); Cow::Owned( article - .split_once("\n") + .split_once('\n') .map(|(first, _)| first) .unwrap_or_default() .trim_start_matches('_') .trim_start_matches('*') + .trim_end_matches('_') + .trim_end_matches('*') .to_owned(), ) } diff --git a/src/tutors/fs_tutor.rs b/src/tutors/fs_tutor.rs index dc8a635..6b05fd8 100644 --- a/src/tutors/fs_tutor.rs +++ b/src/tutors/fs_tutor.rs @@ -1,3 +1,5 @@ +use comrak::Options; + use crate::tutors::abstractions::tutor::Tutor; use std::{borrow::Cow, cmp::Ordering, fs, path::PathBuf}; @@ -25,7 +27,7 @@ impl Tutor for FsTutor { let mut path = self.dir.to_owned(); path.push(format!("{}.md", self.get_id())); let blurb = fs::read_to_string(path).unwrap(); - Cow::Owned(blurb) + Cow::Owned(comrak::markdown_to_html(&blurb, &Options::default())) } } diff --git a/src/views.rs b/src/views.rs index cb58813..f880818 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,6 +1,9 @@ pub mod about; pub mod brochure; +pub mod highered; pub mod index; +pub mod k12; pub mod policies; pub mod post; pub mod posts; +pub mod pro; diff --git a/src/views/about.rs b/src/views/about.rs index 349c9de..d2e0958 100644 --- a/src/views/about.rs +++ b/src/views/about.rs @@ -3,7 +3,7 @@ use crate::tutors::abstractions::tutor::Tutor; use askama::Template; #[derive(Template)] -#[template(path = "about/index.html")] +#[template(path = "about/index.html.j2")] pub struct AboutView<T: Tutor> { tutors: Vec<T>, } diff --git a/src/views/brochure.rs b/src/views/brochure.rs index 0d2f8fc..fedf827 100644 --- a/src/views/brochure.rs +++ b/src/views/brochure.rs @@ -2,5 +2,5 @@ use crate::helpers::*; use askama::Template; #[derive(Template)] -#[template(path = "brochure/index.html")] +#[template(path = "brochure/index.html.j2")] pub struct BrochureTemplate; diff --git a/src/views/highered.rs b/src/views/highered.rs new file mode 100644 index 0000000..108a76a --- /dev/null +++ b/src/views/highered.rs @@ -0,0 +1,6 @@ +use crate::helpers::*; +use askama::Template; + +#[derive(Template)] +#[template(path = "highered.html.j2")] +pub struct HigherEdTemplate; diff --git a/src/views/index.rs b/src/views/index.rs index 3ced24d..720749a 100644 --- a/src/views/index.rs +++ b/src/views/index.rs @@ -2,5 +2,5 @@ use crate::helpers::*; use askama::Template; #[derive(Template)] -#[template(path = "index.html")] +#[template(path = "index.html.j2")] pub struct IndexTemplate; diff --git a/src/views/k12.rs b/src/views/k12.rs new file mode 100644 index 0000000..c7dc019 --- /dev/null +++ b/src/views/k12.rs @@ -0,0 +1,6 @@ +use crate::helpers::*; +use askama::Template; + +#[derive(Template)] +#[template(path = "k12.html.j2")] +pub struct K12Template; diff --git a/src/views/policies.rs b/src/views/policies.rs index 3d9787d..4253e6c 100644 --- a/src/views/policies.rs +++ b/src/views/policies.rs @@ -2,5 +2,5 @@ use crate::helpers::*; use askama::Template; #[derive(Template)] -#[template(path = "policies.html")] +#[template(path = "policies.html.j2")] pub struct PoliciesTemplate; diff --git a/src/views/post.rs b/src/views/post.rs index 4f0554b..03e5ef2 100644 --- a/src/views/post.rs +++ b/src/views/post.rs @@ -3,7 +3,7 @@ use crate::posts::abstractions::post::Post; use askama::Template; #[derive(Template)] -#[template(path = "post.html")] +#[template(path = "post.html.j2")] pub struct PostView<P: Post> { post: P, } diff --git a/src/views/posts.rs b/src/views/posts.rs index 82b5996..5091059 100644 --- a/src/views/posts.rs +++ b/src/views/posts.rs @@ -3,7 +3,7 @@ use crate::posts::abstractions::post::Post; use askama::Template; #[derive(Template)] -#[template(path = "posts.html")] +#[template(path = "posts.html.j2")] pub struct PostsView<P: Post> { posts: Vec<P>, } diff --git a/src/views/pro.rs b/src/views/pro.rs new file mode 100644 index 0000000..eacfd7c --- /dev/null +++ b/src/views/pro.rs @@ -0,0 +1,6 @@ +use crate::helpers::*; +use askama::Template; + +#[derive(Template)] +#[template(path = "pro.html.j2")] +pub struct ProTemplate; |