Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: create workspace for better compile performance #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,148 changes: 577 additions & 571 deletions Cargo.lock

Large diffs are not rendered by default.

78 changes: 31 additions & 47 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
[package]
name = "blog"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
[workspace]
resolver = "2"
members = ["app", "frontend", "markdown", "server"]

[dependencies]
axum = { version = "0.7", optional = true, features = ["macros"] }
[workspace.dependencies]
axum = { version = "0.7", features = ["macros"] }
console_error_panic_hook = "0.1"
leptos = { version = "0.7.3", features = ["rustls", "nightly"] }
leptos_axum = { version = "0.7.3", optional = true }
leptos_axum = { version = "0.7.3" }
leptos_meta = { version = "0.7.3" }
leptos_router = { version = "0.7.3", features = ["nightly"] }
tokio = { version = "1", features = ["rt-multi-thread"], optional = true }
tower = { version = "0.5.1", optional = true }
tokio = { version = "1", features = ["rt-multi-thread"] }
tower = { version = "0.5.1" }
tower-http = { version = "0.6.2", features = [
"fs",
"trace",
Expand All @@ -23,25 +19,22 @@ tower-http = { version = "0.6.2", features = [
"compression-deflate",
"compression-zstd",
"compression-gzip",
], optional = true }
] }
wasm-bindgen = "0.2.96"
thiserror = "1"
tracing = { version = "0.1", optional = true }
tracing = { version = "0.1" }
http = "1"
surrealdb = { version = "2.0.1", features = ["protocol-http"] }
chrono = "0.4.38"
serde = { version = "1.0.204", features = ["derive"] }
dotenvy = "0.15.7"
syntect = { version = "5.2.0", optional = true }
regex = { version = "1.10.6", optional = true }
pulldown-cmark = { version = "0.12.0", optional = true }
rss = { version = "2.0.9", optional = true }
tracing-subscriber = { version = "0.3.18", optional = true }
katex = { version = "0.4.6", optional = true, default-features = false, features = [
"duktape",
] }
regex = { version = "1.10.6" }
pulldown-cmark = { version = "0.12.0" }
rss = { version = "2.0.9" }
tracing-subscriber = { version = "0.3.18" }
katex = { version = "0.4.6", default-features = false, features = ["duktape"] }
web-sys = "0.3.72"
lettre = { version = "0.11.9", default-features = false, optional = true, features = [
lettre = { version = "0.11.9", default-features = false, features = [
"builder",
"smtp-transport",
"rustls-tls",
Expand All @@ -50,27 +43,9 @@ lettre = { version = "0.11.9", default-features = false, optional = true, featur
] }
leptos_icons = "0.4.0"
icondata = "0.5.0"

[features]
hydrate = ["leptos/hydrate"]
ssr = [
"dep:axum",
"dep:tokio",
"dep:tower",
"dep:tower-http",
"dep:leptos_axum",
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"dep:tracing",
"dep:syntect",
"dep:regex",
"dep:pulldown-cmark",
"dep:rss",
"dep:tracing-subscriber",
"dep:katex",
"dep:lettre",
]
syntect = "5.2.0"
console_log = "1"
log = "0.4.20"

# Defines a size-optimized profile for the WASM bundle in release mode
[profile.wasm-release]
Expand All @@ -80,7 +55,16 @@ lto = true
codegen-units = 1
panic = "abort"

[package.metadata.leptos]
[[workspace.metadata.leptos]]
# this name is used for the wasm, js and css file names
name = "blog"

# the package in the workspace that contains the server binary (binary crate)
bin-package = "server"

# the package in the workspace that contains the frontend wasm binary (library crate)
lib-package = "frontend"

# The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name
output-name = "blog"

Expand Down Expand Up @@ -123,7 +107,7 @@ env = "DEV"
# The features to use when compiling the bin target
#
# Optional. Can be over-ridden with the command line parameter --bin-features
bin-features = ["ssr"]
bin-features = []

# If the --no-default-features flag should be used when compiling the bin target
#
Expand All @@ -133,7 +117,7 @@ bin-default-features = false
# The features to use when compiling the lib target
#
# Optional. Can be over-ridden with the command line parameter --lib-features
lib-features = ["hydrate"]
lib-features = []

# If the --no-default-features flag should be used when compiling the lib target
#
Expand Down
27 changes: 27 additions & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "app"
version = "0.1.0"
edition = "2021"

[dependencies]
markdown = { path = "../markdown", default-features = false, features = ["ssr"], optional = true}
axum = {workspace = true, optional = true}
chrono.workspace = true
leptos.workspace = true
leptos_meta.workspace = true
leptos_router.workspace = true
leptos_axum = { workspace = true, optional = true }
leptos_icons.workspace = true
icondata.workspace = true
http.workspace = true
lettre = {workspace = true, optional = true}
serde.workspace = true
surrealdb.workspace = true
thiserror.workspace = true
web-sys.workspace = true
tracing ={ workspace = true, optional = true }

[features]
hydrate = ["leptos/hydrate"]
ssr = ["leptos/ssr", "leptos_meta/ssr", "leptos_router/ssr", "dep:leptos_axum", "dep:axum", "dep:lettre", "dep:tracing", "dep:markdown"]

27 changes: 14 additions & 13 deletions src/ssr/api.rs → app/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::collections::BTreeMap;
use leptos::prelude::{server, ServerFnError};
use serde::{Deserialize, Serialize};

use crate::ssr::types::{Post, Reference};
use crate::types::{Post, Reference};

#[server(endpoint = "/posts")]
pub async fn select_posts(#[server(default)] tags: Vec<String>) -> Result<Vec<Post>, ServerFnError> {
use crate::ssr::app_state::AppState;
use crate::types::AppState;
use chrono::{DateTime, Utc};
use leptos::prelude::expect_context;

Expand All @@ -34,15 +34,15 @@ pub async fn select_posts(#[server(default)] tags: Vec<String>) -> Result<Vec<Po
.with_timezone(&Utc);
let naive_date = date_time.date_naive();
let formatted_date = naive_date.format("%b %-d, %Y").to_string();
post.created_at = formatted_date.into();
post.created_at = formatted_date;
});

Ok(posts)
}

#[server(endpoint = "/tags")]
pub async fn select_tags() -> Result<BTreeMap<String, usize>, ServerFnError> {
use crate::ssr::app_state::AppState;
use crate::types::AppState;
use leptos::prelude::expect_context;

let AppState { db, .. } = expect_context::<AppState>();
Expand Down Expand Up @@ -70,14 +70,15 @@ pub async fn select_tags() -> Result<BTreeMap<String, usize>, ServerFnError> {

#[server(endpoint = "/post")]
pub async fn select_post(slug: String) -> Result<Post, ServerFnError> {
use super::server_utils::process_markdown;
use crate::ssr::app_state::AppState;
use crate::types::AppState;
use chrono::{DateTime, Utc};
use leptos::prelude::expect_context;
use markdown::process_markdown;

let AppState { db, .. } = expect_context::<AppState>();

let query = format!(r#"SELECT *, author.* from post WHERE slug = "{slug}""#);
println!("{:?}", query);
let query = db.query(&query).await;

if let Err(e) = query {
Expand All @@ -90,15 +91,15 @@ pub async fn select_post(slug: String) -> Result<Post, ServerFnError> {
let date_time = DateTime::parse_from_rfc3339(&post.created_at)?.with_timezone(&Utc);
let naive_date = date_time.date_naive();
let formatted_date = naive_date.format("%b %-d").to_string();
post.created_at = formatted_date.into();
post.body = process_markdown(post.body.to_string()).await?.into();
post.created_at = formatted_date;
post.body = process_markdown(post.body.to_string()).await?;

Ok(post)
}

#[server(endpoint = "/increment_views")]
pub async fn increment_views(id: String) -> Result<(), ServerFnError> {
use crate::ssr::app_state::AppState;
use crate::types::AppState;
use leptos::prelude::expect_context;

let AppState { db, .. } = expect_context::<AppState>();
Expand Down Expand Up @@ -144,25 +145,25 @@ pub async fn hire_us(data: HireUsRequest) -> Result<(), ServerFnError> {
match mailer.send(email).await {
Ok(_) => {
tracing::info!("Email sent successfully");
return Ok(());
Ok(())
}
Err(e) => {
tracing::error!("Failed to send email: {:?}", e);
return Err(ServerFnError::from(e));
Err(ServerFnError::from(e))
}
}
}

#[server(endpoint = "/references")]
pub async fn select_references() -> Result<Vec<Reference>, ServerFnError> {
use crate::ssr::app_state::AppState;
use crate::types::AppState;
use leptos::prelude::expect_context;

let AppState { db, .. } = expect_context::<AppState>();

let query = "SELECT * from reference WHERE is_published = true ORDER BY created_at DESC;";
let query = db.query(query).await;

println!("{:?}", query);
if let Err(e) = query {
return Err(ServerFnError::from(e));
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn component(outside_errors: Option<Errors>, errors: Option<RwSignal<Errors>

div().child((
h1().class("text-xl tracking-widest text-gray-400 uppercase").child(
format!("{}| {}", error_code.to_string(), error_string),
format!("{}| {}", error_code, error_string),
),
a()
.href("/")
Expand Down
2 changes: 1 addition & 1 deletion src/components/header.rs → app/src/components/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::components::icons;
use crate::icons;
use leptos::{
ev,
html::{a, div, header},
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pages/hireus.rs → app/src/hireus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use leptos::{
svg::{path, svg},
};

use crate::ssr::api::{hire_us, HireUsRequest};
use crate::api::{hire_us, HireUsRequest};

pub fn component() -> impl IntoView {
let state = RwSignal::new(HireUsRequest::default());
Expand Down
6 changes: 4 additions & 2 deletions src/pages/home.rs → app/src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use leptos_icons::{Icon, IconProps};
use leptos_meta::{Title, TitleProps};
use leptos_router::components::{AProps, A};

use crate::components::loader;
use crate::ssr::api::{select_posts, select_tags};
use crate::{
api::{select_posts, select_tags},
components::loader,
};

pub fn component() -> impl IntoView {
let selected_tags = RwSignal::new(Vec::<String>::new());
Expand Down
13 changes: 9 additions & 4 deletions src/app.rs → app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
components::{error_template, header, icons},
pages::{hireus, home, post, references},
};
use crate::components::{error_template, header, icons};
use chrono::{Datelike, Utc};
use leptos::{
html::{a, body, div, footer, head, html, main, meta, p},
Expand All @@ -13,6 +10,14 @@ use leptos_router::{
ParamSegment, SsrMode, StaticSegment,
};

pub mod api;
mod components;
mod hireus;
mod home;
mod post;
mod references;
pub mod types;

pub fn shell(options: LeptosOptions) -> impl IntoView {
let html_ = html().lang("en").child((
head().child((
Expand Down
8 changes: 5 additions & 3 deletions src/pages/post.rs → app/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use leptos::{ev, prelude::*};
use leptos_meta::*;
use leptos_router::hooks::use_params_map;

use crate::components::loader;
use crate::ssr::api::{increment_views, select_post};
use crate::ssr::types::Post;
use crate::{
api::{increment_views, select_post},
components::loader,
types::Post,
};

pub fn component() -> impl IntoView {
let params = use_params_map();
Expand Down
4 changes: 2 additions & 2 deletions src/pages/references.rs → app/src/references.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ssr::api::select_references;
use crate::api::select_references;
use leptos::{
html::{div, h1, h3, p, section, span},
prelude::*,
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn component() -> impl IntoView {
r.tech_stack
.clone()
.into_iter()
.zip(r.teck_stack_percentage.clone().into_iter())
.zip(r.teck_stack_percentage.clone())
.collect::<Vec<_>>()
})
.key(|tech| tech.0.to_string())
Expand Down
14 changes: 14 additions & 0 deletions src/ssr/types.rs → app/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;

#[cfg(feature = "ssr")]
use axum::extract::FromRef;
#[cfg(feature = "ssr")]
use leptos::config::LeptosOptions;
#[cfg(feature = "ssr")]
use surrealdb::{engine::remote::http::Client, Surreal};

#[cfg(feature = "ssr")]
#[derive(FromRef, Debug, Clone)]
pub struct AppState {
pub db: Surreal<Client>,
pub leptos_options: LeptosOptions,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Author {
pub id: Thing,
Expand Down
18 changes: 18 additions & 0 deletions frontend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "frontend"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
app = { path = "../app", default-features = false, features = [
"hydrate",
] }
leptos = { workspace = true, features = ["hydrate"] }

console_error_panic_hook.workspace = true
console_log.workspace = true
log.workspace = true
wasm-bindgen.workspace = true
9 changes: 9 additions & 0 deletions frontend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
use app::*;
// initializes logging using the `log` crate
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();

leptos::mount::hydrate_body(component);
}
Loading