From 5a693a4bdbc315f6c16bbe32a98383d341250cb4 Mon Sep 17 00:00:00 2001 From: Brandon Konkle Date: Tue, 7 Jan 2025 16:14:09 -0700 Subject: [PATCH] Fix the fake dependency by moving away from git (#126) --- CHANGELOG.md | 7 ++++ examples/async-graphql/Cargo.toml | 9 +---- examples/cqrs-es/Cargo.toml | 9 +---- examples/simple-warp/Cargo.toml | 9 +---- examples/simple/Cargo.toml | 9 +---- nakago/Cargo.toml | 9 +---- nakago_async_graphql/Cargo.toml | 9 +---- nakago_axum/Cargo.toml | 9 +---- nakago_axum/src/init.rs | 4 +- nakago_figment/Cargo.toml | 9 +---- nakago_sea_orm/Cargo.toml | 2 +- nakago_warp/Cargo.toml | 7 +--- nakago_ws/Cargo.toml | 9 +---- website/docs/tutorial.md | 67 ++++++++++++++++++++++++++++--- 14 files changed, 91 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47972e4..2dacad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.24.1] + +### Changed + +- (all packages) - Remove the `git = ""` parameter from the `fake` dependency, as it is no longer needed. +- `nakago-axum` - Change `std::panic::PanicInfo` to `std::panic::PanicHookInfo` + ## [0.24.0] ### Added diff --git a/examples/async-graphql/Cargo.toml b/examples/async-graphql/Cargo.toml index 1016ba2..2c3bdb2 100644 --- a/examples/async-graphql/Cargo.toml +++ b/examples/async-graphql/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-examples-async-graphql" -version = "0.24.0" +version = "0.24.1" description = "A lightweight Rust toolkit for sharp dependency injection 😎" license.workspace = true edition.workspace = true @@ -24,12 +24,7 @@ async-trait = "0.1" axum = { version = "0.7", features = ["ws", "macros"] } chrono = { version = "0.4.19", features = ["serde"] } derive-new = "0.7" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env", "toml", "yaml", "json"] } futures = "0.3" hyper = "1.0" diff --git a/examples/cqrs-es/Cargo.toml b/examples/cqrs-es/Cargo.toml index 827d767..16d0b0d 100644 --- a/examples/cqrs-es/Cargo.toml +++ b/examples/cqrs-es/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-examples-cqrs-es" -version = "0.23.0" +version = "0.24.1" description = "A lightweight Rust toolkit for sharp dependency injection 😎" license.workspace = true edition.workspace = true @@ -20,11 +20,6 @@ nakago = "0.23" [dev-dependencies] criterion = "0.5" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } mockall = "0.13" pretty_assertions = "1.2" diff --git a/examples/simple-warp/Cargo.toml b/examples/simple-warp/Cargo.toml index b80c051..faf5585 100644 --- a/examples/simple-warp/Cargo.toml +++ b/examples/simple-warp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-examples-simple-warp" -version = "0.23.0" +version = "0.24.1" description = "A lightweight Rust toolkit for sharp dependency injection 😎" license.workspace = true edition.workspace = true @@ -17,12 +17,7 @@ integration = [] anyhow = "1.0" async-trait = "0.1" chrono = { version = "0.4.19", features = ["serde"] } -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env", "toml", "yaml", "json"] } futures = "0.3" hyper = "1.0" diff --git a/examples/simple/Cargo.toml b/examples/simple/Cargo.toml index 3166961..4358d23 100644 --- a/examples/simple/Cargo.toml +++ b/examples/simple/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-examples-simple" -version = "0.23.0" +version = "0.24.1" description = "A lightweight Rust toolkit for sharp dependency injection 😎" license.workspace = true edition.workspace = true @@ -18,12 +18,7 @@ anyhow = "1.0" async-trait = "0.1" axum = { version = "0.7", features = ["ws", "macros"] } chrono = { version = "0.4.19", features = ["serde"] } -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env", "toml", "yaml", "json"] } futures = "0.3" hyper = "1.0" diff --git a/nakago/Cargo.toml b/nakago/Cargo.toml index 3ec55bb..95c9628 100644 --- a/nakago/Cargo.toml +++ b/nakago/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago" -version = "0.23.0" +version = "0.24.1" description = "A lightweight Rust toolkit for sharp dependency injection 😎" documentation = "https://docs.rs/nakago/" license.workspace = true @@ -30,12 +30,7 @@ tokio = { version = "1", features = ["full"] } [dev-dependencies] criterion = "0.5" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } mockall = "0.13" pretty_assertions = "1.2" googletest = "0.12" diff --git a/nakago_async_graphql/Cargo.toml b/nakago_async_graphql/Cargo.toml index 99b7878..29f0e4b 100644 --- a/nakago_async_graphql/Cargo.toml +++ b/nakago_async_graphql/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-async-graphql" -version = "0.24.0" +version = "0.24.1" description = "An Async-GraphQL integration for Nakago" documentation = "https://docs.rs/nakago-async-graphql/" license.workspace = true @@ -19,12 +19,7 @@ async-graphql-axum = { version = "6.0", git = "https://github.com/bkonkle/async- async-trait = "0.1" axum = { version = "0.7", features = ["macros"] } derive-new = "0.7" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env"] } hyper = "1.0" log = "0.4" diff --git a/nakago_axum/Cargo.toml b/nakago_axum/Cargo.toml index 34466ac..40875fd 100644 --- a/nakago_axum/Cargo.toml +++ b/nakago_axum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-axum" -version = "0.24.0" +version = "0.24.1" description = "An Axum HTTP routes integration for Nakago" documentation = "https://docs.rs/nakago-axum/" license.workspace = true @@ -18,12 +18,7 @@ biscuit = "0.7.0" bytes = "1" crossterm = "0.28" derive-new = "0.7" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env"] } futures-util = { version = "0.3", default-features = false, features = [ "sink", diff --git a/nakago_axum/src/init.rs b/nakago_axum/src/init.rs index ac165e8..8442ef4 100644 --- a/nakago_axum/src/init.rs +++ b/nakago_axum/src/init.rs @@ -1,4 +1,4 @@ -use std::{io, net::SocketAddr, panic::PanicInfo, sync::Arc}; +use std::{io, net::SocketAddr, panic::PanicHookInfo, sync::Arc}; use axum::{serve::Serve, Router}; use backtrace::Backtrace; @@ -84,7 +84,7 @@ pub fn trace_layer() -> TraceLayer> { // -------------- /// A generic function to log stacktraces on panic -pub fn handle_panic(info: &PanicInfo<'_>) { +pub fn handle_panic(info: &PanicHookInfo<'_>) { if cfg!(debug_assertions) { let location = info.location().unwrap(); diff --git a/nakago_figment/Cargo.toml b/nakago_figment/Cargo.toml index 7494a89..cf888f5 100644 --- a/nakago_figment/Cargo.toml +++ b/nakago_figment/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-figment" -version = "0.23.0" +version = "0.24.1" description = "Figment config loading utils for Nakago" documentation = "https://docs.rs/nakago-figment/" license.workspace = true @@ -12,12 +12,7 @@ homepage.workspace = true [dependencies] anyhow = "1.0" derive-new = "0.7" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env", "toml", "yaml", "json"] } log = "0.4" nakago = "0.23" diff --git a/nakago_sea_orm/Cargo.toml b/nakago_sea_orm/Cargo.toml index 7817681..8d05b52 100644 --- a/nakago_sea_orm/Cargo.toml +++ b/nakago_sea_orm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-sea-orm" -version = "0.23.0" +version = "0.24.1" description = "A SeaORM integration for Nakago" documentation = "https://docs.rs/nakago-sea-orm/" license.workspace = true diff --git a/nakago_warp/Cargo.toml b/nakago_warp/Cargo.toml index b30d073..9e53bcc 100644 --- a/nakago_warp/Cargo.toml +++ b/nakago_warp/Cargo.toml @@ -15,12 +15,7 @@ async-trait = "0.1" biscuit = "0.7.0" bytes = "1" derive-new = "0.7" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } figment = { version = "0.10", features = ["env"] } futures-util = { version = "0.3", default-features = false, features = [ "sink", diff --git a/nakago_ws/Cargo.toml b/nakago_ws/Cargo.toml index fa6151b..0847b98 100644 --- a/nakago_ws/Cargo.toml +++ b/nakago_ws/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nakago-ws" -version = "0.24.0" +version = "0.24.1" description = "Websocket utils for Nakago" documentation = "https://docs.rs/nakago-ws/" license.workspace = true @@ -15,12 +15,7 @@ async-trait = "0.1" axum = { version = "0.7", features = ["ws", "macros"] } biscuit = "0.7.0" derive-new = "0.7" -fake = { version = "2.9", features = [ - 'derive', - 'chrono', - 'http', - 'uuid', -], git = "https://github.com/cksac/fake-rs.git" } +fake = { version = "2.9", features = ['derive', 'chrono', 'http', 'uuid'] } futures = "0.3" http = "1.0.0" hyper = "1.0" diff --git a/website/docs/tutorial.md b/website/docs/tutorial.md index 8ca4464..81ffbf4 100644 --- a/website/docs/tutorial.md +++ b/website/docs/tutorial.md @@ -113,24 +113,62 @@ nakago_figment::Init::::default() .await?; ``` -First, add the default JWKS Validator from `nakago_axum`'s `auth` module using the `provide_type` method, which uses the type as the key for the Inject container: +First, add the default JWKS Validator from `nakago_axum`'s `auth` module using the `provide` method, which uses the type as the key for the Inject container. Add this to your `init.rs` file, within the `app()` function: ```rust use nakago_axum::auth::{validator, Validator}; // ... -i.provide::(validator::Provide::default()).await?; +i.provide::>(validator::Provide::default()) + .await?; ``` -This will be overridden in your tests to use the unverified variant, but we'll get to that later. Next you should use `jwks::Provide` to inject the JWKS config: +This will be overridden in your tests to use the unverified variant, but we'll get to that later. Next you should use `jwks::Provide` to inject the JWKS config. Add thios to your `init.rs` file as well: ```rust -use nakago_axum::auth::{jwks, Jwks}; +use nakago_axum::auth::{jwks, JWKSet, Empty}; // ... -i.provide::(jwks::Provide::::default()).await?; +i.provide::>(jwks::Provide::::default()) + .await?; +``` + +Your `init.rs` should now look like this: + +```rust +use std::path::PathBuf; + +use nakago::{Inject, Result}; +use nakago_axum::{ + auth::{jwks, validator, Empty, JWKSet, Validator}, + config, +}; + +use crate::config::Config; + +/// Create a dependency injection container for the top-level application +pub async fn app(config_path: Option) -> Result { + let i = Inject::default(); + + i.provide::>(validator::Provide::default()) + .await?; + + i.provide::>(jwks::Provide::::default()) + .await?; + + // Add config loaders before the Config is initialized + config::add_default_loaders(&i).await?; + + // Initialize the Config + nakago_figment::Init::::default() + .maybe_with_path(config_path) + .init(&i) + .await?; + + Ok(i) +} ``` ### Axum Route @@ -167,10 +205,29 @@ pub async fn get_username(sub: Subject) -> Json { } ``` +Make sure to add the `user.rs` file to your `http/mod.rs` file: + +```rust +/// User handlers +pub mod user; +``` + The `Subject` extension uses Nakago Axum's State proivider to find the Inject container, which it then uses to grab the JWT config and the Validator instance. It decodes the JWT and returns the `sub` claim from the payload. If the user is not logged in, the `Subject` will contain a `None`. Now add a route that uses the handler to the Init hook at `http/router.rs`: +```rust +use super::{health, user}; + +// ... + +Router::new() + // ... + .route("/username", get(user::get_username)) +``` + +Your `http/router.rs` file should now look like this: + ```rust /// This method should already exist in your `http/router.rs` file pub fn init(i: &Inject) -> Router {