Update
This commit is contained in:
parent
501b9d3093
commit
964534d0d9
21 changed files with 762 additions and 207 deletions
|
@ -9,7 +9,7 @@ use anyhow::{bail, Result};
|
|||
use chrono::prelude::*;
|
||||
use clap::{Parser, Subcommand};
|
||||
use diesel::prelude::*;
|
||||
use r2d2_redis::redis;
|
||||
use itertools::Itertools;
|
||||
use scan_dir::ScanDir;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
|
@ -33,12 +33,13 @@ enum Commands {
|
|||
Clear,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
struct PostFrontmatter {
|
||||
id: Option<i32>,
|
||||
name: String,
|
||||
slug: String,
|
||||
description: String,
|
||||
tags: Vec<String>,
|
||||
published_at: NaiveDate,
|
||||
edited_at: Option<NaiveDate>,
|
||||
active: bool,
|
||||
|
@ -57,9 +58,9 @@ fn main() -> Result<()> {
|
|||
let db_conn = &mut db::establish_connection()?;
|
||||
let redis_conn = &mut cache::establish_connection()?;
|
||||
|
||||
let posts = ScanDir::dirs()
|
||||
.read("/blog", |iter| {
|
||||
iter.map(|(entry, _)| {
|
||||
let (tags_raw, posts_raw) = ScanDir::dirs().read("/blog", |iter| -> Result<_> {
|
||||
let x = iter
|
||||
.map(|(entry, _)| {
|
||||
let path = entry.path().join("post.md");
|
||||
let src = fs::read_to_string(&path)?;
|
||||
let frontmatter = match fronma::parser::parse::<PostFrontmatter>(&src) {
|
||||
|
@ -67,54 +68,74 @@ fn main() -> Result<()> {
|
|||
Err(x) => bail!("Error parsing frontmatter: {:?}", x),
|
||||
};
|
||||
|
||||
Ok(Post {
|
||||
path,
|
||||
frontmatter: frontmatter.headers,
|
||||
content: frontmatter.body.to_string(),
|
||||
})
|
||||
Ok((
|
||||
frontmatter.headers.tags.to_owned(),
|
||||
Post {
|
||||
path,
|
||||
frontmatter: frontmatter.headers,
|
||||
content: frontmatter.body.to_string(),
|
||||
},
|
||||
))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()
|
||||
})??
|
||||
.collect::<Result<Vec<(Vec<String>, Post)>>>()?;
|
||||
|
||||
let tags: Vec<String> = x.iter().flat_map(|y| y.0.to_owned()).unique().collect();
|
||||
let posts: Vec<Post> = x.into_iter().map(|y| y.1).collect();
|
||||
|
||||
Ok((tags, posts))
|
||||
})??;
|
||||
|
||||
let tags = tags_raw
|
||||
.into_iter()
|
||||
.map(|post| -> Result<_> {
|
||||
.map(|tag| {
|
||||
Ok(
|
||||
match db::schema::tags::table
|
||||
.select(db::schema::tags::id)
|
||||
.filter(db::schema::tags::name.eq(&tag))
|
||||
.first::<i32>(db_conn)
|
||||
.optional()?
|
||||
{
|
||||
Some(x) => x,
|
||||
None => diesel::insert_into(db::schema::tags::table)
|
||||
.values(db::models::NewTag { name: &tag })
|
||||
.returning(db::schema::tags::id)
|
||||
.get_result::<i32>(db_conn)?,
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
diesel::delete(
|
||||
db::schema::tags::table.filter(diesel::dsl::not(db::schema::tags::id.eq_any(tags))),
|
||||
)
|
||||
.execute(db_conn)?;
|
||||
|
||||
let posts = posts_raw
|
||||
.into_iter()
|
||||
.map(|post| {
|
||||
let trimmed = PostFrontmatter {
|
||||
id: post.frontmatter.id,
|
||||
name: post.frontmatter.name.trim().to_string(),
|
||||
slug: post.frontmatter.slug.trim().to_string(),
|
||||
description: post.frontmatter.description.trim().to_string(),
|
||||
tags: post
|
||||
.frontmatter
|
||||
.tags
|
||||
.iter()
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect(),
|
||||
published_at: post.frontmatter.published_at,
|
||||
edited_at: post.frontmatter.edited_at,
|
||||
active: post.frontmatter.active,
|
||||
};
|
||||
let content = post.content.trim();
|
||||
|
||||
if let Some(id) = trimmed.id {
|
||||
diesel::update(db::schema::posts::table)
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.set(db::models::UpdatePost {
|
||||
name: Some(&trimmed.name),
|
||||
slug: Some(&trimmed.slug),
|
||||
description: Some(&trimmed.description),
|
||||
content: Some(content),
|
||||
published_at: Some(trimmed.published_at),
|
||||
edited_at: Some(trimmed.edited_at),
|
||||
active: Some(trimmed.active),
|
||||
})
|
||||
.execute(db_conn)?;
|
||||
|
||||
Ok(id)
|
||||
} else {
|
||||
let id = if let Some(id) = db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.filter(db::schema::posts::slug.eq(&trimmed.slug))
|
||||
.first::<i32>(db_conn)
|
||||
.optional()?
|
||||
{
|
||||
let id = {
|
||||
let res: Result<i32, anyhow::Error> = if let Some(id) = trimmed.id {
|
||||
diesel::update(db::schema::posts::table)
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.set(db::models::UpdatePost {
|
||||
name: Some(&trimmed.name),
|
||||
slug: None,
|
||||
slug: Some(&trimmed.slug),
|
||||
description: Some(&trimmed.description),
|
||||
content: Some(content),
|
||||
published_at: Some(trimmed.published_at),
|
||||
|
@ -123,69 +144,111 @@ fn main() -> Result<()> {
|
|||
})
|
||||
.execute(db_conn)?;
|
||||
|
||||
id
|
||||
Ok(id)
|
||||
} else {
|
||||
diesel::insert_into(db::schema::posts::table)
|
||||
.values(db::models::NewPost {
|
||||
name: &trimmed.name,
|
||||
slug: &trimmed.slug,
|
||||
description: &trimmed.description,
|
||||
content: content,
|
||||
published_at: trimmed.published_at,
|
||||
edited_at: trimmed.edited_at,
|
||||
active: trimmed.active,
|
||||
})
|
||||
.returning(db::schema::posts::id)
|
||||
.get_result::<i32>(db_conn)?
|
||||
let id = if let Some(id) = db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.filter(db::schema::posts::slug.eq(&trimmed.slug))
|
||||
.first::<i32>(db_conn)
|
||||
.optional()?
|
||||
{
|
||||
diesel::update(db::schema::posts::table)
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.set(db::models::UpdatePost {
|
||||
name: Some(&trimmed.name),
|
||||
slug: None,
|
||||
description: Some(&trimmed.description),
|
||||
content: Some(content),
|
||||
published_at: Some(trimmed.published_at),
|
||||
edited_at: Some(trimmed.edited_at),
|
||||
active: Some(trimmed.active),
|
||||
})
|
||||
.execute(db_conn)?;
|
||||
|
||||
id
|
||||
} else {
|
||||
diesel::insert_into(db::schema::posts::table)
|
||||
.values(db::models::NewPost {
|
||||
name: &trimmed.name,
|
||||
slug: &trimmed.slug,
|
||||
description: &trimmed.description,
|
||||
content: content,
|
||||
published_at: trimmed.published_at,
|
||||
edited_at: trimmed.edited_at,
|
||||
active: trimmed.active,
|
||||
})
|
||||
.returning(db::schema::posts::id)
|
||||
.get_result::<i32>(db_conn)?
|
||||
};
|
||||
|
||||
fs::write(
|
||||
post.path,
|
||||
format!(
|
||||
"---\n{}---\n\n{}\n",
|
||||
serde_yaml::to_string(&PostFrontmatter {
|
||||
id: Some(id),
|
||||
..trimmed.to_owned()
|
||||
})?,
|
||||
content
|
||||
),
|
||||
)?;
|
||||
|
||||
Ok(id)
|
||||
};
|
||||
|
||||
fs::write(
|
||||
post.path,
|
||||
format!(
|
||||
"---\n{}---\n\n{}\n",
|
||||
serde_yaml::to_string(&PostFrontmatter {
|
||||
id: Some(id),
|
||||
..trimmed
|
||||
})?,
|
||||
content
|
||||
),
|
||||
)?;
|
||||
res
|
||||
}?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
let tag_ids = db::schema::tags::table
|
||||
.select(db::schema::tags::id)
|
||||
.filter(db::schema::tags::name.eq_any(trimmed.tags))
|
||||
.load::<i32>(db_conn)?;
|
||||
|
||||
let post_tags = db::schema::post_tags::table
|
||||
.filter(db::schema::post_tags::post_id.eq(id))
|
||||
.load::<db::models::PostTag>(db_conn)?;
|
||||
|
||||
diesel::delete(
|
||||
db::schema::post_tags::table
|
||||
.filter(db::schema::post_tags::post_id.eq(id))
|
||||
.filter(diesel::dsl::not(
|
||||
db::schema::post_tags::tag_id.eq_any(&tag_ids),
|
||||
)),
|
||||
)
|
||||
.execute(db_conn)?;
|
||||
|
||||
let post_tag_tag_ids: Vec<_> = post_tags.iter().map(|x| x.tag_id).collect();
|
||||
diesel::insert_into(db::schema::post_tags::table)
|
||||
.values(
|
||||
tag_ids
|
||||
.into_iter()
|
||||
.filter(|x| !post_tag_tag_ids.contains(x))
|
||||
.map(|x| db::models::NewPostTag {
|
||||
post_id: id,
|
||||
tag_id: x,
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.execute(db_conn)?;
|
||||
|
||||
Ok(id)
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let ids = db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.load::<i32>(db_conn)?;
|
||||
|
||||
diesel::delete(
|
||||
db::schema::posts::table
|
||||
.filter(diesel::dsl::not(db::schema::posts::id.eq_any(posts))),
|
||||
)
|
||||
.execute(db_conn)?;
|
||||
|
||||
for id in ids {
|
||||
redis::cmd("DEL")
|
||||
.arg(cache::keys::post_content(id))
|
||||
.query::<()>(redis_conn)?;
|
||||
}
|
||||
cache::clear(redis_conn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Commands::Clear => {
|
||||
let db_conn = &mut db::establish_connection()?;
|
||||
let redis_conn = &mut cache::establish_connection()?;
|
||||
|
||||
for id in db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.load::<i32>(db_conn)?
|
||||
{
|
||||
redis::cmd("DEL")
|
||||
.arg(cache::keys::post_content(id))
|
||||
.query::<()>(redis_conn)?;
|
||||
}
|
||||
cache::clear(redis_conn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
use anyhow::Result;
|
||||
use chrono::prelude::*;
|
||||
use diesel::prelude::*;
|
||||
use lazy_static::lazy_static;
|
||||
use r2d2_redis::{r2d2, redis, RedisConnectionManager};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::config;
|
||||
use crate::{config, db, markdown, web};
|
||||
|
||||
pub type RedisPool = r2d2::Pool<RedisConnectionManager>;
|
||||
pub type ConnectionPool = r2d2::PooledConnection<RedisConnectionManager>;
|
||||
pub type Connection = redis::Connection;
|
||||
|
||||
pub fn establish_connection() -> Result<redis::Connection> {
|
||||
Ok(redis::Client::open(config::CONFIG.redis_url.as_str())?.get_connection()?)
|
||||
|
@ -22,7 +26,201 @@ lazy_static! {
|
|||
}
|
||||
|
||||
pub mod keys {
|
||||
pub fn post_content(id: i32) -> String {
|
||||
format!("post_content:{}", id)
|
||||
pub const POSTS: &str = "posts";
|
||||
|
||||
pub const POST: &str = "post:";
|
||||
|
||||
pub fn post(id: i32) -> String {
|
||||
format!("{}{}", POST, id)
|
||||
}
|
||||
|
||||
pub const TAG_POSTS: &str = "tag_posts:";
|
||||
|
||||
pub fn tag_posts(id: i32) -> String {
|
||||
format!("{}{}", TAG_POSTS, id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(redis_conn: &mut Connection) -> Result<()> {
|
||||
redis::cmd("UNLINK").arg(keys::POSTS).query(redis_conn)?;
|
||||
|
||||
for key in redis::cmd("KEYS")
|
||||
.arg(format!("{}*", keys::POST))
|
||||
.query::<Vec<String>>(redis_conn)?
|
||||
{
|
||||
redis::cmd("UNLINK").arg(key).query(redis_conn)?;
|
||||
}
|
||||
|
||||
for key in redis::cmd("KEYS")
|
||||
.arg(format!("{}*", keys::TAG_POSTS))
|
||||
.query::<Vec<String>>(redis_conn)?
|
||||
{
|
||||
redis::cmd("UNLINK").arg(key).query(redis_conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cache_posts(
|
||||
db_conn: &mut db::Connection,
|
||||
redis_conn: &mut Connection,
|
||||
) -> Result<Vec<web::templates::PostIndexPost>> {
|
||||
if let Some(s) = redis::cmd("GET")
|
||||
.arg(keys::POSTS)
|
||||
.query::<Option<String>>(redis_conn)?
|
||||
{
|
||||
Ok(serde_json::from_str(&s)?)
|
||||
} else {
|
||||
let posts_cache: Vec<web::templates::PostIndexPost> = db::schema::posts::table
|
||||
.select((
|
||||
db::schema::posts::id,
|
||||
db::schema::posts::name,
|
||||
db::schema::posts::slug,
|
||||
db::schema::posts::description,
|
||||
db::schema::posts::published_at,
|
||||
db::schema::posts::edited_at,
|
||||
))
|
||||
.filter(db::schema::posts::active)
|
||||
.order(db::schema::posts::published_at.desc())
|
||||
.load::<(i32, String, String, String, NaiveDate, Option<NaiveDate>)>(db_conn)?
|
||||
.into_iter()
|
||||
.map(
|
||||
|p: (i32, String, String, String, NaiveDate, Option<NaiveDate>)| -> Result<_> {
|
||||
let (id, name, slug, description, published_at, edited_at) = p;
|
||||
let tags = web::get_tags_by_post(id, db_conn)?;
|
||||
|
||||
Ok(web::templates::PostIndexPost {
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
published_at,
|
||||
edited_at,
|
||||
tags,
|
||||
})
|
||||
},
|
||||
)
|
||||
.collect::<Result<_>>()?;
|
||||
|
||||
redis::cmd("SET")
|
||||
.arg(keys::POSTS)
|
||||
.arg(serde_json::to_string(&posts_cache)?)
|
||||
.query(redis_conn)?;
|
||||
redis::cmd("EXPIRE")
|
||||
.arg(keys::POSTS)
|
||||
.arg(config::CONFIG.cache_ttl)
|
||||
.query(redis_conn)?;
|
||||
|
||||
Ok(posts_cache)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Post {
|
||||
pub name: String,
|
||||
pub slug: String,
|
||||
pub published_at: NaiveDate,
|
||||
pub edited_at: Option<NaiveDate>,
|
||||
pub tags: Vec<String>,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
pub fn cache_post(
|
||||
id: i32,
|
||||
db_conn: &mut db::Connection,
|
||||
redis_conn: &mut Connection,
|
||||
) -> Result<Post> {
|
||||
let key = keys::post(id);
|
||||
|
||||
if let Some(s) = redis::cmd("GET")
|
||||
.arg(&key)
|
||||
.query::<Option<String>>(redis_conn)?
|
||||
{
|
||||
Ok(serde_json::from_str(&s)?)
|
||||
} else {
|
||||
let post = db::schema::posts::table
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.first::<db::models::Post>(db_conn)?;
|
||||
|
||||
let post_cache = Post {
|
||||
name: post.name,
|
||||
slug: post.slug,
|
||||
published_at: post.published_at,
|
||||
edited_at: post.edited_at,
|
||||
tags: web::get_tags_by_post(id, db_conn)?,
|
||||
content: markdown::to_html(&post.content),
|
||||
};
|
||||
|
||||
redis::cmd("SET")
|
||||
.arg(&key)
|
||||
.arg(serde_json::to_string(&post_cache)?)
|
||||
.query(redis_conn)?;
|
||||
redis::cmd("EXPIRE")
|
||||
.arg(key)
|
||||
.arg(config::CONFIG.cache_ttl)
|
||||
.query(redis_conn)?;
|
||||
|
||||
Ok(post_cache)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cache_tag_posts(
|
||||
id: i32,
|
||||
db_conn: &mut db::Connection,
|
||||
redis_conn: &mut Connection,
|
||||
) -> Result<Vec<web::templates::PostIndexPost>> {
|
||||
let key = keys::tag_posts(id);
|
||||
|
||||
if let Some(s) = redis::cmd("GET")
|
||||
.arg(&key)
|
||||
.query::<Option<String>>(redis_conn)?
|
||||
{
|
||||
Ok(serde_json::from_str(&s)?)
|
||||
} else {
|
||||
let post_tag_post_ids = db::schema::post_tags::table
|
||||
.select(db::schema::post_tags::post_id)
|
||||
.filter(db::schema::post_tags::tag_id.eq(id))
|
||||
.load::<i32>(db_conn)?;
|
||||
|
||||
let posts: Vec<web::templates::PostIndexPost> = db::schema::posts::table
|
||||
.select((
|
||||
db::schema::posts::id,
|
||||
db::schema::posts::name,
|
||||
db::schema::posts::slug,
|
||||
db::schema::posts::description,
|
||||
db::schema::posts::published_at,
|
||||
db::schema::posts::edited_at,
|
||||
))
|
||||
.filter(db::schema::posts::id.eq_any(post_tag_post_ids))
|
||||
.filter(db::schema::posts::active)
|
||||
.order(db::schema::posts::published_at.desc())
|
||||
.load::<(i32, String, String, String, NaiveDate, Option<NaiveDate>)>(db_conn)?
|
||||
.into_iter()
|
||||
.map(
|
||||
|p: (i32, String, String, String, NaiveDate, Option<NaiveDate>)| -> Result<_> {
|
||||
let (id, name, slug, description, published_at, edited_at) = p;
|
||||
let tags = web::get_tags_by_post(id, db_conn)?;
|
||||
|
||||
Ok(web::templates::PostIndexPost {
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
published_at,
|
||||
edited_at,
|
||||
tags,
|
||||
})
|
||||
},
|
||||
)
|
||||
.collect::<Result<_>>()?;
|
||||
|
||||
redis::cmd("SET")
|
||||
.arg(&key)
|
||||
.arg(serde_json::to_string(&posts)?)
|
||||
.query(redis_conn)?;
|
||||
redis::cmd("EXPIRE")
|
||||
.arg(key)
|
||||
.arg(config::CONFIG.cache_ttl)
|
||||
.query(redis_conn)?;
|
||||
|
||||
Ok(posts)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ pub struct Config {
|
|||
#[envconfig(from = "BACKEND_REDIS_URL")]
|
||||
pub redis_url: String,
|
||||
|
||||
#[envconfig(from = "BACKEND_CACHE_POST_CONTENT_TTL")]
|
||||
pub cache_post_content_ttl: usize,
|
||||
#[envconfig(from = "BACKEND_CACHE_TTL")]
|
||||
pub cache_ttl: usize,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
|
|
|
@ -10,8 +10,11 @@ pub mod models;
|
|||
pub mod schema;
|
||||
|
||||
pub type DbPool = Pool<ConnectionManager<PgConnection>>;
|
||||
pub type Connection = PgConnection;
|
||||
|
||||
pub fn establish_connection() -> ConnectionResult<PgConnection> {
|
||||
use diesel::Connection;
|
||||
|
||||
PgConnection::establish(&config::CONFIG.db_url)
|
||||
}
|
||||
|
||||
|
|
|
@ -52,3 +52,20 @@ pub struct UpdatePost<'a> {
|
|||
pub edited_at: Option<Option<NaiveDate>>,
|
||||
pub active: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Associations, Identifiable, Queryable, Debug)]
|
||||
#[diesel(belongs_to(Post))]
|
||||
#[diesel(belongs_to(Tag))]
|
||||
#[diesel(table_name = schema::post_tags)]
|
||||
pub struct PostTag {
|
||||
pub id: i32,
|
||||
pub post_id: i32,
|
||||
pub tag_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Insertable, Debug)]
|
||||
#[diesel(table_name = schema::post_tags)]
|
||||
pub struct NewPostTag {
|
||||
pub post_id: i32,
|
||||
pub tag_id: i32,
|
||||
}
|
||||
|
|
|
@ -17,3 +17,11 @@ diesel::table! {
|
|||
active -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
post_tags {
|
||||
id -> Integer,
|
||||
post_id -> Integer,
|
||||
tag_id -> Integer,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
use actix_web::{get, http, web, HttpResponse};
|
||||
use actix_web_static_files::ResourceFiles;
|
||||
use anyhow::Result;
|
||||
use askama_actix::TemplateToResponse;
|
||||
use diesel::prelude::*;
|
||||
use r2d2_redis::redis;
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use crate::{cache, config, db, markdown};
|
||||
use crate::{cache, db};
|
||||
|
||||
pub mod templates;
|
||||
|
||||
|
@ -24,27 +23,6 @@ async fn not_found() -> HttpResponse {
|
|||
resp
|
||||
}
|
||||
|
||||
#[get("/posts")]
|
||||
async fn posts(db_pool: web::Data<db::DbPool>) -> HttpResponse {
|
||||
let db_conn = &mut match db_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
let posts = match db::schema::posts::table
|
||||
.filter(db::schema::posts::active)
|
||||
.order(db::schema::posts::published_at.desc())
|
||||
.load::<db::models::Post>(db_conn)
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
return HttpResponse::InternalServerError().body(format!("{:?}", e));
|
||||
}
|
||||
};
|
||||
|
||||
templates::Posts { posts }.to_response()
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
templates::Index.to_response()
|
||||
|
@ -55,6 +33,43 @@ async fn about() -> HttpResponse {
|
|||
templates::About.to_response()
|
||||
}
|
||||
|
||||
pub fn get_tags_by_post(id: i32, db_conn: &mut diesel::PgConnection) -> Result<Vec<String>> {
|
||||
Ok(db::schema::tags::table
|
||||
.select(db::schema::tags::name)
|
||||
.filter(
|
||||
db::schema::tags::id.eq_any(
|
||||
db::schema::post_tags::table
|
||||
.select(db::schema::post_tags::tag_id)
|
||||
.filter(db::schema::post_tags::post_id.eq(id))
|
||||
.load::<i32>(db_conn)?,
|
||||
),
|
||||
)
|
||||
.order(db::schema::tags::name)
|
||||
.load::<String>(db_conn)?)
|
||||
}
|
||||
|
||||
#[get("/posts")]
|
||||
async fn posts(
|
||||
db_pool: web::Data<db::DbPool>,
|
||||
redis_pool: web::Data<cache::RedisPool>,
|
||||
) -> HttpResponse {
|
||||
let db_conn = &mut match db_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
let redis_conn = &mut match redis_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
let posts = match cache::cache_posts(db_conn, redis_conn) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
templates::Posts { posts }.to_response()
|
||||
}
|
||||
|
||||
#[get("/posts/{slug}")]
|
||||
async fn post_by_slug(
|
||||
db_pool: web::Data<db::DbPool>,
|
||||
|
@ -72,89 +87,181 @@ async fn post_by_slug(
|
|||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
let post_stripped: Option<(i32, String)> = match db::schema::posts::table
|
||||
.select((db::schema::posts::id, db::schema::posts::name))
|
||||
if let Some(post_id) = match db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.filter(db::schema::posts::slug.eq(&slug))
|
||||
.filter(db::schema::posts::active)
|
||||
.get_result::<(i32, String)>(db_conn)
|
||||
.first::<i32>(db_conn)
|
||||
.optional()
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
return HttpResponse::InternalServerError().body(format!("{:?}", e));
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
} {
|
||||
let post = match cache::cache_post(post_id, db_conn, redis_conn) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
templates::PostBySlug { post }.to_response()
|
||||
} else {
|
||||
let mut resp = templates::StatusCode {
|
||||
status_code: http::StatusCode::NOT_FOUND,
|
||||
message: Some("this post does not exists... yet".to_string()),
|
||||
}
|
||||
.to_response();
|
||||
*resp.status_mut() = http::StatusCode::NOT_FOUND;
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
// let post_stripped: Option<(i32, String, NaiveDate, Option<NaiveDate>)> =
|
||||
// match db::schema::posts::table
|
||||
// .select((
|
||||
// db::schema::posts::id,
|
||||
// db::schema::posts::name,
|
||||
// db::schema::posts::published_at,
|
||||
// db::schema::posts::edited_at,
|
||||
// ))
|
||||
// .filter(db::schema::posts::slug.eq(&slug))
|
||||
// .filter(db::schema::posts::active)
|
||||
// .first::<(i32, String, NaiveDate, Option<NaiveDate>)>(db_conn)
|
||||
// .optional()
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
// };
|
||||
//
|
||||
// match post_stripped {
|
||||
// Some(stripped) => {
|
||||
// let (stripped_id, stripped_name, stripped_published_at, stripped_edited_at) = stripped;
|
||||
|
||||
// let key = cache::keys::post_content(stripped_id);
|
||||
|
||||
// match match redis::cmd("GET")
|
||||
// .arg(&key)
|
||||
// .query::<Option<String>>(redis_conn.deref_mut())
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
// } {
|
||||
// Some(s) => {
|
||||
// let tags = match get_tags_by_post(stripped_id, db_conn) {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
|
||||
// templates::PostBySlug {
|
||||
// name: stripped_name,
|
||||
// slug,
|
||||
// published_at: stripped_published_at,
|
||||
// edited_at: stripped_edited_at,
|
||||
// tags,
|
||||
// content: s,
|
||||
// }
|
||||
// }
|
||||
// .to_response(),
|
||||
// None => {
|
||||
// let post = match db::schema::posts::table
|
||||
// .filter(db::schema::posts::id.eq(stripped_id))
|
||||
// .first::<db::models::Post>(db_conn)
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
|
||||
// let html = markdown::to_html(&post.content);
|
||||
|
||||
// match redis::cmd("SET")
|
||||
// .arg(&key)
|
||||
// .arg(&html)
|
||||
// .query::<Option<String>>(redis_conn.deref_mut())
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
// if let Err(e) = redis::cmd("EXPIRE")
|
||||
// .arg(key)
|
||||
// .arg(config::CONFIG.cache_ttl)
|
||||
// .query::<()>(redis_conn.deref_mut())
|
||||
// {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e));
|
||||
// }
|
||||
|
||||
// let tags = match get_tags_by_post(stripped_id, db_conn) {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
|
||||
// templates::PostBySlug {
|
||||
// name: post.name,
|
||||
// slug: post.slug,
|
||||
// published_at: stripped_published_at,
|
||||
// edited_at: stripped_edited_at,
|
||||
// tags,
|
||||
// content: html,
|
||||
// }
|
||||
// .to_response()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// None => {
|
||||
// let mut resp = templates::StatusCode {
|
||||
// status_code: http::StatusCode::NOT_FOUND,
|
||||
// message: Some("this post does not exists... yet".to_string()),
|
||||
// }
|
||||
// .to_response();
|
||||
// *resp.status_mut() = http::StatusCode::NOT_FOUND;
|
||||
|
||||
// resp
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
#[get("/tags/{name}")]
|
||||
async fn tag_by_name(
|
||||
db_pool: web::Data<db::DbPool>,
|
||||
redis_pool: web::Data<cache::RedisPool>,
|
||||
path: web::Path<String>,
|
||||
) -> HttpResponse {
|
||||
const MESSAGE: &str = "this post does not exists... yet";
|
||||
|
||||
let name = path.into_inner();
|
||||
|
||||
let db_conn = &mut match db_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
let redis_conn = &mut match redis_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
match post_stripped {
|
||||
Some(stripped) => {
|
||||
let (stripped_id, stripped_name) = stripped;
|
||||
let tag_id = match db::schema::tags::table
|
||||
.select(db::schema::tags::id)
|
||||
.filter(db::schema::tags::name.eq(&name))
|
||||
.first::<i32>(db_conn)
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
let posts_cache = match cache::cache_tag_posts(tag_id, db_conn, redis_conn) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
let key = cache::keys::post_content(stripped_id);
|
||||
|
||||
match match redis::cmd("GET")
|
||||
.arg(&key)
|
||||
.query::<Option<String>>(redis_conn.deref_mut())
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
} {
|
||||
Some(s) => templates::PostBySlug {
|
||||
name: stripped_name,
|
||||
slug,
|
||||
content: s,
|
||||
}
|
||||
.to_response(),
|
||||
None => {
|
||||
let post = match db::schema::posts::table
|
||||
.filter(db::schema::posts::id.eq(stripped_id))
|
||||
.first::<db::models::Post>(db_conn)
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
return HttpResponse::InternalServerError().body(format!("{:?}", e));
|
||||
}
|
||||
};
|
||||
|
||||
let html = markdown::to_html(&post.content);
|
||||
|
||||
match redis::cmd("SET")
|
||||
.arg(&key)
|
||||
.arg(&html)
|
||||
.query::<Option<String>>(redis_conn.deref_mut())
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
}
|
||||
};
|
||||
if let Err(e) = redis::cmd("EXPIRE")
|
||||
.arg(key)
|
||||
.arg(config::CONFIG.cache_post_content_ttl)
|
||||
.query::<()>(redis_conn.deref_mut())
|
||||
{
|
||||
return HttpResponse::InternalServerError().body(format!("{:?}", e));
|
||||
}
|
||||
|
||||
templates::PostBySlug {
|
||||
name: post.name,
|
||||
slug: post.slug,
|
||||
content: html,
|
||||
}
|
||||
.to_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let mut resp = templates::StatusCode {
|
||||
status_code: http::StatusCode::NOT_FOUND,
|
||||
message: Some("this post does not exists... yet".to_string()),
|
||||
}
|
||||
.to_response();
|
||||
*resp.status_mut() = http::StatusCode::NOT_FOUND;
|
||||
|
||||
resp
|
||||
}
|
||||
templates::TagByName {
|
||||
name,
|
||||
posts: posts_cache,
|
||||
}
|
||||
.to_response()
|
||||
}
|
||||
|
||||
fn setup_routes(cfg: &mut web::ServiceConfig) {
|
||||
|
@ -165,6 +272,7 @@ fn setup_routes(cfg: &mut web::ServiceConfig) {
|
|||
.service(posts)
|
||||
.service(ResourceFiles::new("/static", generated))
|
||||
.service(post_by_slug)
|
||||
.service(tag_by_name)
|
||||
.default_service(web::route().to(not_found));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
use actix_web::http;
|
||||
use askama_actix::Template;
|
||||
use chrono::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::db;
|
||||
use crate::cache;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "status_code.html")]
|
||||
|
@ -18,16 +20,37 @@ pub struct Index;
|
|||
#[template(path = "web/about.html")]
|
||||
pub struct About;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct PostIndexPost {
|
||||
pub name: String,
|
||||
pub slug: String,
|
||||
pub description: String,
|
||||
pub published_at: NaiveDate,
|
||||
pub edited_at: Option<NaiveDate>,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "web/posts/index.html")]
|
||||
pub struct Posts {
|
||||
pub posts: Vec<db::models::Post>,
|
||||
pub posts: Vec<PostIndexPost>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "web/posts/{slug}.html")]
|
||||
pub struct PostBySlug {
|
||||
pub name: String,
|
||||
pub slug: String,
|
||||
pub content: String,
|
||||
// pub name: String,
|
||||
// pub slug: String,
|
||||
// pub published_at: NaiveDate,
|
||||
// pub edited_at: Option<NaiveDate>,
|
||||
// pub tags: Vec<String>,
|
||||
// pub content: String,
|
||||
pub post: cache::Post,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "web/tags/{name}.html")]
|
||||
pub struct TagByName {
|
||||
pub name: String,
|
||||
pub posts: Vec<PostIndexPost>,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue