This commit is contained in:
30
src/components/layout.rs
Normal file
30
src/components/layout.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use sycamore::prelude::*;
|
||||
|
||||
#[component]
|
||||
pub fn Layout<'a, G: Html>(
|
||||
cx: Scope<'a>,
|
||||
LayoutProps { title, children }: LayoutProps<'a, G>,
|
||||
) -> View<G> {
|
||||
let children = children.call(cx);
|
||||
|
||||
view! { cx,
|
||||
// These elements are styled with bright colors for demonstration purposes
|
||||
header(style = "background-color: red; color: white; padding: 1rem") {
|
||||
p { (title.to_string()) }
|
||||
}
|
||||
main(style = "padding: 1rem") {
|
||||
(children)
|
||||
}
|
||||
footer(style = "background-color: black; color: white; padding: 1rem") {
|
||||
p { "Hey there, I'm a footer!" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct LayoutProps<'a, G: Html> {
|
||||
/// The title of the page, which will be displayed in the header.
|
||||
pub title: &'a str,
|
||||
/// The content to put inside the layout.
|
||||
pub children: Children<'a, G>,
|
||||
}
|
||||
1
src/components/mod.rs
Normal file
1
src/components/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod layout;
|
||||
62
src/error_views.rs
Normal file
62
src/error_views.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use perseus::errors::ClientError;
|
||||
use perseus::prelude::*;
|
||||
use sycamore::prelude::*;
|
||||
|
||||
pub fn get_error_views<G: Html>() -> ErrorViews<G> {
|
||||
ErrorViews::new(|cx, err, _err_info, _err_pos| {
|
||||
match err {
|
||||
ClientError::ServerError { status, message: _ } => match status {
|
||||
404 => (
|
||||
view! { cx,
|
||||
title { "Page not found" }
|
||||
},
|
||||
view! { cx,
|
||||
p { "Sorry, that page doesn't seem to exist." }
|
||||
},
|
||||
),
|
||||
// 4xx is a client error
|
||||
_ if (400..500).contains(&status) => (
|
||||
view! { cx,
|
||||
title { "Error" }
|
||||
},
|
||||
view! { cx,
|
||||
p { "There was something wrong with the last request, please try reloading the page." }
|
||||
},
|
||||
),
|
||||
// 5xx is a server error
|
||||
_ => (
|
||||
view! { cx,
|
||||
title { "Error" }
|
||||
},
|
||||
view! { cx,
|
||||
p { "Sorry, our server experienced an internal error. Please try reloading the page." }
|
||||
},
|
||||
),
|
||||
},
|
||||
ClientError::Panic(_) => (
|
||||
view! { cx,
|
||||
title { "Critical error" }
|
||||
},
|
||||
view! { cx,
|
||||
p { "Sorry, but a critical internal error has occurred. This has been automatically reported to our team, who'll get on it as soon as possible. In the mean time, please try reloading the page." }
|
||||
},
|
||||
),
|
||||
ClientError::FetchError(_) => (
|
||||
view! { cx,
|
||||
title { "Error" }
|
||||
},
|
||||
view! { cx,
|
||||
p { "A network error occurred, do you have an internet connection? (If you do, try reloading the page.)" }
|
||||
},
|
||||
),
|
||||
_ => (
|
||||
view! { cx,
|
||||
title { "Error" }
|
||||
},
|
||||
view! { cx,
|
||||
p { (format!("An internal error has occurred: '{}'.", err)) }
|
||||
},
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
33
src/main.rs
Normal file
33
src/main.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
mod components;
|
||||
mod templates;
|
||||
mod error_views;
|
||||
|
||||
use perseus::prelude::*;
|
||||
use sycamore::prelude::view;
|
||||
|
||||
#[perseus::main(perseus_axum::dflt_server)]
|
||||
pub fn main<G: Html>() -> PerseusApp<G> {
|
||||
use log::{info, warn};
|
||||
env_logger::init();
|
||||
|
||||
PerseusApp::new()
|
||||
.template(crate::templates::index::get_template())
|
||||
.template(crate::templates::long::get_template())
|
||||
.error_views(crate::error_views::get_error_views())
|
||||
.index_view(|cx| {
|
||||
view! { cx,
|
||||
html {
|
||||
head {
|
||||
meta(charset = "UTF-8")
|
||||
meta(name = "viewport", content = "width=device-width, initial-scale=1.0")
|
||||
// Perseus automatically resolves `/.perseus/static/` URLs to the contents of the `static/` directory at the project root
|
||||
link(rel = "stylesheet", href = ".perseus/static/style.css")
|
||||
}
|
||||
body {
|
||||
// Quirk: this creates a wrapper `<div>` around the root `<div>` by necessity
|
||||
PerseusRoot()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
29
src/templates/index.rs
Normal file
29
src/templates/index.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::components::layout::Layout;
|
||||
use perseus::prelude::*;
|
||||
use sycamore::prelude::*;
|
||||
use crate::templates::get_path;
|
||||
|
||||
fn index_page<G: Html>(cx: Scope) -> View<G> {
|
||||
view! { cx,
|
||||
Layout(title = "Index") {
|
||||
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
||||
p { "Hello World!" }
|
||||
br {}
|
||||
a(href = "long") { "Long page" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[engine_only_fn]
|
||||
fn head(cx: Scope) -> View<SsrNode> {
|
||||
view! { cx,
|
||||
title { "Index Page" }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_template<G: Html>() -> Template<G> {
|
||||
Template::build(get_path("").as_str())
|
||||
.view(index_page)
|
||||
.head(head)
|
||||
.build()
|
||||
}
|
||||
86
src/templates/long.rs
Normal file
86
src/templates/long.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use crate::components::layout::Layout;
|
||||
use perseus::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use sycamore::prelude::*;
|
||||
use crate::templates::get_path;
|
||||
|
||||
// Reactive page
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||
#[rx(alias = "PageStateRx")]
|
||||
struct PageState {
|
||||
ip: String,
|
||||
text: String,
|
||||
}
|
||||
|
||||
fn request_state_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
||||
view! { cx,
|
||||
p {
|
||||
(
|
||||
format!("Your IP address is {}.", state.ip.get())
|
||||
)
|
||||
}
|
||||
p {
|
||||
(
|
||||
state.text.get().repeat(5000)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[engine_only_fn]
|
||||
async fn get_request_state(
|
||||
_info: StateGeneratorInfo<()>,
|
||||
req: Request,
|
||||
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
||||
let text = fs::read_to_string("static/test.txt")
|
||||
.unwrap_or("~".to_string())
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
Ok(PageState {
|
||||
ip: format!(
|
||||
"{:?}",
|
||||
req.headers()
|
||||
// NOTE: This header can be trivially spoofed, and may well not be the user's actual
|
||||
// IP address
|
||||
.get("X-Forwarded-For")
|
||||
.unwrap_or(&perseus::http::HeaderValue::from_str("hidden from view!").unwrap())
|
||||
),
|
||||
text,
|
||||
})
|
||||
}
|
||||
|
||||
// Regular page
|
||||
|
||||
fn long_page<G: Html>(cx: Scope) -> View<G> {
|
||||
view! { cx,
|
||||
Layout(title = "Long") {
|
||||
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
||||
a(href = "") { "Index" }
|
||||
br {}
|
||||
p {
|
||||
("This is a test. ".repeat(5000))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[engine_only_fn]
|
||||
fn head(cx: Scope) -> View<SsrNode> {
|
||||
view! { cx,
|
||||
title { "Long Page" }
|
||||
}
|
||||
}
|
||||
|
||||
// Template
|
||||
|
||||
pub fn get_template<G: Html>() -> Template<G> {
|
||||
// Template::build(get_full_path("long")).view(long_page).head(head).build()
|
||||
Template::build(get_path("long").as_str())
|
||||
.request_state_fn(get_request_state)
|
||||
.view_with_state(request_state_page)
|
||||
.head(head)
|
||||
.build()
|
||||
}
|
||||
17
src/templates/mod.rs
Normal file
17
src/templates/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use std::env;
|
||||
|
||||
pub mod index;
|
||||
pub mod long;
|
||||
|
||||
pub fn get_path(path: &str) -> String {
|
||||
// Get base path
|
||||
match env::var("PERSEUS_BASE_PATH") {
|
||||
Ok(env_path) => {
|
||||
// Strip the slash on both sides for directory consistency
|
||||
// let stripped_env = env_path.trim_start_matches("/").trim_end_matches("/");
|
||||
// format!("{stripped_env}/{path}").to_string().trim_end_matches("/").to_owned()
|
||||
path.to_owned()
|
||||
}
|
||||
Err(_) => path.to_owned(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user