forked from slonk/bingus-blog
main #1
2 changed files with 13 additions and 17 deletions
18
src/main.rs
18
src/main.rs
|
@ -316,29 +316,23 @@ async fn main() -> eyre::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write cache to file
|
// write cache to file
|
||||||
let AppState { config, posts } = Arc::<AppState>::try_unwrap(state).unwrap_or_else(|state| {
|
let config = &state.config;
|
||||||
warn!("couldn't unwrap Arc over AppState, more than one strong reference exists for Arc. cloning instead");
|
let posts = &state.posts;
|
||||||
// TODO: only do this when persistence is enabled
|
|
||||||
// first check config from inside the arc, then try unwrap
|
|
||||||
AppState::clone(state.as_ref())
|
|
||||||
});
|
|
||||||
if config.cache.enable
|
if config.cache.enable
|
||||||
&& config.cache.persistence
|
&& config.cache.persistence
|
||||||
&& let Some(cache) = posts.into_cache()
|
&& let Some(cache) = posts.cache()
|
||||||
{
|
{
|
||||||
let path = &config.cache.file;
|
let path = &config.cache.file;
|
||||||
let serialized = bitcode::serialize(&cache).context("failed to serialize cache")?;
|
let serialized = bitcode::serialize(cache).context("failed to serialize cache")?;
|
||||||
let mut cache_file = tokio::fs::File::create(path)
|
let mut cache_file = tokio::fs::File::create(path)
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("failed to open cache at {}", path.display()))?;
|
.with_context(|| format!("failed to open cache at {}", path.display()))?;
|
||||||
|
let compression_level = config.cache.compression_level;
|
||||||
if config.cache.compress {
|
if config.cache.compress {
|
||||||
let cache_file = cache_file.into_std().await;
|
let cache_file = cache_file.into_std().await;
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
std::io::Write::write_all(
|
std::io::Write::write_all(
|
||||||
&mut zstd::stream::write::Encoder::new(
|
&mut zstd::stream::write::Encoder::new(cache_file, compression_level)?
|
||||||
cache_file,
|
|
||||||
config.cache.compression_level,
|
|
||||||
)?
|
|
||||||
.auto_finish(),
|
.auto_finish(),
|
||||||
&serialized,
|
&serialized,
|
||||||
)
|
)
|
||||||
|
|
|
@ -151,7 +151,7 @@ impl PostManager {
|
||||||
Ok((metadata, post, (parsing, rendering)))
|
Ok((metadata, post, (parsing, rendering)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list_posts(
|
pub async fn get_all_post_metadata_filtered(
|
||||||
&self,
|
&self,
|
||||||
filter: impl Fn(&PostMetadata) -> bool,
|
filter: impl Fn(&PostMetadata) -> bool,
|
||||||
) -> Result<Vec<PostMetadata>, PostError> {
|
) -> Result<Vec<PostMetadata>, PostError> {
|
||||||
|
@ -206,7 +206,9 @@ impl PostManager {
|
||||||
tag: Option<&String>,
|
tag: Option<&String>,
|
||||||
) -> Result<Vec<PostMetadata>, PostError> {
|
) -> Result<Vec<PostMetadata>, PostError> {
|
||||||
let mut posts = self
|
let mut posts = self
|
||||||
.list_posts(|metadata| !tag.is_some_and(|tag| !metadata.tags.contains(tag)))
|
.get_all_post_metadata_filtered(|metadata| {
|
||||||
|
!tag.is_some_and(|tag| !metadata.tags.contains(tag))
|
||||||
|
})
|
||||||
.await?;
|
.await?;
|
||||||
posts.sort_unstable_by_key(|metadata| metadata.created_at.unwrap_or_default());
|
posts.sort_unstable_by_key(|metadata| metadata.created_at.unwrap_or_default());
|
||||||
|
|
||||||
|
@ -257,8 +259,8 @@ impl PostManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_cache(self) -> Option<Cache> {
|
pub fn cache(&self) -> Option<&Cache> {
|
||||||
self.cache
|
self.cache.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cleanup(&self) {
|
pub async fn cleanup(&self) {
|
||||||
|
|
Loading…
Reference in a new issue