Add Host property in HttpServerSupport
And change the defaults to: * "0.0.0.0" for the host * any available port, starting at 8080
This commit is contained in:
@@ -26,6 +26,8 @@ import org.springframework.http.server.reactive.HttpHandler;
|
||||
*/
|
||||
public interface HttpServer extends InitializingBean, Lifecycle {
|
||||
|
||||
void setHost(String host);
|
||||
|
||||
void setPort(int port);
|
||||
|
||||
void setHandler(HttpHandler handler);
|
||||
|
||||
@@ -17,23 +17,37 @@
|
||||
package org.springframework.http.server.reactive.boot;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class HttpServerSupport {
|
||||
|
||||
private String host = "0.0.0.0";
|
||||
|
||||
private int port = -1;
|
||||
|
||||
private HttpHandler httpHandler;
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
if(this.port == -1) {
|
||||
this.port = SocketUtils.findAvailableTcpPort(8080);
|
||||
}
|
||||
return this.port;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -44,10 +43,6 @@ public class JettyHttpServer extends HttpServerSupport implements InitializingBe
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
if (getPort() == -1) {
|
||||
setPort(SocketUtils.findAvailableTcpPort(8080));
|
||||
}
|
||||
|
||||
this.jettyServer = new Server();
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
@@ -59,6 +54,7 @@ public class JettyHttpServer extends HttpServerSupport implements InitializingBe
|
||||
contextHandler.addServlet(servletHolder, "/");
|
||||
|
||||
ServerConnector connector = new ServerConnector(this.jettyServer);
|
||||
connector.setHost(getHost());
|
||||
connector.setPort(getPort());
|
||||
this.jettyServer.addConnector(connector);
|
||||
}
|
||||
|
||||
@@ -39,9 +39,7 @@ public class ReactorHttpServer extends HttpServerSupport
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandler());
|
||||
|
||||
this.reactorServer = (getPort() != -1 ? reactor.io.netty.http.HttpServer.create(getPort()) :
|
||||
reactor.io.netty.http.HttpServer.create());
|
||||
this.reactorServer = reactor.io.netty.http.HttpServer.create(getHost(), getPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.http.server.reactive.boot;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter;
|
||||
@@ -38,9 +40,8 @@ public class RxNettyHttpServer extends HttpServerSupport implements HttpServer {
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandler());
|
||||
|
||||
this.rxNettyServer = (getPort() != -1 ?
|
||||
io.reactivex.netty.protocol.http.server.HttpServer.newServer(getPort()) :
|
||||
io.reactivex.netty.protocol.http.server.HttpServer.newServer());
|
||||
this.rxNettyServer = io.reactivex.netty.protocol.http.server.HttpServer
|
||||
.newServer(new InetSocketAddress(getHost(), getPort()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.File;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.core.StandardHost;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -46,11 +47,8 @@ public class TomcatHttpServer extends HttpServerSupport implements InitializingB
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
if (getPort() == -1) {
|
||||
setPort(SocketUtils.findAvailableTcpPort(8080));
|
||||
}
|
||||
|
||||
this.tomcatServer = new Tomcat();
|
||||
this.tomcatServer.setHostname(getHost());
|
||||
this.tomcatServer.setPort(getPort());
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
|
||||
@@ -44,8 +44,7 @@ public class UndertowHttpServer extends HttpServerSupport implements HttpServer
|
||||
Assert.notNull(getHttpHandler());
|
||||
HttpHandler handler =
|
||||
new UndertowHttpHandlerAdapter(getHttpHandler(), dataBufferFactory);
|
||||
int port = (getPort() != -1 ? getPort() : 8080);
|
||||
this.server = Undertow.builder().addHttpListener(port, "localhost")
|
||||
this.server = Undertow.builder().addHttpListener(getPort(), getHost())
|
||||
.setHandler(handler).build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user