Skip to content

Commit

Permalink
Restore binding to any local address (rather than loopback address) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave authored Nov 30, 2024
1 parent 818e919 commit c79815f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
15 changes: 10 additions & 5 deletions avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public Executor executor() {
}

@Override
public JexConfig executor(Executor factory) {
this.executor = factory;
public JexConfig executor(Executor executor) {
this.executor = executor;
return this;
}

Expand Down Expand Up @@ -131,14 +131,19 @@ public Map<String, TemplateRender> renderers() {
return renderers;
}

@Override
public String scheme() {
return httpsConfig == null ? "http" : "https";
}

@Override
public HttpsConfigurator httpsConfig() {
return this.httpsConfig;
return httpsConfig;
}

@Override
public JexConfig httpsConfig(HttpsConfigurator ssl) {
this.httpsConfig = ssl;
public JexConfig httpsConfig(HttpsConfigurator httpsConfig) {
this.httpsConfig = httpsConfig;
return this;
}

Expand Down
5 changes: 4 additions & 1 deletion avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public sealed interface JexConfig permits DJexConfig {

/**
* Set the host on which the HttpServer will bind to.
* Set the host on which the HttpServer will bind to. Defaults to any local address.
*
* @param host The host.
*/
Expand Down Expand Up @@ -122,6 +122,9 @@ public sealed interface JexConfig permits DJexConfig {
/** Return the {@link HttpsConfigurator} if https is enabled. */
HttpsConfigurator httpsConfig();

/** Return the schema as http or https. */
String scheme();

/** Returns the configured compression settings. */
CompressionConfig compression();

Expand Down
28 changes: 15 additions & 13 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;

import io.avaje.applog.AppLog;
import io.avaje.jex.AppLifecycle;
import io.avaje.jex.Jex;
import io.avaje.jex.JexConfig;
import io.avaje.jex.core.SpiServiceManager;
import io.avaje.jex.routes.SpiRoutes;

Expand All @@ -23,25 +25,20 @@ public final class JdkServerStart {
public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) {
try {
final var config = jex.config();
final var port = config.port();
final var host = InetAddress.getByName(config.host());
final var socket = new InetSocketAddress(host, config.port());
final var contextPath = config.contextPath();
final var socketAddress = createSocketAddress(config);
final var https = config.httpsConfig();
final var backlog = config.socketBacklog();

final HttpServer server;
final String scheme;
if (https != null) {
var httpsServer = HttpsServer.create(socket, backlog);
var httpsServer = HttpsServer.create(socketAddress, config.socketBacklog());
httpsServer.setHttpsConfigurator(https);
server = httpsServer;
scheme = "https";
} else {
scheme = "http";
server = HttpServer.create(socket, backlog);
server = HttpServer.create(socketAddress, config.socketBacklog());
}

final var scheme = config.scheme();
final var contextPath = config.contextPath();
final var manager = new CtxServiceManager(serviceManager, scheme, contextPath);
final var handler = new RoutingHandler(routes, manager, config.compression());

Expand All @@ -51,12 +48,17 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana

jex.lifecycle().status(AppLifecycle.Status.STARTED);
log.log(
INFO,
"started com.sun.net.httpserver.HttpServer on port %s://%s:%s"
.formatted(scheme, host.getHostName(), port));
INFO,
"started com.sun.net.httpserver.HttpServer on port {0}://{1}",
scheme, socketAddress);
return new JdkJexServer(server, jex.lifecycle(), handler);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static InetSocketAddress createSocketAddress(JexConfig config) throws UnknownHostException {
final var inetAddress = config.host() == null ? null : InetAddress.getByName(config.host());
return new InetSocketAddress(inetAddress, config.port());
}
}

0 comments on commit c79815f

Please sign in to comment.