package org.wfc.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.wfc.gateway.handler.SentinelFallbackHandler;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.http.HttpStatus;

/**
 * 网关限流配置
 * 
 * @author wfc
 */
@Configuration
public class GatewayConfig
{
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SentinelFallbackHandler sentinelGatewayExceptionHandler()
    {
        return new SentinelFallbackHandler();
    }

    /**
     * @return a RouterFunction that handles health check requests
     */
    @Bean
    public RouterFunction<ServerResponse> healthCheckRoute() {
        // Implement a health check endpoint
        return RouterFunctions.route(RequestPredicates.GET("/health"),
                        request -> ServerResponse.status(HttpStatus.OK)
                                .body(BodyInserters.fromValue("Gateway is healthy"))
                );
    }
}