Skip to content

Commit

Permalink
feat: added rocket integration (#266)
Browse files Browse the repository at this point in the history
* 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
Miroito authored Apr 6, 2023
1 parent 2017f69 commit 25bd115
Show file tree
Hide file tree
Showing 14 changed files with 496 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bonnie.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ test.subcommands.cli.desc = "runs the cli tests (all are long-running, this will
test.subcommands.example-all-integrations.cmd = [
"EXAMPLE_INTEGRATION=actix-web bonnie dev example %category %example test",
"EXAMPLE_INTEGRATION=warp bonnie dev example %category %example test",
"EXAMPLE_INTEGRATION=axum bonnie dev example %category %example test"
"EXAMPLE_INTEGRATION=axum bonnie dev example %category %example test",
"EXAMPLE_INTEGRATION=rocket bonnie dev example %category %example test"
]
test.subcommands.example-all-integrations.args = [ "category", "example" ]
test.subcommands.example-all-integrations.desc = "tests a single example with all integrations (assumes geckodriver running in background)"
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions examples/core/custom_server_rocket/Cargo.toml
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]
3 changes: 3 additions & 0 deletions examples/core/custom_server_rocket/README.md
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.
50 changes: 50 additions & 0 deletions examples/core/custom_server_rocket/src/main.rs
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())
}
19 changes: 19 additions & 0 deletions examples/core/custom_server_rocket/src/templates/about.rs
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()
}
39 changes: 39 additions & 0 deletions examples/core/custom_server_rocket/src/templates/index.rs
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()
}
2 changes: 2 additions & 0 deletions examples/core/custom_server_rocket/src/templates/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod about;
pub mod index;
2 changes: 2 additions & 0 deletions packages/perseus-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ edition = "2021"
perseus-actix-web = { path = "../perseus-actix-web", features = [ "dflt-server" ], optional = true }
perseus-warp = { path = "../perseus-warp", features = [ "dflt-server" ], optional = true }
perseus-axum = { path = "../perseus-axum", features = [ "dflt-server" ], optional = true }
perseus-rocket = { path = "../perseus-rocket", features = [ "dflt-server" ], optional = true }

[features]
default = [ "warp" ]

actix-web = [ "perseus-actix-web" ]
warp = [ "perseus-warp" ]
axum = [ "perseus-axum" ]
rocket = [ "perseus-rocket" ]
2 changes: 2 additions & 0 deletions packages/perseus-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
pub use perseus_actix_web::dflt_server;
#[cfg(feature = "axum")]
pub use perseus_axum::dflt_server;
#[cfg(feature = "rocket")]
pub use perseus_rocket::dflt_server;
#[cfg(feature = "warp")]
pub use perseus_warp::dflt_server;
24 changes: 24 additions & 0 deletions packages/perseus-rocket/Cargo.toml
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"]
7 changes: 7 additions & 0 deletions packages/perseus-rocket/README.md
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.
1 change: 1 addition & 0 deletions packages/perseus-rocket/README.proj.md
Loading

0 comments on commit 25bd115

Please sign in to comment.