Skip to content

Commit

Permalink
feat: (wip) facilitate route setup for WebSockets
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Dec 13, 2024
1 parent a27fb49 commit 7f61a0c
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion crates/ws-auth/src/runtime_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,87 @@
//! Houses code to make using WebSockets easier and extracts out the boilerplate
use std::sync::Arc;

use crate::{AuthTokenManager, WebSocketAuthError, WsId};
use actix_web::{dev::ConnectionInfo, web, HttpRequest, HttpResponse};
use anyhow::Context as _;
use wykies_shared::host_branch::HostId;
use wykies_shared::{e500, host_branch::HostId, session::UserSessionInfo, token::AuthToken};

/// Creates two closures that add routes for the Open and Protected endpoints for a websocket connection
pub fn create_ws_endpoints<T>(

Check failure on line 11 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

function `create_ws_endpoints` is never used

Check failure on line 11 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / ReleaseCompile

function `create_ws_endpoints` is never used

Check failure on line 11 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Test

function `create_ws_endpoints` is never used
name: &'static str,
ws_id: WsId,
client_loop_handler: impl WsClientLoopHandler<T>,

Check failure on line 14 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused variable: `client_loop_handler`

Check failure on line 14 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / ReleaseCompile

unused variable: `client_loop_handler`

Check failure on line 14 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `client_loop_handler`
) -> (
impl Fn(&mut web::ServiceConfig) + Send + Clone + 'static,
impl Fn(&mut web::ServiceConfig) + Send + Clone + 'static,
) {
let open_handler = move |auth_manager: web::Data<AuthTokenManager>,
conn: ConnectionInfo,
user_info: web::ReqData<UserSessionInfo>| {
get_token(auth_manager, conn, user_info, ws_id)
};

let add_open = |cfg: &mut web::ServiceConfig| {
cfg.route(name, web::get().to(ws_start_session));
};

let add_protected = move |cfg: &mut web::ServiceConfig| {
cfg.route(name, web::post().to(open_handler));
};

(add_open, add_protected)
}

#[tracing::instrument(ret, err(Debug))]
async fn get_token(
auth_manager: web::Data<AuthTokenManager>,
conn: ConnectionInfo,
user_info: web::ReqData<UserSessionInfo>,
ws_id: WsId,
) -> actix_web::Result<web::Json<AuthToken>> {
let result = AuthToken::new_rand();
let host_id: HostId = conn
.try_into()
.context("failed to get host_id")
.map_err(e500)?;
auth_manager.record_token(
host_id,
ws_id,
Arc::new(user_info.into_inner()),
result.clone(),
);
Ok(web::Json(result))
}

/// Handshake and start WebSocket handler with heartbeats.
#[tracing::instrument(skip(stream))]
pub async fn ws_start_session(
req: HttpRequest,
stream: web::Payload,
chat_server_handle: web::Data<WsId>,
auth_manager: web::Data<AuthTokenManager>,
conn: ConnectionInfo,
) -> Result<HttpResponse, WebSocketAuthError> {
let (session, msg_stream, client_identifier, res) =

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused variable: `session`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused variable: `msg_stream`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused variable: `client_identifier`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / ReleaseCompile

unused variable: `session`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / ReleaseCompile

unused variable: `msg_stream`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / ReleaseCompile

unused variable: `client_identifier`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `session`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `msg_stream`

Check failure on line 66 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `client_identifier`
create_ws_session(req, stream, conn, &auth_manager, **chat_server_handle)?;

// spawn websocket handler (don't await) so response is sent immediately
// spawn_local(chat_ws_start_client_handler_loop(
// (**chat_server_handle).clone(),
// session,
// msg_stream,
// auth_manager,
// client_identifier,
// ));

Ok(res)
}

pub trait WsClientLoopHandler<T>:

Check failure on line 81 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Clippy

trait `WsClientLoopHandler` is never used

Check failure on line 81 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / ReleaseCompile

trait `WsClientLoopHandler` is never used

Check failure on line 81 in crates/ws-auth/src/runtime_utils.rs

View workflow job for this annotation

GitHub Actions / Test

trait `WsClientLoopHandler` is never used
FnOnce(T, actix_ws::Session, actix_ws::MessageStream, web::Data<AuthTokenManager>, HostId)
{
}

pub fn create_ws_session(
req: HttpRequest,
Expand Down

0 comments on commit 7f61a0c

Please sign in to comment.