removed dead code and finished permalinks

This commit is contained in:
Leonard Lorenz 2020-10-22 00:25:30 +02:00
parent cfbeb07f37
commit b848922eee
2 changed files with 15 additions and 14 deletions

View file

@ -16,6 +16,7 @@ pub fn get_posts() -> std::vec::Vec<Post> {
let connection = establish_connection();
posts
.filter(published.eq(true))
.limit(5)
.load::<Post>(&connection)
.expect("Error loading posts")
}

View file

@ -4,19 +4,6 @@ use actix_files as fs;
use actix_web::{get, web, HttpResponse, Responder};
use tera::{Context, Tera};
fn create_context() -> tera::Context {
// Use globbing
let tera: Tera = match Tera::new("templates/**/*.html") {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
}
};
Context::new()
}
#[get("/")]
async fn root() -> impl Responder {
fs::NamedFile::open("html/index.html")
@ -44,5 +31,18 @@ async fn blog() -> impl Responder {
#[get("/blog/{post_id}")]
async fn blog_permalink(web::Path(post_id): web::Path<u32>) -> impl Responder {
let post = get_post_by_id(post_id as i32);
HttpResponse::Ok().json(post)
let mut context = Context::new();
context.insert("posts", &[post]);
// one-off render blog template with context
let result = Tera::one_off(
&(std::fs::read_to_string("templates/blog.html")
.unwrap_or_else(|_| panic!("Couldn't load blog template."))
.as_str()),
&context,
true,
)
.unwrap_or_else(|_| panic!("Couldn't render blog template."));
HttpResponse::Ok().body(result)
}