|
| 1 | +package kz.ncanode.configuration; |
| 2 | + |
| 3 | +import jakarta.servlet.http.HttpServletRequest; |
| 4 | +import jakarta.servlet.http.HttpServletResponse; |
| 5 | +import kz.ncanode.exception.WarmupException; |
| 6 | +import kz.ncanode.service.WarmupService; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import org.springframework.boot.actuate.health.Health; |
| 9 | +import org.springframework.boot.actuate.health.HealthIndicator; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 13 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 14 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 15 | + |
| 16 | +/** |
| 17 | + * Пока не скачаны сертификаты УЦ / не обновлены CRL: |
| 18 | + * <ul> |
| 19 | + * <li>{@code /actuator/health} показывает компоненты {@code ca} / {@code crl} со статусом DOWN |
| 20 | + * (а значит и общий статус — DOWN);</li> |
| 21 | + * <li>эндпоинты {@code /*}{@code /verify} отвечают 503.</li> |
| 22 | + * </ul> |
| 23 | + */ |
| 24 | +@Configuration |
| 25 | +@RequiredArgsConstructor |
| 26 | +public class WarmupConfiguration implements WebMvcConfigurer { |
| 27 | + private final WarmupService warmupService; |
| 28 | + |
| 29 | + @Bean |
| 30 | + public HealthIndicator caHealthIndicator() { |
| 31 | + return () -> warmupService.isCaReady() |
| 32 | + ? Health.up().build() |
| 33 | + : Health.down().withDetail("reason", "CA certificates are not downloaded yet").build(); |
| 34 | + } |
| 35 | + |
| 36 | + @Bean |
| 37 | + public HealthIndicator crlHealthIndicator() { |
| 38 | + return () -> warmupService.isCrlReady() |
| 39 | + ? Health.up().build() |
| 40 | + : Health.down().withDetail("reason", "CRL cache is not updated yet").build(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void addInterceptors(InterceptorRegistry registry) { |
| 45 | + registry.addInterceptor(new HandlerInterceptor() { |
| 46 | + @Override |
| 47 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { |
| 48 | + if (!warmupService.isReady()) { |
| 49 | + throw new WarmupException(); |
| 50 | + } |
| 51 | + return true; |
| 52 | + } |
| 53 | + }).addPathPatterns("/*/verify"); |
| 54 | + } |
| 55 | +} |
0 commit comments