package com.oguerreiro.resilient.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpAndHttpsUndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {

  @Value("${resilient.server.http.enabled:false}")
  private Boolean httpEnabled;

  @Value("${resilient.server.http.port:8081}")
  private Integer httpPort;

  @Override
  public void customize(UndertowServletWebServerFactory factory) {
    if (httpEnabled) {
      factory.addBuilderCustomizers(builder -> builder.addHttpListener(httpPort, "0.0.0.0") // This enables HTTP on port 8081
      );
    }
  }
}
