package com.ensign.ensigngateway.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @Description: TODO
 * @Date: 5/11/2024 2:25 pm
 * @Created: by ZZSLL
 */

@Component
public class LoggingGatewayFilterFactory extends AbstractGatewayFilterFactory<LoggingGatewayFilterFactory.Config> {
    private static final Logger logger = LoggerFactory.getLogger(LoggingGatewayFilterFactory.class);

    public LoggingGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            logger.info("Request: {} {}", exchange.getRequest().getMethod(), exchange.getRequest().getURI());

            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                logger.info("Response status code: {}", exchange.getResponse().getStatusCode());
            }));
        };
    }

    @Override
    public String name() {
        return "LoggingGatewayFilter";
    }

    public static class Config {
        // 可以添加过滤器的配置信息
    }
}