package com.qqchen.deploy.backend.deploy.config;

import com.qqchen.deploy.backend.deploy.handler.ServerSSHWebSocketHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 */
@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private ServerSSHWebSocketHandler serverSSHWebSocketHandler;

    @Resource
    private WebSocketAuthInterceptor webSocketAuthInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        log.info("注册WebSocket处理器: /api/v1/server-ssh/connect/*");
        
        // 注册SSH终端WebSocket处理器（添加认证拦截器）
        registry.addHandler(serverSSHWebSocketHandler, "/api/v1/server-ssh/connect/{serverId}")
                .addInterceptors(webSocketAuthInterceptor)  // 添加认证拦截器
                .setAllowedOrigins("*");  // 生产环境建议配置具体的域名
    }
}
