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

Feature: Flexible, Generic WebSocket Sessions #115

Merged
merged 5 commits into from
Sep 4, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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).

## [Unreleased]

### Removed

- `nakago-ws` - Removed `Session` in favor of a generic type parameter for the `Connections` struct.

## [0.23.0]

Major pivot! Until now, Nakago has been aimed at growing into a full-scale API server framework. However, I've realized that the core value of Nakago is the Dependency Injection system, and that it can be used in a wide variety of contexts. This release is a pivot to focus on the DI system itself, and to make it more flexible and easier to use in a variety of contexts.
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ default-members = [
"nakago_ws",
]

[workspace.package]
license = "MIT"
edition = "2021"
authors = ["Brandon Konkle <brandon@konkle.us>"]
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"

[patch.crates-io]
nakago = { path = "./nakago" }
nakago-async-graphql = { path = "./nakago_async_graphql" }
Expand Down
13 changes: 7 additions & 6 deletions examples/async-graphql/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "nakago-examples-async-graphql"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
version = "0.24.0"
description = "A lightweight Rust toolkit for sharp dependency injection 😎"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

publish = false

Expand Down Expand Up @@ -38,7 +39,7 @@ nakago-axum = "0.23"
nakago-derive = "0.23"
nakago-figment = "0.23"
nakago-sea-orm = "0.23"
nakago-ws = "0.23"
nakago-ws = "0.24"
nakago = "0.23"
oso = "0.27"
pico-args = "0.5.0"
Expand Down
22 changes: 11 additions & 11 deletions examples/async-graphql/src/domains/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ pub type SchemaBuilder = async_graphql::SchemaBuilder<Query, Mutation, EmptySubs

/// Loads the GraphQL schema builder dependencies
pub async fn load(i: &Inject) -> nakago::Result<()> {
users::schema::load(&i).await?;
profiles::schema::load(&i).await?;
role_grants::schema::load(&i).await?;
shows::schema::load(&i).await?;
episodes::schema::load(&i).await?;
users::schema::load(i).await?;
profiles::schema::load(i).await?;
role_grants::schema::load(i).await?;
shows::schema::load(i).await?;
episodes::schema::load(i).await?;

Ok(())
}
Expand Down Expand Up @@ -67,14 +67,14 @@ pub async fn init(i: &Inject) -> nakago::Result<()> {

i.inject::<SchemaBuilder>(builder).await?;

users::schema::init(&i).await?;
profiles::schema::init(&i).await?;
role_grants::schema::init(&i).await?;
shows::schema::init(&i).await?;
episodes::schema::init(&i).await?;
users::schema::init(i).await?;
profiles::schema::init(i).await?;
role_grants::schema::init(i).await?;
shows::schema::init(i).await?;
episodes::schema::init(i).await?;

schema::Init::<Query, Mutation, EmptySubscription>::default()
.init(&i)
.init(i)
.await?;

Ok(())
Expand Down
22 changes: 13 additions & 9 deletions examples/async-graphql/src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ use nakago_axum::auth::Subject;
use nakago_derive::Provider;
use nakago_ws::connections::Connections;

use crate::domains::users::{self, model::User};
use crate::domains::users;

use super::messages::{IncomingMessage, OutgoingMessage};
use super::{
messages::{IncomingMessage, OutgoingMessage},
session::Session,
};

/// Message Handler
#[derive(Clone)]
pub struct Handler {
connections: Arc<Connections<User>>,
connections: Arc<Connections<Session>>,
users: Arc<Box<dyn users::Service>>,
}

#[async_trait]
impl nakago_ws::Handler<User> for Handler {
async fn get_user(&self, sub: Subject) -> Option<User> {
impl nakago_ws::Handler<Session> for Handler {
async fn get_session(&self, sub: Subject) -> Option<Session> {
if let Subject(Some(ref username)) = sub {
self.users
.get_by_username(username, &true)
.await
.unwrap_or(None)
.map(Session::new)
.ok()
} else {
None
}
Expand Down Expand Up @@ -58,12 +62,12 @@ pub struct Provide {}

#[Provider]
#[async_trait]
impl Provider<Box<dyn nakago_ws::Handler<User>>> for Provide {
impl Provider<Box<dyn nakago_ws::Handler<Session>>> for Provide {
async fn provide(
self: Arc<Self>,
i: nakago::Inject,
) -> provider::Result<Arc<Box<dyn nakago_ws::Handler<User>>>> {
let connections = i.get::<Connections<User>>().await?;
) -> provider::Result<Arc<Box<dyn nakago_ws::Handler<Session>>>> {
let connections = i.get::<Connections<Session>>().await?;
let users = i.get::<Box<dyn users::Service>>().await?;

Ok(Arc::new(Box::new(Handler { connections, users })))
Expand Down
3 changes: 3 additions & 0 deletions examples/async-graphql/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub mod handler;

/// WebSocket message types
pub mod messages;

/// WebSocket sessions
pub mod session;
37 changes: 37 additions & 0 deletions examples/async-graphql/src/events/session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use serde::{Deserialize, Serialize};

use crate::domains::users::model::User;

/// A Session tracking details about this particular connection
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum Session {
/// A session that is not associated with a User
#[default]
Anonymous,

/// A session that is associated with a User
User {
/// The User instance
user: User,
},
}

impl Session {
/// Create a new session for the given User
pub fn new(user: Option<User>) -> Self {
match user {
Some(user) => Self::User { user },
None => Self::Anonymous,
}
}

/// Get the User associated with this session, if any
#[allow(dead_code)]
pub fn get_user(&self) -> Option<&User> {
match self {
Session::Anonymous => None,
Session::User { user, .. } => Some(user),
}
}
}
4 changes: 2 additions & 2 deletions examples/async-graphql/src/http/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use axum::{extract::WebSocketUpgrade, response::IntoResponse};
use nakago_axum::{auth::Subject, Inject};

use crate::domains::users::model::User;
use crate::events::session::Session;

/// Handle WebSocket Events
pub async fn handle(
Inject(events_controller): Inject<nakago_ws::Controller<User>>,
Inject(events_controller): Inject<nakago_ws::Controller<Session>>,
sub: Subject,
ws: WebSocketUpgrade,
) -> axum::response::Result<impl IntoResponse> {
Expand Down
8 changes: 4 additions & 4 deletions examples/async-graphql/src/http/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use nakago::Inject;
use nakago_axum::{init::trace_layer, State};
use nakago_ws::{connections, controller};

use crate::{domains::users::model::User, events::handler};
use crate::events::{handler, session::Session};

use super::{events, graphql, health};

/// Provide dependencies needed for the HTTP service
pub async fn load(i: &Inject) -> nakago::Result<()> {
i.provide::<nakago_ws::Connections<User>>(connections::Provide::default())
i.provide::<nakago_ws::Connections<Session>>(connections::Provide::default())
.await?;

i.provide::<Box<dyn nakago_ws::Handler<User>>>(handler::Provide::default())
i.provide::<Box<dyn nakago_ws::Handler<Session>>>(handler::Provide::default())
.await?;

i.provide::<nakago_ws::Controller<User>>(controller::Provide::default())
i.provide::<nakago_ws::Controller<Session>>(controller::Provide::default())
.await?;

Ok(())
Expand Down
11 changes: 5 additions & 6 deletions examples/cqrs-es/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[package]
name = "nakago-examples-cqrs-es"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "A lightweight Rust toolkit for sharp dependency injection 😎"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

publish = false #

Expand Down
9 changes: 5 additions & 4 deletions examples/simple-warp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "nakago-examples-simple-warp"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "A lightweight Rust toolkit for sharp dependency injection 😎"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

publish = false

Expand Down
9 changes: 5 additions & 4 deletions examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "nakago-examples-simple"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "A lightweight Rust toolkit for sharp dependency injection 😎"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

publish = false

Expand Down
10 changes: 5 additions & 5 deletions nakago/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "nakago"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "A lightweight Rust toolkit for sharp dependency injection 😎"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

[dependencies]
anyhow = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions nakago_async_graphql/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "nakago-async-graphql"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "An Async-GraphQL integration for Nakago"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
documentation = "https://docs.rs/nakago-async-graphql/"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

[dependencies]
anyhow = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions nakago_axum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "nakago-axum"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "An Axum HTTP routes integration for Nakago"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
documentation = "https://docs.rs/nakago-axum/"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

[dependencies]
anyhow = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions nakago_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "nakago-derive"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "Macros for nakago"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
documentation = "https://docs.rs/nakago-derive/"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

[lib]
proc-macro = true
Expand Down
12 changes: 6 additions & 6 deletions nakago_figment/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "nakago-figment"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "Figment config loading utils for Nakago"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
documentation = "https://docs.rs/nakago-figment/"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

[dependencies]
anyhow = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions nakago_sea_orm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "nakago-sea-orm"
version = "0.23.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "A SeaORM integration for Nakago"
documentation = "https://docs.rs/nakago/"
homepage = "https://github.com/bkonkle/nakago"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"
documentation = "https://docs.rs/nakago-sea-orm/"
license.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true

[dependencies]
anyhow = "1.0"
Expand Down
Loading