package ink.wgink.gateway.filter.global;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: CountGolbalFilter
 * @Description: 计数过滤器
 * @Author: WangGeng
 * @Date: 2021/4/13 12:10
 * @Version: 1.0
 **/
@Slf4j
@Component
public class CountGlobalFilter implements GlobalFilter, Ordered {

    private AtomicInteger count = new AtomicInteger(0);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        URI uri = exchange.getRequest().getURI();
        System.out.println();
        System.out.println("uri: " + uri);
        System.out.println("count: "+ count.getAndAdd(1));
        System.out.println();

        return chain.filter(exchange);
//        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 10000;
    }
}
