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

feat: Add support for domain guards #375

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use pavex::f;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
CookieKit::new().register(&mut bp);
bp.singleton(f!(<pavex::cookie::ProcessorConfig as std::default::Default>::default));
bp.nest_at("/core", crate::core::blueprint());
bp.nest_at("/multiple", crate::multiple::blueprint());
bp.singleton(f!(
<pavex::cookie::ProcessorConfig as std::default::Default>::default
));
bp.prefix("/core").nest(crate::core::blueprint());
bp.prefix("/multiple").nest(crate::multiple::blueprint());
bp
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn blueprint() -> Blueprint {
bp.singleton(f!(
<pavex::cookie::ProcessorConfig as std::default::Default>::default
));
bp.nest_at("/core", crate::core::blueprint());
bp.nest_at("/delete", crate::delete::blueprint());
bp.prefix("/core").nest(crate::core::blueprint());
bp.prefix("/delete").nest(crate::delete::blueprint());
bp
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pavex::blueprint::Blueprint;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.nest_at("/core", crate::core::blueprint());
bp.nest_at("/universal", crate::universal::blueprint());
bp.prefix("/core").nest(crate::core::blueprint());
bp.prefix("/universal").nest(crate::universal::blueprint());
bp
}
19 changes: 11 additions & 8 deletions doc_examples/guide/middleware/order/project/src/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use pavex::blueprint::Blueprint;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.nest(crate::core::blueprint());
bp.nest_at("/pre_only", crate::pre_only::blueprint());
bp.nest_at("/post_only", crate::post_only::blueprint());
bp.nest_at("/wrap_only", crate::wrap_only::blueprint());
bp.nest_at("/pre_and_post", crate::pre_and_post::blueprint());
bp.nest_at("/post_and_wrap", crate::post_and_wrap::blueprint());
bp.nest_at("/pre_and_wrap", crate::pre_and_wrap::blueprint());
bp.nest_at("/order1", crate::order1::blueprint());
bp.nest_at("/order2", crate::order2::blueprint());
bp.prefix("/pre_only").nest(crate::pre_only::blueprint());
bp.prefix("/post_only").nest(crate::post_only::blueprint());
bp.prefix("/wrap_only").nest(crate::wrap_only::blueprint());
bp.prefix("/pre_and_post")
.nest(crate::pre_and_post::blueprint());
bp.prefix("/post_and_wrap")
.nest(crate::post_and_wrap::blueprint());
bp.prefix("/pre_and_wrap")
.nest(crate::pre_and_wrap::blueprint());
bp.prefix("/order1").nest(crate::order1::blueprint());
bp.prefix("/order2").nest(crate::order2::blueprint());
bp
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.request_scoped(f!(self::root_span));
bp.nest(crate::core::blueprint());
bp.nest_at("/fallible", crate::fallible::blueprint());
bp.prefix("/fallible").nest(crate::fallible::blueprint());
bp
}
2 changes: 1 addition & 1 deletion doc_examples/guide/middleware/pre/project/src/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ use pavex::blueprint::Blueprint;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.nest(crate::core::blueprint());
bp.nest_at("/fallible", crate::fallible::blueprint());
bp.prefix("/fallible").nest(crate::fallible::blueprint());
bp
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pavex::f;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/users/:id" /* (1)! */, f!(super::handler));
bp.route(GET, "/users/{id}" /* (1)! */, f!(super::handler));
bp
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use pavex::f;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/room/:id", f!(super::handler));
bp.route(GET, "/room/{id}", f!(super::handler));
bp
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use pavex::f;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/users/:id" /* (1)! */, f!(super::handler));
bp.route(GET, "/users/{id}" /* (1)! */, f!(super::handler));
bp
}
18 changes: 18 additions & 0 deletions doc_examples/guide/routing/domain_guards/project-catch_all.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```rust title="src/catch_all/blueprint.rs" hl_lines="7"
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("{*any}.pavex.dev").nest(sub_bp());
bp
}

fn sub_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::handler));
// Other routes...
bp
}
```
18 changes: 18 additions & 0 deletions doc_examples/guide/routing/domain_guards/project-dynamic.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```rust title="src/dynamic/blueprint.rs" hl_lines="7"
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("{sub}.pavex.dev").nest(sub_bp());
bp
}

fn sub_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::index));
// Other routes...
bp
}
```
13 changes: 13 additions & 0 deletions doc_examples/guide/routing/domain_guards/project-fallback.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
```rust title="src/fallback/blueprint.rs" hl_lines="9"
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("pavex.dev").nest(website_bp());
bp.domain("api.pavex.dev").prefix("/v1").nest(api_bp());
// If no domain matches, serve a 404 page.
bp.fallback(f!(super::unknown_domain));
bp
}
```
28 changes: 28 additions & 0 deletions doc_examples/guide/routing/domain_guards/project-intro.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```rust title="src/intro/blueprint.rs" hl_lines="8 10"
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
// Serve the website from the root domain...
bp.domain("pavex.dev").nest(website_bp());
// ...while reserving a subdomain for the REST API.
bp.domain("api.pavex.dev").prefix("/v1").nest(api_bp());
bp
}

fn website_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::index));
// Other web pages...
bp
}

fn api_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/users", f!(super::users));
// Other API routes...
bp
}
```
18 changes: 18 additions & 0 deletions doc_examples/guide/routing/domain_guards/project-multi.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```rust title="src/multi/blueprint.rs" hl_lines="7"
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("{user_id}.{tenant_id}.pavex.dev").nest(user_bp());
bp
}

fn user_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::index));
// Other routes...
bp
}
```
18 changes: 18 additions & 0 deletions doc_examples/guide/routing/domain_guards/project-static.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```rust title="src/static_/blueprint.rs" hl_lines="7"
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("pavex.dev").nest(pavex_bp());
bp
}

fn pavex_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::index));
// Other routes...
bp
}
```
2 changes: 2 additions & 0 deletions doc_examples/guide/routing/domain_guards/project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
13 changes: 13 additions & 0 deletions doc_examples/guide/routing/domain_guards/project/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "domain_guards"
version = "0.1.0"
edition = "2021"

[dependencies]
pavex = { path = "../../../../../libs/pavex" }
pavex_cli_client = { path = "../../../../../libs/pavex_cli_client" }
serde = { version = "1", features = ["derive"] }
cargo_px_env = "0.1"

[workspace]
members = [".", "server_sdk"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "domain_guards_server_sdk"
version = "0.1.0"
edition = "2021"

[package.metadata.px.generate]
generator_type = "cargo_workspace_binary"
generator_name = "domain_guards"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions doc_examples/guide/routing/domain_guards/project/src/blueprint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use pavex::blueprint::Blueprint;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.nest(crate::intro::bp());
bp.prefix("/static").nest(crate::static_::bp());
bp.prefix("/dynamic").nest(crate::dynamic::bp());
bp.prefix("/multi").nest(crate::multi::bp());
bp.prefix("/catch_all").nest(crate::catch_all::bp());
bp.prefix("/fallback").nest(crate::fallback::bp());
bp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("{*any}.pavex.dev").nest(sub_bp());
bp
}

fn sub_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::handler));
// Other routes...
bp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use blueprint::bp;
pub use routes::*;

mod blueprint;
mod routes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use pavex::http::StatusCode;

pub fn handler() -> StatusCode {
StatusCode::OK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("{sub}.pavex.dev").nest(sub_bp());
bp
}

fn sub_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::index));
// Other routes...
bp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use blueprint::bp;
pub use routes::*;

mod blueprint;
mod routes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use pavex::http::StatusCode;

pub fn index() -> StatusCode {
StatusCode::OK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.domain("pavex.dev").nest(website_bp());
bp.domain("api.pavex.dev").prefix("/v1").nest(api_bp());
// If no domain matches, serve a 404 page.
bp.fallback(f!(super::unknown_domain));
bp
}

fn website_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(pavex::blueprint::router::GET, "/", f!(super::index));
// Other web pages...
bp
}

fn api_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(pavex::blueprint::router::GET, "/users", f!(super::users));
// Other API routes...
bp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use blueprint::bp;
pub use routes::*;

mod blueprint;
mod routes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use pavex::http::StatusCode;

pub fn index() -> StatusCode {
StatusCode::OK
}

pub fn unknown_domain() -> StatusCode {
StatusCode::NOT_FOUND
}

pub fn users() -> StatusCode {
StatusCode::OK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn bp() -> Blueprint {
let mut bp = Blueprint::new();
// Serve the website from the root domain...
bp.domain("pavex.dev").nest(website_bp());
// ...while reserving a subdomain for the REST API.
bp.domain("api.pavex.dev").prefix("/v1").nest(api_bp());
bp
}

fn website_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/", f!(super::index));
// Other web pages...
bp
}

fn api_bp() -> Blueprint {
let mut bp = Blueprint::new();
bp.route(GET, "/users", f!(super::users));
// Other API routes...
bp
}
Loading