package com.games.gateway.filters;

import com.games.gateway.config.FilterOrderedConfig;
import com.games.gateway.utils.IpAddressUtils;
import com.games.gateway.utils.TraceUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 打印请求日志 不输出 paylod
 */
@Order(value = FilterOrderedConfig.PrintRequestFilter)
@Slf4j
@Component
public class PrintRequestFilter implements GlobalFilter {

    public static List<String> pathIgnore = Lists.newArrayList("/","/robots.txt");

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        TraceUtils.setTraceId(request);
        String path = request.getPath().value();
        if(pathIgnore.contains(path)){
            exchange.getResponse().setStatusCode(HttpStatus.OK);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
}
