package ru.ulstu.configuration;

import org.eclipse.jetty.server.ServerConnector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpListenerConfiguration implements EmbeddedServletContainerCustomizer {
    @Value("${server.http.port}")
    private int httpPort;

    private void configureJetty(JettyEmbeddedServletContainerFactory jettyFactory) {
        jettyFactory.addServerCustomizers((JettyServerCustomizer) server -> {
            ServerConnector serverConnector = new ServerConnector(server);
            serverConnector.setPort(httpPort);
            server.addConnector(serverConnector);
        });
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (container instanceof JettyEmbeddedServletContainerFactory) {
            configureJetty((JettyEmbeddedServletContainerFactory) container);
        }
    }
}
