Skip to content

Commit

Permalink
Minor CSS Fixes + Limit Content Length (#23)
Browse files Browse the repository at this point in the history
* get everything building and running on a new computer

* fix some CSS issues, trim post length in previews

* cargo fmt
  • Loading branch information
effward authored Apr 27, 2024
1 parent f67c807 commit ec51331
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# will have compiled files and executables
debug/
target/
.cargo/

# These are backup files generated by rustfmt
**/*.rs.bk
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ cargo fmt
Set the following environment variables:
- DATABASE_URL (Aiven shared-sql instance)
- HMAC_KEY (Generate 512 bit key [here](https://generate-random.org/api-key-generator/512-bit/mixed-numbers))
- REDIS_URI (redis://127.0.0.1:6379)
- `REDIS_URI=redis://127.0.0.1:6379`
- `EFFWARD_DEV_ENVIRONMENT=dev`

## Build
Build with:
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
version: '3.8'

services:

redis:
image: redis:latest
ports:
Expand Down
33 changes: 17 additions & 16 deletions src/entities/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ impl Cache {
decode_and_unwrap_ref(encoded)
}

fn remove<T>(&self, key: String) -> Result<Option<T>, EntityError>
where
for<'a> T: Deserialize<'a> + Serialize + PartialEq + Clone + std::fmt::Debug,
{
let encoded = self.map.remove(&key);
// TODO: use in invalidation
// fn remove<T>(&self, key: String) -> Result<Option<T>, EntityError>
// where
// for<'a> T: Deserialize<'a> + Serialize + PartialEq + Clone + std::fmt::Debug,
// {
// let encoded = self.map.remove(&key);

decode_and_unwrap(encoded)
}
// decode_and_unwrap(encoded)
// }

// TODO: expand key builder to allow for tags/collections
pub async fn get_cached<T, Fut, F, FKey>(
Expand Down Expand Up @@ -147,15 +148,15 @@ where
}
}

fn decode_and_unwrap<T>(encoded: Option<(String, Vec<u8>)>) -> Result<Option<T>, EntityError>
where
for<'a> T: Deserialize<'a> + Serialize + PartialEq + Clone + std::fmt::Debug,
{
match encoded {
Some((_, encoded)) => do_decode_and_unwrap(&encoded),
None => Ok(None),
}
}
// fn decode_and_unwrap<T>(encoded: Option<(String, Vec<u8>)>) -> Result<Option<T>, EntityError>
// where
// for<'a> T: Deserialize<'a> + Serialize + PartialEq + Clone + std::fmt::Debug,
// {
// match encoded {
// Some((_, encoded)) => do_decode_and_unwrap(&encoded),
// None => Ok(None),
// }
// }

fn decode_and_unwrap_ref<T>(encoded: Option<Ref<String, Vec<u8>>>) -> Result<Option<T>, EntityError>
where
Expand Down
2 changes: 0 additions & 2 deletions src/entities/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ mod post_store;
pub use post::Post;
pub use post_cache::CachedPostStore;
pub use post_sql::SqlPostStore;
pub use post_sql::MAX_TITLE_LENGTH;
pub use post_sql::MIN_TITLE_LENGTH;
pub use post_store::PostStore;
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
//! # assert_eq!(x, 7);
//! ```

use log::error;
// TODO: ^^^ Update ^^^

use std::{env, str::FromStr};

use effward_dev::server::{Application, Environment, ServerError};
Expand All @@ -15,15 +16,15 @@ async fn main() -> Result<(), ServerError> {
let env = match env::var("EFFWARD_DEV_ENVIRONMENT") {
Ok(e) => Environment::from_str(&e)?,
Err(err) => {
error!("🔥 EFFWARD_DEV_ENVIRONMENT not set: {}", err);
println!("🔥 EFFWARD_DEV_ENVIRONMENT not set: {}", err);
std::process::exit(1);
}
};

let db_url = match env::var("DATABASE_URL") {
Ok(url) => url,
Err(e) => {
error!(
println!(
"🔥 DATABASE_URL environment variable required, but not set: {}",
e
);
Expand All @@ -34,15 +35,15 @@ async fn main() -> Result<(), ServerError> {
let hmac_key = match env::var("HMAC_KEY") {
Ok(key) => key,
Err(e) => {
error!("🔥 HMAC_KEY environment variable is not set! Error: {}", e);
println!("🔥 HMAC_KEY environment variable is not set! Error: {}", e);
std::process::exit(1);
}
};

let redis_uri = match env::var("REDIS_URI") {
Ok(uri) => uri,
Err(e) => {
error!("🔥 REDIS_URI environment variable is not set! Error: {}", e);
println!("🔥 REDIS_URI environment variable is not set! Error: {}", e);
std::process::exit(1);
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/routes/index/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};

const POSTS_PER_PAGE: u8 = 2;
const MAX_CONTENT_PREVIEW_LENGTH: usize = 1500;

pub async fn index(
session: TypedSession,
Expand All @@ -36,7 +37,8 @@ pub async fn index(

let mut posts: Vec<PostSummary> = vec![];
for post_entity in post_entities.iter() {
match models::translate_post_summary(post_entity, &stores).await {
match models::translate_post_summary(post_entity, &stores, MAX_CONTENT_PREVIEW_LENGTH).await
{
Ok(post_summary) => {
posts.push(post_summary);
}
Expand Down
1 change: 0 additions & 1 deletion src/routes/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod utils;

pub use comment::CommentModel;
pub use post_model::translate_post;
pub use post_model::PostModel;
pub use post_summary::translate_post_summary;
pub use post_summary::PostSummary;
pub use user_model::UserModel;
2 changes: 1 addition & 1 deletion src/routes/models/post_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct PostModel {
}

pub async fn translate_post(post: &Post, stores: &EntityStores) -> Result<PostModel, EntityError> {
let summary = translate_post_summary(post, stores).await?;
let summary = translate_post_summary(post, stores, 0).await?;

let comment_entities = stores
.comment_store
Expand Down
9 changes: 8 additions & 1 deletion src/routes/models/post_summary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{DateTime, Utc};
use serde::Serialize;
use substring::Substring;

use crate::entities::{
comment::CommentStore, content::ContentStore, post::Post, user::UserStore, EntityError,
Expand All @@ -23,14 +24,20 @@ pub struct PostSummary {
pub async fn translate_post_summary(
post: &Post,
stores: &EntityStores,
max_content_len: usize,
) -> Result<PostSummary, EntityError> {
let author_entity = stores.user_store.get_by_id(post.author_id).await?;
let author = UserModel::from(author_entity);

let content = match post.content_id {
Some(id) => {
let content = stores.content_store.get_by_id(id).await?;
Some(content.body_html)
let mut html = content.body_html;
if max_content_len > 0 && html.len() > max_content_len {
html = html.substring(0, max_content_len).to_string();
html.push_str("...");
}
Some(html)
}
None => None,
};
Expand Down
4 changes: 3 additions & 1 deletion src/routes/posts/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{

const POSTS_PER_PAGE: u8 = 15;
const HERO_BG_CLASS: &str = "hero-bg-posts";
const MAX_CONTENT_PREVIEW_LENGTH: usize = 2048;

pub async fn posts(
session: TypedSession,
Expand Down Expand Up @@ -45,7 +46,8 @@ pub async fn posts(

let mut posts: Vec<PostSummary> = vec![];
for post_entity in post_entities.iter() {
match models::translate_post_summary(post_entity, &stores).await {
match models::translate_post_summary(post_entity, &stores, MAX_CONTENT_PREVIEW_LENGTH).await
{
Ok(post_summary) => {
posts.push(post_summary);
}
Expand Down
4 changes: 2 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@
}

article.media a, article.media a p {
color: #4a4a4a;
color: #e8505b;
}

article.media a:hover {
color: #e8505b;
color: #b7343e;
}

article.media strong:hover {
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
</div>
<div class="column"></div>
<div class="column is-narrow">
<div class="column is-half">
<div class="section p-3">
<p class="title is-6 mb-3">recent posts</p>
{% for post in posts %}
Expand Down

0 comments on commit ec51331

Please sign in to comment.