package com.simuwang.deploy.components;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class RequestIdInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestId = request.getHeader(RequestIdContext.X_API_REQUESTID);
        if (StrUtil.isBlank(requestId)) {
            requestId = IdUtil.simpleUUID();
        }
        RequestIdContext.setRequestId(requestId);
        response.setHeader(RequestIdContext.X_API_REQUESTID, requestId);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
