elif-http 0.8.8

HTTP server core for the elif.rs LLM-friendly web framework
Documentation
//! WebSocket handler and upgrade mechanism - clean API for elif framework

use super::connection::WebSocketConnection;
use super::registry::ConnectionRegistry;
use super::types::{ConnectionId, WebSocketConfig, WebSocketResult};
use axum::extract::ws::WebSocketUpgrade as AxumWebSocketUpgrade;
use std::sync::Arc;

/// WebSocket upgrade handler - provides clean API over Axum WebSocket
/// This is a simplified version for the foundation
pub struct WebSocketUpgrade {
    /// WebSocket configuration
    _config: WebSocketConfig,
    /// Connection registry
    _registry: Arc<ConnectionRegistry>,
}

impl WebSocketUpgrade {
    /// Create a new WebSocket upgrade handler
    pub fn new(registry: Arc<ConnectionRegistry>) -> Self {
        Self {
            _config: WebSocketConfig::default(),
            _registry: registry,
        }
    }

    /// Create with custom configuration
    pub fn with_config(registry: Arc<ConnectionRegistry>, config: WebSocketConfig) -> Self {
        Self {
            _config: config,
            _registry: registry,
        }
    }

    /// Upgrade an HTTP connection to WebSocket
    /// Simplified for foundation - full implementation in later iterations
    pub async fn upgrade<H, F>(
        self,
        ws: AxumWebSocketUpgrade,
        _handler: H,
    ) -> axum::response::Response
    where
        H: FnOnce(ConnectionId, Arc<WebSocketConnection>) -> F + Send + 'static,
        F: std::future::Future<Output = ()> + Send + 'static,
    {
        ws.on_upgrade(|_socket| async move {
            tracing::info!("WebSocket connection upgraded (foundation mode)");
            // Full implementation will be added in later iterations
        })
    }
}

/// WebSocket handler trait for user-defined handlers
pub trait WebSocketHandler: Send + Sync + 'static {
    /// Handle a new WebSocket connection
    fn handle_connection(
        &self,
        id: ConnectionId,
        connection: Arc<WebSocketConnection>,
    ) -> impl std::future::Future<Output = ()> + Send;
}

/// Helper for extracting WebSocket upgrade from HTTP request
/// Simplified for foundation
pub fn extract_websocket_upgrade(
    ws: AxumWebSocketUpgrade,
) -> WebSocketResult<AxumWebSocketUpgrade> {
    Ok(ws)
}

/// Simple WebSocket handler implementation for basic use cases
#[derive(Clone)]
pub struct SimpleWebSocketHandler<F> {
    handler: F,
}

impl<F, Fut> SimpleWebSocketHandler<F>
where
    F: Fn(ConnectionId, Arc<WebSocketConnection>) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = ()> + Send,
{
    pub fn new(handler: F) -> Self {
        Self { handler }
    }
}

impl<F, Fut> WebSocketHandler for SimpleWebSocketHandler<F>
where
    F: Fn(ConnectionId, Arc<WebSocketConnection>) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = ()> + Send,
{
    async fn handle_connection(&self, id: ConnectionId, connection: Arc<WebSocketConnection>) {
        (self.handler)(id, connection).await;
    }
}

/// Macro for creating WebSocket handlers with clean syntax
/// Simplified for foundation
#[macro_export]
macro_rules! websocket_handler {
    (|$id:ident: ConnectionId, $conn:ident: Arc<WebSocketConnection>| $body:expr) => {
        SimpleWebSocketHandler::new(
            |$id: ConnectionId, $conn: Arc<WebSocketConnection>| async move { $body },
        )
    };
}