Circular dependencies with state #236
-
I have a scenario where I'm hiring circular dependencie issue and wondering what is the best way to solve this. I have an let (layer, io) = SocketIo::builder().with_state(AuthService::new()).build_layer(); As you can see I can't pass Wondering if there is some way to set the state once let (layer, mut io) = SocketIo::builder().build_layer();
io.set_state(AuthService::new(io)); |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
What does the |
Beta Was this translation helpful? Give feedback.
-
There is no way to set state on |
Beta Was this translation helpful? Give feedback.
-
My #[async_trait]
pub trait AuthService: Sync + Send {
async fn decode_token(&self, token: &str) -> AppResult<DecodedToken>;
async fn verify_password(&self, username: &str, password: &Secret<String>) -> AppResult<()>;
async fn change_password_by_username(
&self,
username: &str,
change_password_request: &ChangePasswordRequest,
) -> AppResult<()>;
async fn update_user(&self, id: &str, update_user_request: UpdateUserRequest) -> AppResult<()>;
/// ..
} In async fn on_connect(socket: SocketRef, auth_service: State<Arc<AuthService>>) {
let headers = &socket.req_parts().headers;
let jwt = get_jwt_token_from_header(&headers).unwrap_or_default();
let decoded_token = auth_service.decode_token(jwt).await;
// ...
} As you can see I would like to avoid global variables. I was sort of hoping if we could have some way to set a state later in io, such that we state can be of type |
Beta Was this translation helpful? Give feedback.
-
Thank for the impl exemple. I ended up doing the same thing. @Totodore would it be possible to have an extractor that give us the SocketIo ? |
Beta Was this translation helpful? Give feedback.
You could also have a fn
set_io
that would set it on the AuthService but it would require to put a Rwlock.But I think the best way is to just have functions that takes a SocketIo ref as argument each time it is needed.