Skip to content

Commit

Permalink
refactor: ♻️ moved logic into core package from example
Browse files Browse the repository at this point in the history
We now have a solid foundation for the rest of the project
  • Loading branch information
arctic-hen7 committed Jul 28, 2021
1 parent c8530cf commit b2e9a68
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 173 deletions.
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sycamore = { version = "0.5.1", features = ["ssr"] }
sycamore-router = "0.5.1"
web-sys = { version = "0.3", features = ["Headers", "Request", "RequestInit", "RequestMode", "Response", "ReadableStream", "Window"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
typetag = "0.1"
error-chain = "0.12"
futures = "0.3"
console_error_panic_hook = "0.1.6"
urlencoding = "2.1"

[workspace]
members = [
"examples/showcase/app",
"examples/showcase/server"
]
14 changes: 14 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,18 @@ This folder contains examples for Perseus, which are used to test the project an

These examples are all fully self-contained, and do not serve as examples in the traditional Cargo way, they are each indepedent crates to enable the use of build tools such as `wasm-pack`.

## Workspaces??

A Perseus setup is composed of an app and a server, which would normally be in a workspace project. However, examples with their own `Cargo.toml` manifests aren't detected by RLS, and so we need to make them part of the super-workspace at the root. The problem with that is that then we'd have nested workspaces, which are currently impossible. The solution used is to have each example be atomic (i.e. app OR server), but they're still listed together under the same parent directory. If you want to clone one of these to run locally without the rest of the repo, you'll need to get the appropriate directory with both an app and a server, and then add a new `Cargo.toml` at the root of that with the following contents:

```toml
[workspace]
members = [
"app",
"server"
]
```

All examples should have both an `app` and a `server` directory.

- Showcase -- an app that demonstrates all the different features of Perseus, including SSR, SSG, and ISR (this example is actively used for testing)
5 changes: 0 additions & 5 deletions examples/showcase/Cargo.toml

This file was deleted.

7 changes: 2 additions & 5 deletions examples/showcase/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
perseus = { path = "../../../" }
sycamore = { version = "0.5.1", features = ["ssr"] }
sycamore-router = "0.5.1"
web-sys = { version = "0.3", features = ["Headers", "Request", "RequestInit", "RequestMode", "Response", "ReadableStream", "Window"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
typetag = "0.1"
error-chain = "0.12"
futures = "0.3"
serde_json = "1" # Possibly don't need?
console_error_panic_hook = "0.1.6"
urlencoding = "2.1"

Expand Down
11 changes: 6 additions & 5 deletions examples/showcase/app/src/bin/build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use perseus_showcase_app::{
pages,
use perseus::{
config_manager::{FsConfigManager, ConfigManager},
build_pages
};
use perseus_showcase_app::pages;
use sycamore::prelude::SsrNode;

fn main() {
let config_manager = FsConfigManager::new();

build_pages!([
pages::index::get_page(),
pages::about::get_page(),
pages::post::get_page()
pages::index::get_page::<SsrNode>(),
pages::about::get_page::<SsrNode>(),
pages::post::get_page::<SsrNode>()
], &config_manager);

println!("Static generation successfully completed!");
Expand Down
40 changes: 16 additions & 24 deletions examples/showcase/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
pub mod errors;
pub mod pages;
mod shell;
pub mod serve;
pub mod render_cfg;
pub mod config_manager;
pub mod page;
pub mod build;

use sycamore::prelude::*;
use sycamore_router::{Route, BrowserRouter};
use wasm_bindgen::prelude::*;
use perseus::shell::app_shell;

// Define our routes
#[derive(Route)]
Expand Down Expand Up @@ -44,26 +38,24 @@ pub fn run() -> Result<(), JsValue> {
template! {
BrowserRouter(|route: AppRoute| {
match route {
AppRoute::Index => app_shell!({
name => "index",
props => pages::index::IndexPageProps,
template => |props: Option<pages::index::IndexPageProps>| template! {
AppRoute::Index => app_shell(
"index".to_string(),
Box::new(|props: Option<pages::index::IndexPageProps>| template! {
pages::index::IndexPage(props.unwrap())
},
}),
AppRoute::About => app_shell!({
name => "about",
template => |_: Option<()>| template! {
})
),
AppRoute::About => app_shell(
"about".to_string(),
Box::new(|_: Option<()>| template! {
pages::about::AboutPage()
},
}),
AppRoute::Post { slug } => app_shell!({
name => &format!("post/{}", slug),
props => pages::post::PostPageProps,
template => |props: Option<pages::post::PostPageProps>| template! {
})
),
AppRoute::Post { slug } => app_shell(
format!("post/{}", slug),
Box::new(|props: Option<pages::post::PostPageProps>| template! {
pages::post::PostPage(props.unwrap())
},
}),
})
),
AppRoute::NotFound => template! {
p {"Not Found."}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/showcase/app/src/pages/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sycamore::prelude::*;
use crate::page::Page;
use perseus::page::Page;

#[component(AboutPage<G>)]
pub fn about_page() -> Template<G> {
Expand All @@ -8,7 +8,7 @@ pub fn about_page() -> Template<G> {
}
}

pub fn get_page() -> Page<()> {
pub fn get_page<G: GenericNode>() -> Page<(), G> {
Page::new("about")
.template(Box::new(|_| template! {
AboutPage()
Expand Down
4 changes: 2 additions & 2 deletions examples/showcase/app/src/pages/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sycamore::prelude::*;
use serde::{Serialize, Deserialize};
use crate::page::Page;
use perseus::page::Page;

#[derive(Serialize, Deserialize, Debug)]
pub struct IndexPageProps {
Expand All @@ -15,7 +15,7 @@ pub fn index_page(props: IndexPageProps) -> Template<G> {
}
}

pub fn get_page() -> Page<IndexPageProps> {
pub fn get_page<G: GenericNode>() -> Page<IndexPageProps, G> {
Page::new("index")
.build_state_fn(Box::new(get_static_props))
.template(Box::new(|props: Option<IndexPageProps>| template! {
Expand Down
4 changes: 2 additions & 2 deletions examples/showcase/app/src/pages/post.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sycamore::prelude::*;
use serde::{Serialize, Deserialize};
use crate::page::Page;
use perseus::page::Page;

#[derive(Serialize, Deserialize)]
pub struct PostPageProps {
Expand All @@ -22,7 +22,7 @@ pub fn post_page(props: PostPageProps) -> Template<G> {
}
}

pub fn get_page() -> Page<PostPageProps> {
pub fn get_page<G: GenericNode>() -> Page<PostPageProps, G> {
Page::new("post")
.build_paths_fn(Box::new(get_static_paths))
.build_state_fn(Box::new(get_static_props))
Expand Down
99 changes: 0 additions & 99 deletions examples/showcase/app/src/shell.rs

This file was deleted.

1 change: 1 addition & 0 deletions examples/showcase/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
perseus = { path = "../../../" }
actix-web = "3.3"
actix-files = "0.5"
urlencoding = "2.1"
Expand Down
9 changes: 6 additions & 3 deletions examples/showcase/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use actix_web::{web, App, HttpRequest, HttpServer, Result as ActixResult, error};
use actix_files::{NamedFile};
use perseus_showcase_app::serve::{get_render_cfg, get_page};
use perseus_showcase_app::render_cfg::RenderCfg;
use perseus_showcase_app::config_manager::FsConfigManager;

use perseus::{
serve::{get_render_cfg, get_page},
render_cfg::RenderCfg,
config_manager::FsConfigManager
};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
Expand Down
3 changes: 2 additions & 1 deletion examples/showcase/app/src/build.rs → src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
};
use crate::errors::*;
use std::any::Any;
use sycamore::prelude::SsrNode;

/// Builds a page, writing static data as appropriate. This should be used as part of a larger build process.
pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props>, config_manager: &impl ConfigManager) -> Result<Vec<RenderOpt>> {
pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props, SsrNode>, config_manager: &impl ConfigManager) -> Result<Vec<RenderOpt>> {
let mut render_opts: Vec<RenderOpt> = Vec::new();
let page_path = page.get_path();

Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub mod errors;
pub mod shell;
pub mod serve;
pub mod render_cfg;
pub mod config_manager;
pub mod page;
pub mod build;
Loading

0 comments on commit b2e9a68

Please sign in to comment.