Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bkonkle committed Sep 4, 2024
1 parent d0bd200 commit af25543
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
13 changes: 7 additions & 6 deletions examples/async-graphql/src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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},
Expand All @@ -22,13 +22,14 @@ pub struct Handler {
}

#[async_trait]
impl nakago_ws::Handler<User> for Handler {
async fn get_session(&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(|u| Session::new(u))
.ok()
} else {
None
}
Expand Down Expand Up @@ -61,11 +62,11 @@ 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>>>> {
) -> 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?;

Expand Down
8 changes: 8 additions & 0 deletions examples/async-graphql/src/events/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ pub enum Session {
}

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> {
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

0 comments on commit af25543

Please sign in to comment.