Skip to content

Commit

Permalink
Add cookie documentation to Pavex's guide (LukeMathWalker#236)
Browse files Browse the repository at this point in the history
Misc: I restructured the guide to use navigation sections since the left
sidebar was getting busy.
  • Loading branch information
LukeMathWalker authored Apr 6, 2024
1 parent 883aed7 commit f749294
Show file tree
Hide file tree
Showing 68 changed files with 752 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ jobs:
--exclude-path="site/api_reference/help.html"
--exclude-path="site/api_reference/settings.html"
--exclude="https://doc.rust-lang.org/*"
--exclude="https://github.com/LukeMathWalker/pavex/edit/main/*"
--exclude="https://docs.rs/**/*"
--exclude-path="site/api_reference/static.files"
--exclude="https://fonts.gstatic.com"
--require-https
--verbose
--no-progress
site
3 changes: 2 additions & 1 deletion doc_examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
!*.snap
!*.snap
tutorial_envs
10 changes: 10 additions & 0 deletions doc_examples/guide/cookies/installation/project-kit.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```rust title="src/blueprint.rs" hl_lines="6"
use pavex::blueprint::Blueprint;
use pavex::cookie::CookieKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
CookieKit::new().register(&mut bp);
// [...]
}
```
2 changes: 2 additions & 0 deletions doc_examples/guide/cookies/installation/project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
14 changes: 14 additions & 0 deletions doc_examples/guide/cookies/installation/project/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "cookie_installation"
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"
tokio = { version = "1.35.1", features = ["time"] }

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pavex::blueprint::Blueprint;
use pavex::cookie::CookieKit;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
CookieKit::new().register(&mut bp);
bp
}
5 changes: 5 additions & 0 deletions doc_examples/guide/cookies/installation/project/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![allow(dead_code)]
#![allow(unused_variables)]
pub use blueprint::blueprint;

mod blueprint;
13 changes: 13 additions & 0 deletions doc_examples/guide/cookies/installation/project/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::error::Error;

use cargo_px_env::generated_pkg_manifest_path;
use cookie_installation::blueprint;
use pavex_cli_client::Client;

fn main() -> Result<(), Box<dyn Error>> {
let generated_dir = generated_pkg_manifest_path()?.parent().unwrap().into();
Client::new()
.generate(blueprint(), generated_dir)
.execute()?;
Ok(())
}
9 changes: 9 additions & 0 deletions doc_examples/guide/cookies/installation/tutorial.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
starter_project_folder: "project"
commands:
- command: "cargo px c"
expected_outcome: "success"
snippets:
- name: "kit"
source_path: "src/blueprint.rs"
ranges: [ "0..6", "7..8" ]
hl_lines: [ 6 ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```rust title="src/blueprint.rs" hl_lines="7"
use pavex::blueprint::Blueprint;
use pavex::cookie::CookieKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
CookieKit::new()
.with_default_processor_config()
.register(&mut bp);
// [...]
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "cookie_installation_with_default"
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"
tokio = { version = "1.35.1", features = ["time"] }

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use pavex::blueprint::Blueprint;
use pavex::cookie::CookieKit;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
CookieKit::new()
.with_default_processor_config()
.register(&mut bp);
bp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![allow(dead_code)]
#![allow(unused_variables)]
pub use blueprint::blueprint;

mod blueprint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::error::Error;

use cargo_px_env::generated_pkg_manifest_path;
use cookie_installation_with_default::blueprint;
use pavex_cli_client::Client;

fn main() -> Result<(), Box<dyn Error>> {
let generated_dir = generated_pkg_manifest_path()?.parent().unwrap().into();
Client::new()
.generate(blueprint(), generated_dir)
.execute()?;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
starter_project_folder: "project"
commands:
- command: "cargo px c"
expected_outcome: "success"
snippets:
- name: "default"
source_path: "src/blueprint.rs"
ranges: [ "0..8", "9.." ]
hl_lines: [ 7 ]
12 changes: 12 additions & 0 deletions doc_examples/guide/cookies/request_cookies/project-inject.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```rust title="src/core/routes.rs"
use pavex::cookie::RequestCookies;
use pavex::response::Response;
pub fn handler(request_cookies: &RequestCookies) -> Response {
let Some(session_id) = request_cookies.get("session_id") else {
return Response::unauthorized();
};
// Further processing
// [...]
}
```
13 changes: 13 additions & 0 deletions doc_examples/guide/cookies/request_cookies/project-multiple.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
```rust title="src/multiple/routes.rs"
use pavex::cookie::RequestCookies;
use pavex::response::Response;
pub fn handler(request_cookies: &RequestCookies) -> Response {
let cookies_values: Vec<_> = match request_cookies.get_all("origin") {
Some(cookies) => cookies.values().collect(),
None => Vec::new(),
};
// Further processing
// [...]
}
```
2 changes: 2 additions & 0 deletions doc_examples/guide/cookies/request_cookies/project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
14 changes: 14 additions & 0 deletions doc_examples/guide/cookies/request_cookies/project/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "request_cookies"
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"
tokio = { version = "1.35.1", features = ["time"] }

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use pavex::blueprint::Blueprint;
use pavex::cookie::CookieKit;
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

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

mod blueprint;
mod routes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use pavex::cookie::RequestCookies;
use pavex::response::Response;

pub fn handler(request_cookies: &RequestCookies) -> Response {
let Some(session_id) = request_cookies.get("session_id") else {
return Response::unauthorized();
};
// Further processing
Response::ok()
}
7 changes: 7 additions & 0 deletions doc_examples/guide/cookies/request_cookies/project/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(dead_code)]
#![allow(unused_variables)]
pub use blueprint::blueprint;

mod blueprint;
pub mod core;
pub mod multiple;
13 changes: 13 additions & 0 deletions doc_examples/guide/cookies/request_cookies/project/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::error::Error;

use cargo_px_env::generated_pkg_manifest_path;
use request_cookies::blueprint;
use pavex_cli_client::Client;

fn main() -> Result<(), Box<dyn Error>> {
let generated_dir = generated_pkg_manifest_path()?.parent().unwrap().into();
Client::new()
.generate(blueprint(), generated_dir)
.execute()?;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use pavex::blueprint::router::GET;
use pavex::blueprint::Blueprint;
use pavex::f;

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

mod blueprint;
mod routes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use pavex::cookie::RequestCookies;
use pavex::response::Response;

pub fn handler(request_cookies: &RequestCookies) -> Response {
let cookies_values: Vec<_> = match request_cookies.get_all("origin") {
Some(cookies) => cookies.values().collect(),
None => Vec::new(),
};
// Further processing
Response::ok()
}
11 changes: 11 additions & 0 deletions doc_examples/guide/cookies/request_cookies/tutorial.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
starter_project_folder: "project"
commands:
- command: "cargo px c"
expected_outcome: "success"
snippets:
- name: "inject"
source_path: "src/core/routes.rs"
ranges: [ "0..8", "9..10" ]
- name: "multiple"
source_path: "src/multiple/routes.rs"
ranges: [ "0..9", "10.." ]
13 changes: 13 additions & 0 deletions doc_examples/guide/cookies/response_cookies/project-delete.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
```rust title="src/delete/routes.rs"
use pavex::cookie::{RemovalCookie, ResponseCookies};
use pavex::response::Response;
pub fn handler(response_cookies: &mut ResponseCookies) -> Response {
let cookie = RemovalCookie::new("last_visited")
// We need to match the path of the cookie we want to delete.
.set_path("/web");
response_cookies.insert(cookie);
Response::ok()
}
```
21 changes: 21 additions & 0 deletions doc_examples/guide/cookies/response_cookies/project-insert.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```rust title="src/core/routes.rs"
use pavex::cookie::{ResponseCookie, ResponseCookies};
use pavex::response::Response;
use pavex::time::{format_description::well_known::Iso8601, OffsetDateTime};
pub fn handler(response_cookies: &mut ResponseCookies) -> Response {
let now = OffsetDateTime::now_utc().format(&Iso8601::DEFAULT).unwrap();
let cookie = ResponseCookie::new("last_visited", now)
// We restrict the cookie to a specific path.
.set_path("/web");
// Make sure to insert the cookie into `&mut ResponseCookies`!
// Otherwise, the cookie won't be attached to the response.
response_cookies.insert(cookie);
// You don't have to manually attach the cookie to the response!
// It'll be done by the injector middleware at the end of the request
// processing pipeline.
Response::ok()
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
Loading

0 comments on commit f749294

Please sign in to comment.