-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added rocket integration (#266)
* perseus custom rocket server * Add example * Fix issues with routes rank * Change unwraps to more idiomatic rust syntax * Configure host in default function * remove unnecessary Arc wrapper * remove unnecessary clones * Bump perseus beta version * fix: some typos * feat: use the host parameter * use basic example templates * Add warning for Rocket Beta * fix: Copy paste blunders
- Loading branch information
Showing
14 changed files
with
496 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "custom_server_rocket" | ||
version = "0.4.0-beta.22" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
perseus = { path = "../../../packages/perseus", features = ["hydrate"] } | ||
sycamore = "^0.8.1" | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1" | ||
|
||
[target.'cfg(engine)'.dev-dependencies] | ||
fantoccini = "0.17" | ||
|
||
[target.'cfg(engine)'.dependencies] | ||
tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] } | ||
perseus-rocket = { path = "../../../packages/perseus-rocket", features = [ "dflt-server" ] } | ||
rocket = "0.5.0-rc.2" | ||
|
||
[target.'cfg(client)'.dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Custom Server Rocket Example | ||
|
||
This is an example of setting up a custom server using rocket with Perseus, with one example API route. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
mod templates; | ||
|
||
use perseus::prelude::*; | ||
|
||
#[cfg(engine)] | ||
mod api { | ||
use rocket::get; | ||
|
||
#[get("/hello")] | ||
pub fn hello() -> String { | ||
"Hello from an api".to_string() | ||
} | ||
} | ||
|
||
#[cfg(engine)] | ||
pub async fn dflt_server< | ||
M: perseus::stores::MutableStore + 'static, | ||
T: perseus::i18n::TranslationsManager + 'static, | ||
>( | ||
turbine: &'static perseus::turbine::Turbine<M, T>, | ||
opts: perseus::server::ServerOptions, | ||
(host, port): (String, u16), | ||
) { | ||
use perseus_rocket::perseus_base_app; | ||
use rocket::routes; | ||
|
||
let addr = host.parse().expect("Invalid address provided to bind to."); | ||
|
||
let mut config = rocket::Config::default(); | ||
|
||
let mut app = perseus_base_app(turbine, opts).await; | ||
app = app.mount("/api", routes![api::hello]); | ||
|
||
config.address = addr; | ||
config.port = port; | ||
app = app.configure(config); | ||
|
||
match app.launch().await { | ||
Err(e) => println!("Error lauching rocket app: {}", e), | ||
_ => (), | ||
} | ||
} | ||
|
||
#[perseus::main(dflt_server)] | ||
pub fn main<G: Html>() -> PerseusApp<G> { | ||
PerseusApp::new() | ||
.template(crate::templates::index::get_template()) | ||
.template(crate::templates::about::get_template()) | ||
.error_views(ErrorViews::unlocalized_development_default()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use perseus::prelude::*; | ||
use sycamore::prelude::*; | ||
|
||
fn about_page<G: Html>(cx: Scope) -> View<G> { | ||
view! { cx, | ||
p { "About." } | ||
} | ||
} | ||
|
||
#[engine_only_fn] | ||
fn head(cx: Scope) -> View<SsrNode> { | ||
view! { cx, | ||
title { "About Page | Perseus Example – Basic" } | ||
} | ||
} | ||
|
||
pub fn get_template<G: Html>() -> Template<G> { | ||
Template::build("about").view(about_page).head(head).build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use perseus::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
use sycamore::prelude::*; | ||
|
||
#[derive(Serialize, Deserialize, ReactiveState, Clone)] | ||
#[rx(alias = "IndexPageStateRx")] | ||
struct IndexPageState { | ||
greeting: String, | ||
} | ||
|
||
#[auto_scope] | ||
fn index_page<G: Html>(cx: Scope, state: &IndexPageStateRx) -> View<G> { | ||
view! { cx, | ||
p { (state.greeting.get()) } | ||
a(href = "about", id = "about-link") { "About!" } | ||
} | ||
} | ||
|
||
#[engine_only_fn] | ||
fn head(cx: Scope, _props: IndexPageState) -> View<SsrNode> { | ||
view! { cx, | ||
title { "Index Page | Perseus Example – Basic" } | ||
} | ||
} | ||
|
||
#[engine_only_fn] | ||
async fn get_build_state(_info: StateGeneratorInfo<()>) -> IndexPageState { | ||
IndexPageState { | ||
greeting: "Hello World!".to_string(), | ||
} | ||
} | ||
|
||
pub fn get_template<G: Html>() -> Template<G> { | ||
Template::build("index") | ||
.build_state_fn(get_build_state) | ||
.view_with_state(index_page) | ||
.head_with_state(head) | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod about; | ||
pub mod index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "perseus-rocket" | ||
version = "0.4.0-beta.22" | ||
edition = "2021" | ||
description = "An integration that makes the Perseus framework easy to use with Rocket." | ||
authors = ["Miroito <[email protected]", "arctic_hen7 <[email protected]>"] | ||
license = "MIT" | ||
repository = "https://github.com/framesurge/perseus" | ||
homepage = "https://framesurge.sh/perseus" | ||
readme = "./README.md" | ||
keywords = ["wasm", "frontend", "webdev", "ssg", "ssr"] | ||
categories = ["wasm", "web-programming::http-server", "development-tools", "asynchronous", "gui"] | ||
|
||
[dependencies] | ||
perseus = { path = "../perseus", version = "0.4.0-beta.21"} | ||
rocket = "0.5.0-rc.2" | ||
|
||
[features] | ||
# Enables the default server configuration, which provides a convenience function if you're not adding any extra routes | ||
dflt-server = [] | ||
|
||
[package.metadata.docs.rs] | ||
rustc-args = ["--cfg=engine"] | ||
rustdoc-args = ["--cfg=engine"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Perseus Rocket Integration | ||
|
||
> :warning: This integration uses Rocket 0.5.0-rc.2 which is still in beta | ||
This is the official [Perseus](https://github.com/framesurge/perseus) integration for making serving your apps on [Rocket](https://rocket.rs/) significantly easier! | ||
|
||
If you're new to Perseus, you should check out [the core package](https://github.com/framesurge/perseus) first. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../README.md |
Oops, something went wrong.